diff --git a/Context.php b/Context.php index a4b3acc..9f49516 100644 --- a/Context.php +++ b/Context.php @@ -112,7 +112,7 @@ public function offsetGet($id) } if (true === is_callable($value)) { - if (true === is_string($value) && + if (true === is_string($value) && false === in_array(strtolower($value), get_defined_functions()['user'])) { return $value; } diff --git a/Model/Operator.php b/Model/Operator.php index 75742f3..e219cb1 100644 --- a/Model/Operator.php +++ b/Model/Operator.php @@ -47,9 +47,7 @@ * @copyright Copyright © 2007-2017 Hoa community * @license New BSD License */ -class Operator - extends Ruler\Model\Bag\Context - implements Visitor\Element +class Operator extends Ruler\Model\Bag\Context implements Visitor\Element { /** * Lazy evaluation should break. diff --git a/Test/Unit/DynamicCallable.php b/Test/Unit/DynamicCallable.php index 6b0378a..0ab35e0 100644 --- a/Test/Unit/DynamicCallable.php +++ b/Test/Unit/DynamicCallable.php @@ -53,7 +53,8 @@ class DynamicCallable extends Test\Unit\Suite public function case_is_a_xcallable() { $this - ->when($result = new SUT(function () {})) + ->when($result = new SUT(function () { + })) ->then ->object($result) ->isInstanceOf(Consistency\Xcallable::class); diff --git a/Test/Unit/Visitor/Asserter.php b/Test/Unit/Visitor/Asserter.php index 65b9087..367dde3 100644 --- a/Test/Unit/Visitor/Asserter.php +++ b/Test/Unit/Visitor/Asserter.php @@ -404,7 +404,8 @@ public function case_set_operator() ->given( $asserter = new SUT(), $oldOperators = $asserter->getOperators(), - $operator = function () {} + $operator = function () { + } ) ->when($result = $asserter->setOperator('_foo_', $operator)) ->then @@ -418,14 +419,34 @@ public function case_set_operator() ->isEqualTo(xcallable($operator)); } + public function case_set_operator_insensitive() + { + $this + ->given( + $asserter = new SUT(), + $operator = function () { + } + ) + ->when($result = $asserter->setOperator('_FΛO_', $operator)) + ->then + ->boolean($asserter->operatorExists('_FΛO_')) + ->isTrue() + ->boolean($asserter->operatorExists('_fλo_')) + ->isTrue() + ->object($asserter->getOperator('_fλo_')) + ->isEqualTo(xcallable($operator)); + } + public function case_set_operator_overwrite() { $this ->given( $asserter = new SUT(), - $asserter->setOperator('_foo_', function () {}), + $asserter->setOperator('_foo_', function () { + }), $oldOperators = $asserter->getOperators(), - $operator = function () {} + $operator = function () { + } ) ->when($result = $asserter->setOperator('_foo_', $operator)) ->then @@ -444,7 +465,8 @@ public function case_operator_exists() $this ->given( $asserter = new SUT(), - $asserter->setOperator('_foo_', function () {}) + $asserter->setOperator('_foo_', function () { + }) ) ->when($result = $asserter->operatorExists('_foo_')) ->then @@ -467,7 +489,8 @@ public function case_get_operator() $this ->given( $asserter = new SUT(), - $operator = function () {}, + $operator = function () { + }, $asserter->setOperator('_foo_', $operator) ) ->when($result = $asserter->getOperator('_foo_')) diff --git a/Visitor/Asserter.php b/Visitor/Asserter.php index ca15d60..c8d2c3e 100644 --- a/Visitor/Asserter.php +++ b/Visitor/Asserter.php @@ -77,19 +77,43 @@ public function __construct(Ruler\Context $context = null) $this->setContext($context); } - $this->setOperator('and', function ($a = false, $b = false) { return $a && $b; }); - $this->setOperator('or', function ($a = false, $b = false) { return $a || $b; }); - $this->setOperator('xor', function ($a, $b) { return (bool) ($a ^ $b); }); - $this->setOperator('not', function ($a) { return !$a; }); - $this->setOperator('=', function ($a, $b) { return $a == $b; }); - $this->setOperator('is', $this->getOperator('=')); - $this->setOperator('!=', function ($a, $b) { return $a != $b; }); - $this->setOperator('>', function ($a, $b) { return $a > $b; }); - $this->setOperator('>=', function ($a, $b) { return $a >= $b; }); - $this->setOperator('<', function ($a, $b) { return $a < $b; }); - $this->setOperator('<=', function ($a, $b) { return $a <= $b; }); - $this->setOperator('in', function ($a, array $b) { return in_array($a, $b); }); - $this->setOperator('sum', function () { return array_sum(func_get_args()); }); + $this->setOperator('and', function ($a = false, $b = false) { + return $a && $b; + }); + $this->setOperator('or', function ($a = false, $b = false) { + return $a || $b; + }); + $this->setOperator('xor', function ($a, $b) { + return (bool) ($a ^ $b); + }); + $this->setOperator('not', function ($a) { + return !$a; + }); + $this->setOperator('=', function ($a, $b) { + return $a == $b; + }); + $this->setOperator('is', $this->getOperator('=')); + $this->setOperator('!=', function ($a, $b) { + return $a != $b; + }); + $this->setOperator('>', function ($a, $b) { + return $a > $b; + }); + $this->setOperator('>=', function ($a, $b) { + return $a >= $b; + }); + $this->setOperator('<', function ($a, $b) { + return $a < $b; + }); + $this->setOperator('<=', function ($a, $b) { + return $a <= $b; + }); + $this->setOperator('in', function ($a, array $b) { + return in_array($a, $b); + }); + $this->setOperator('sum', function () { + return array_sum(func_get_args()); + }); $this->setOperator('matches', function ($subject, $pattern) { $escapedPattern = preg_replace('/(?_operators[$operator] = $callable; + $this->_operators[mb_strtolower($operator)] = $callable; return $this; } @@ -512,7 +536,7 @@ public function setOperator($operator, $callable) */ public function operatorExists($operator) { - return true === array_key_exists($operator, $this->_operators); + return true === array_key_exists(mb_strtolower($operator), $this->_operators); } /** @@ -527,6 +551,8 @@ public function getOperator($operator) return null; } + $operator = mb_strtolower($operator); + $handle = &$this->_operators[$operator]; if (!($handle instanceof Consistency\Xcallable)) { diff --git a/Visitor/Compiler.php b/Visitor/Compiler.php index 99fe953..b14799e 100644 --- a/Visitor/Compiler.php +++ b/Visitor/Compiler.php @@ -100,7 +100,7 @@ public function visit(Visitor\Element $element, &$handle = null, $eldnah = null) $out .= '(' . "\n"; } else { - $out .= 'func(' . "\n" . $_ . ' '; + $out .= 'func(' . "\n" . $_ . ' '; $_handle[] = '\'' . $name . '\''; } @@ -173,7 +173,7 @@ protected function visitContext(Ruler\Model\Bag\Context $context, &$handle, $eld ++$this->_indentation; $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE]; - $out .= "\n" . $_ . ' ->'; + $out .= "\n" . $_ . ' ->'; switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) { case Ruler\Model\Bag\Context::ARRAY_ACCESS: