[TASK] Replace Comparator with Comparison
authorOliver Eglseder <oliver.eglseder@in2code.de>
Tue, 8 Sep 2015 17:06:13 +0000 (19:06 +0200)
committerOliver Eglseder <oliver.eglseder@in2code.de>
Tue, 8 Sep 2015 17:06:13 +0000 (19:06 +0200)
Classes/Domain/Comparator/Comparator.php [deleted file]
Classes/Domain/Comparator/Comparison.php [new file with mode: 0644]
Classes/Domain/Model/Rule.php

diff --git a/Classes/Domain/Comparator/Comparator.php b/Classes/Domain/Comparator/Comparator.php
deleted file mode 100644 (file)
index dc6febf..0000000
+++ /dev/null
@@ -1,152 +0,0 @@
-<?php
-namespace In2code\PowermailCond\Domain\Comparator;
-
-/***************************************************************
- *  Copyright notice
- *
- *  (c) 2015 in2code.de
- *  Alex Kellner <alexander.kellner@in2code.de>,
- *  Oliver Eglseder <oliver.eglseder@in2code.de>
- *
- *  All rights reserved
- *
- *  This script is part of the TYPO3 project. The TYPO3 project is
- *  free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  The GNU General Public License can be found at
- *  http://www.gnu.org/copyleft/gpl.html.
- *
- *  This script is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
-
-use In2code\PowermailCond\Domain\Model\Rule;
-
-/**
- * Class Comparator
- */
-class Comparator {
-
-       /**
-        * @var callable[]
-        */
-       protected $callbacks = array();
-
-       /**
-        *
-        */
-       public function __construct() {
-               $this->callbacks = array(
-                       Rule::OPERATOR_IS_SET => array($this, 'operationIsSet'),
-                       Rule::OPERATOR_NOT_IS_SET => array($this, 'operationNotIsSet'),
-                       Rule::OPERATOR_CONTAINS_VALUE => array($this, 'operationContainsValue'),
-                       Rule::OPERATOR_NOT_CONTAINS_VALUE => array($this, 'operationNotContainsValue'),
-                       Rule::OPERATOR_IS => array($this, 'operationIs'),
-                       Rule::OPERATOR_NOT_IS => array($this, 'operationNotIs'),
-                       Rule::OPERATOR_GREATER_THAN => array($this, 'operationGreaterThan'),
-                       Rule::OPERATOR_LESS_THAN => array($this, 'operationLessThan'),
-                       Rule::OPERATOR_CONTAINS_VALUE_FROM_FIELD => array($this, 'operationContainsValueFromField'),
-                       Rule::OPERATOR_NOT_CONTAINS_VALUE_FROM_FIELD => array($this, 'operationNotContainsValueFromField'),
-               );
-       }
-
-       /**
-        * @param int $operator
-        * @return callable
-        */
-       public function getCallbackForOperator($operator) {
-               return $this->callbacks[$operator];
-       }
-
-       /**
-        * @param string $left
-        * @return bool
-        */
-       public function operationIsSet($left) {
-               return !$this->operationNotIsSet($left);
-       }
-
-       /**
-        * @param string $left
-        * @return bool
-        */
-       public function operationNotIsSet($left) {
-               return empty($left);
-       }
-
-       /**
-        * @param string $left
-        * @param string $right
-        * @return bool
-        */
-       public function operationContainsValue($left, $right) {
-               if ($right === '') {
-                       return FALSE;
-               }
-               if (is_array($left)) {
-                       return in_array($right, $left);
-               }
-               return (strpos($left, $right) !== FALSE);
-       }
-
-       /**
-        * @param string $left
-        * @param string $right
-        * @return bool
-        */
-       public function operationNotContainsValue($left, $right) {
-               return !$this->operationContainsValue($left, $right);
-       }
-
-       /**
-        * @param string $left
-        * @param string $right
-        * @return bool
-        */
-       public function operationIs($left, $right) {
-               return ($left === $right);
-       }
-
-       /**
-        * @param string $left
-        * @param string $right
-        * @return bool
-        */
-       public function operationNotIs($left, $right) {
-               return ($left !== $right);
-       }
-
-       /**
-        * @param string|int $left
-        * @param string|int $right
-        * @return bool
-        */
-       public function operationGreaterThan($left, $right) {
-               return (int) $left > (int) $right;
-       }
-
-       /**
-        * @param string|int $left
-        * @param string|int $right
-        * @return bool
-        */
-       public function operationLessThan($left, $right) {
-               return (int) $left < (int) $right;
-       }
-
-
-       public function operationContainsValueFromField($left, $right, $field) {
-               return $this->operationContainsValue($left, $field->getText());
-       }
-
-       public function operationNotContainsValueFromField($left, $right, $field) {
-               return $this->operationNotContainsValue($left, $field->getText());
-       }
-}
diff --git a/Classes/Domain/Comparator/Comparison.php b/Classes/Domain/Comparator/Comparison.php
new file mode 100644 (file)
index 0000000..5709bff
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+namespace In2code\PowermailCond\Domain\Comparator;
+
+use In2code\Powermail\Domain\Model\Field;
+use In2code\PowermailCond\Domain\Model\Rule;
+
+/**
+ * Class Comparison
+ */
+class Comparison {
+
+       /**
+        * @var int
+        */
+       protected $operation = 0;
+
+       /**
+        * @param int $operation
+        */
+       public function __construct($operation) {
+               $this->operation = $operation;
+       }
+
+       /**
+        * @param Field $leftField
+        * @param string $valueToMatch
+        * @param Field $rightField
+        * @return bool
+        */
+       public function evaluate(Field $leftField, $valueToMatch = '', Field $rightField = NULL) {
+               $result = FALSE;
+               switch ($this->operation) {
+                       case Rule::OPERATOR_IS_SET:
+                               $result = $this->operationIsNotEmpty($leftField->getText());
+                               break;
+                       case Rule::OPERATOR_NOT_IS_SET:
+                               $result = !$this->operationIsNotEmpty($leftField->getText());
+                               break;
+                       case Rule::OPERATOR_CONTAINS_VALUE:
+                               $result = $this->operationContains($leftField->getText(), $valueToMatch);
+                               break;
+                       case Rule::OPERATOR_NOT_CONTAINS_VALUE:
+                               $result = !$this->operationContains($leftField->getText(), $valueToMatch);
+                               break;
+                       case Rule::OPERATOR_IS:
+                               $result = ($leftField->getText() === $valueToMatch);
+                               break;
+                       case Rule::OPERATOR_NOT_IS:
+                               $result = ($leftField->getText() !== $valueToMatch);
+                               break;
+                       case Rule::OPERATOR_GREATER_THAN:
+                               $result = (((int) $leftField->getText()) > ((int) $valueToMatch));
+                               break;
+                       case Rule::OPERATOR_LESS_THAN:
+                               $result = (((int) $leftField->getText()) < ((int) $valueToMatch));
+                               break;
+                       case Rule::OPERATOR_CONTAINS_VALUE_FROM_FIELD:
+                               if ($rightField instanceof Field) {
+                                       $result = $this->operationContains($rightField->getText(), $leftField->getText());
+                               }
+                               break;
+                       case Rule::OPERATOR_NOT_CONTAINS_VALUE_FROM_FIELD:
+                               if ($rightField instanceof Field) {
+                                       $result = !$this->operationContains($rightField->getText(), $leftField->getText());
+                               }
+                               break;
+               }
+               return $result;
+       }
+
+       /**
+        * @param $value
+        * @return bool
+        */
+       protected function operationIsNotEmpty($value) {
+               return !empty($value);
+       }
+
+       /**
+        * @param string|array $haystack
+        * @param string $needle
+        * @return bool
+        */
+       protected function operationContains($haystack, $needle) {
+               if (!$this->operationIsNotEmpty($needle)) {
+                       return FALSE;
+               }
+               if (is_array($haystack)) {
+                       return in_array($needle, $haystack);
+               }
+               return (strpos($haystack, $needle) !== FALSE);
+       }
+}
index dce9cd9..3b74aad 100644 (file)
@@ -31,6 +31,7 @@ use In2code\Powermail\Domain\Model\Field;
 use In2code\Powermail\Domain\Model\Form;
 use In2code\Powermail\Domain\Model\Page;
 use In2code\PowermailCond\Domain\Comparator\Comparator;
+use In2code\PowermailCond\Domain\Comparator\Comparison;
 use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
 
 /**
@@ -178,13 +179,16 @@ class Rule extends AbstractEntity {
         * @return bool
         */
        public function applies(Form $form) {
-               $comparator = new Comparator();
+               $equalField = NULL;
                /** @var Page $page */
-               foreach ($form->getPages() as $page) {
-                       /** @var Field $field */
-                       foreach ($page->getFields() as $field) {
-                               if ($field->getUid() === (int) $this->equalField) {
-                                       $equalField = $field;break;
+               if (((int)$this->equalField) > 0 ) {
+                       foreach ($form->getPages() as $page) {
+                               /** @var Field $field */
+                               foreach ($page->getFields() as $field) {
+                                       if ($field->getUid() === (int) $this->equalField) {
+                                               $equalField = $field;
+                                               break;
+                                       }
                                }
                        }
                }
@@ -193,8 +197,8 @@ class Rule extends AbstractEntity {
                        /** @var Field $field */
                        foreach ($page->getFields() as $field) {
                                if ($field === $this->startField) {
-                                       $comparison = $comparator->getCallbackForOperator($this->ops);
-                                       if ($comparison($field->getText(), $this->condString, $equalField)) {
+                                       $comparison = new Comparison($this->ops);
+                                       if ($comparison->evaluate($field, $this->condString, $equalField)) {
                                                return TRUE;
                                        }
                                }