[FEATURE] TypoScript: Allow easy comparison against multiple values
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Sat, 11 Aug 2012 01:53:25 +0000 (03:53 +0200)
committerIngo Renner <ingo@typo3.org>
Fri, 17 Aug 2012 12:23:32 +0000 (14:23 +0200)
Allow easy comparison like:
[globalVar = TSFE:id = 10|12|15]   (in list)
[globalVar = TSFE:id != 10|12|15]  (not in list)

Change-Id: Iae920720ae6058c2cd741f74204c2fbce779e00f
Resolves: #39700
Releases: 6.0
Reviewed-on: http://review.typo3.org/13589
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Ingo Renner
Tested-by: Ingo Renner
t3lib/matchcondition/class.t3lib_matchcondition_abstract.php
tests/Unit/t3lib/matchcondition/class.t3lib_matchcondition_backendTest.php
tests/Unit/t3lib/matchcondition/class.t3lib_matchcondition_frontendTest.php

index f125461..53bfdc4 100644 (file)
@@ -482,7 +482,19 @@ abstract class t3lib_matchCondition_abstract {
                                        return ($leftValue <= doubleval($rightValue));
                                break;
                                case '!=':
-                                       return ($leftValue != doubleval($rightValue));
+                                               // multiple values may be split with '|'
+                                               // see if none matches ("not in list")
+                                       $found = FALSE;
+
+                                       $rightValueParts = t3lib_div::trimExplode('|', $rightValue);
+                                       foreach ($rightValueParts as $rightValueSingle) {
+                                               if ($leftValue === doubleval($rightValueSingle)) {
+                                                       $found = TRUE;
+                                                       break;
+                                               }
+                                       }
+
+                                       return ($found === FALSE);
                                break;
                                case '<':
                                        return ($leftValue < doubleval($rightValue));
@@ -492,7 +504,20 @@ abstract class t3lib_matchCondition_abstract {
                                break;
                                default:
                                                // nothing valid found except '=', use '='
-                                       return ($leftValue == trim($rightValue));
+
+                                               // multiple values may be split with '|'
+                                               // see if one matches ("in list")
+                                       $found = FALSE;
+
+                                       $rightValueParts = t3lib_div::trimExplode('|', $rightValue);
+                                       foreach ($rightValueParts as $rightValueSingle) {
+                                               if ($leftValue == $rightValueSingle) {
+                                                       $found = TRUE;
+                                                       break;
+                                               }
+                                       }
+
+                                       return $found;
                                break;
                        }
                }
index 6736c81..e146857 100644 (file)
@@ -381,6 +381,22 @@ class t3lib_matchCondition_backendTest extends tx_phpunit_testcase {
         * Tests whether numerical comparison matches.
         * @test
         */
+       public function globalVarConditionMatchesOnEqualExpressionWithMultipleValues() {
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
+
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
+       }
+
+       /**
+        * Tests whether numerical comparison matches.
+        * @test
+        */
        public function globalVarConditionMatchesOnNotEqualExpression() {
                $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
                $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
@@ -390,6 +406,15 @@ class t3lib_matchCondition_backendTest extends tx_phpunit_testcase {
         * Tests whether numerical comparison matches.
         * @test
         */
+       public function globalVarConditionMatchesOnNotEqualExpressionWithMultipleValues() {
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
+       }
+
+       /**
+        * Tests whether numerical comparison matches.
+        * @test
+        */
        public function globalVarConditionMatchesOnLowerThanExpression() {
                $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
                $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));
index 88827e9..ffc5eb4 100644 (file)
@@ -351,6 +351,22 @@ class t3lib_matchCondition_frontendTest extends tx_phpunit_testcase {
         * Tests whether numerical comparison matches.
         * @test
         */
+       public function globalVarConditionMatchesOnEqualExpressionWithMultipleValues() {
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 = 10|20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 = 10.1|20.2|30.3]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 = 10|20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 = 10.1|20.2|30.3]'));
+
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 == 10|20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 == 10.1|20.2|30.3]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20 == 10|20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:20.2 == 10.1|20.2|30.3]'));
+       }
+
+       /**
+        * Tests whether numerical comparison matches.
+        * @test
+        */
        public function globalVarConditionMatchesOnNotEqualExpression() {
                $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20]'));
                $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2]'));
@@ -360,6 +376,15 @@ class t3lib_matchCondition_frontendTest extends tx_phpunit_testcase {
         * Tests whether numerical comparison matches.
         * @test
         */
+       public function globalVarConditionMatchesOnNotEqualExpressionWithMultipleValues() {
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 != 20|30]'));
+               $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 != 10.2|20.3]'));
+       }
+
+       /**
+        * Tests whether numerical comparison matches.
+        * @test
+        */
        public function globalVarConditionMatchesOnLowerThanExpression() {
                $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10 < 20]'));
                $this->assertTrue($this->matchCondition->match('[globalVar = LIT:10.1 < 10.2]'));