Skip to content

Commit

Permalink
Context accepts now array values, containment operator added, small r…
Browse files Browse the repository at this point in the history
…efactor, license and more tests.
  • Loading branch information
subzeta committed Jan 29, 2016
1 parent 7b0c64b commit 7ed056d
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 103 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Sergio Cabrera Dur�n

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Comparison | same as | ===
Comparison | not same as | !==
Logical | and | &&
Logical | or | \|\|
Containment | contained in (alias: in) | in

# Notes
* It's not necessary to provide callbacks for *execute* method, it will return a boolean instead as *assert* does.
Expand All @@ -85,10 +86,15 @@ $ phpunit
* Increase the number of unit tests to prevent bad contexts or bad formatted rules from being executed.

# To do
* Add more operators (in, for example).
* Improve the interpreted method response.

# Changelist
* Allow aliases ("is equal to" can be written as "is" and "is not equal to" as "is not"/"isn't").
* Context values may permit callable functions too.
* Added the strict comparison operators (same as, not same as).
* It can be interesting to implement a kind of *dump* method to show the interpreted rule.
* Added the "in" operator.
* Context accepts array values.

# License
MIT
4 changes: 3 additions & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ private function processValue($value)

$value = is_callable($value) ? $value() : $value;

if (is_string($value)) {
if (is_array($value)) {
$value = '['.implode(',', array_map(function($e) {return is_string($e) ? '"'.$e.'"' : $e;}, $value)).']';
} elseif (is_string($value)) {
$value = '"'.$value.'"';
} elseif (is_bool($value)) {
$value = $value ? 'true' : 'false';
Expand Down
63 changes: 33 additions & 30 deletions src/Evaluator/Evaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class Evaluator
*/
public function assert($rules, $context)
{
foreach ($rules->get() as $rule) {
if (!(new Rule($this->prepare($rule, $context)))->isTrue()) {
return false;
}
}

return true;
return array_product(
array_map(
function($rule) {
return (new Rule($rule))->isTrue();
},
$this->interpret($rules, $context)
)
);
}

/**
Expand All @@ -33,13 +34,14 @@ public function assert($rules, $context)
*/
public function valid($rules, $context)
{
foreach ($rules->get() as $rule) {
if (!(new Rule($this->prepare($rule, $context)))->isValid()) {
return false;
}
}

return true;
return array_product(
array_map(
function($rule) {
return (new Rule($rule))->isValid();
},
$this->interpret($rules, $context)
)
);
}

/**
Expand All @@ -49,13 +51,22 @@ public function valid($rules, $context)
*/
public function interpret($rules, $context)
{
$interpretations = [];

foreach ($rules->get() as $rule) {
$interpretations[] = $this->prepare($rule, $context);
}
return $this->build($rules, $context);
}

return $interpretations;
/**
* @param RuleCollection $rules
* @param Context $context
* @return string[]
*/
private function build($rules, $context)
{
return array_map(
function($rule) use ($context) {
return $this->prepare($rule, $context);
},
$rules->get()
);
}

/**
Expand All @@ -65,16 +76,8 @@ public function interpret($rules, $context)
*/
private function prepare($rule, $context)
{
$replacements = array_merge(
(new ComparisonOperator())->getAll(),
(new LogicalOperator())->getAll(),
$context->get()
);

foreach ($replacements as $search => $replace) {
$rule = str_replace($search, $replace, $rule);
}
$replacements = array_merge((new ComparisonOperator())->all(), (new LogicalOperator())->all(), $context->get());

return $rule;
return str_replace(array_keys($replacements), array_values($replacements), $rule);
}
}
4 changes: 3 additions & 1 deletion src/Operator/ComparisonOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ComparisonOperator
/**
* @return array
*/
public function getAll()
public function all()
{
return [
' is greater than ' => ' > ',
Expand All @@ -22,6 +22,8 @@ public function getAll()
' isn\'t ' => ' != ',
' isn"t ' => ' != ',
' is ' => ' == ',
' contained in ' => ' in ',
' in ' => ' in ',
];
}
}
2 changes: 1 addition & 1 deletion src/Operator/LogicalOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class LogicalOperator
/**
* @return array
*/
public function getAll()
public function all()
{
return [
' and ' => ' && ',
Expand Down
Loading

0 comments on commit 7ed056d

Please sign in to comment.