Skip to content

Commit

Permalink
Add extension point for matching using custom conditions or standards.
Browse files Browse the repository at this point in the history
* Add comparison type implementations
* Centralize the comparison at the right place
* enable a new extension place to provide custom operators.
* Add MatchClosure Expression
  • Loading branch information
yvoyer committed Dec 12, 2016
1 parent 0b0b541 commit 678664e
Show file tree
Hide file tree
Showing 18 changed files with 770 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,78 +124,7 @@ public static function sortByField($name, $orientation = 1, \Closure $next = nul
*/
public function walkComparison(Comparison $comparison)
{
$field = $comparison->getField();
$value = $comparison->getValue()->getValue(); // shortcut for walkValue()

switch ($comparison->getOperator()) {
case Comparison::EQ:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) === $value;
};

case Comparison::NEQ:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) !== $value;
};

case Comparison::LT:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) < $value;
};

case Comparison::LTE:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) <= $value;
};

case Comparison::GT:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) > $value;
};

case Comparison::GTE:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) >= $value;
};

case Comparison::IN:
return function ($object) use ($field, $value) {
return in_array(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value);
};

case Comparison::NIN:
return function ($object) use ($field, $value) {
return ! in_array(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value);
};

case Comparison::CONTAINS:
return function ($object) use ($field, $value) {
return false !== strpos(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value);
};

case Comparison::MEMBER_OF:
return function ($object) use ($field, $value) {
$fieldValues = ClosureExpressionVisitor::getObjectFieldValue($object, $field);
if (!is_array($fieldValues)) {
$fieldValues = iterator_to_array($fieldValues);
}
return in_array($value, $fieldValues);
};

case Comparison::STARTS_WITH:
return function ($object) use ($field, $value) {
return 0 === strpos(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value);
};

case Comparison::ENDS_WITH:
return function ($object) use ($field, $value) {
return $value === substr(ClosureExpressionVisitor::getObjectFieldValue($object, $field), -strlen($value));
};


default:
throw new \RuntimeException("Unknown comparison operator: " . $comparison->getOperator());
}
return $comparison->getFilterCallback();
}

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/Doctrine/Common/Collections/Expr/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Comparison implements Expression
const CONTAINS = 'CONTAINS';
const MEMBER_OF = 'MEMBER_OF';
const STARTS_WITH = 'STARTS_WITH';
const ENDS_WITH = 'ENDS_WITH';
const ENDS_WITH = 'ENDS_WITH';
/**
* @var string
*/
Expand Down Expand Up @@ -102,4 +102,13 @@ public function visit(ExpressionVisitor $visitor)
{
return $visitor->walkComparison($this);
}

/**
* @return \Closure
*/
public function getFilterCallback()
{
// todo should be abstract, defined for BC
throw new \RuntimeException(__METHOD__ . ' is not defined yet, you can override it.');
}
}
55 changes: 55 additions & 0 deletions lib/Doctrine/Common/Collections/Expr/Comparison/Contains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Common\Collections\Expr\Comparison;

use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\Value;

/**
* @author Yannick Voyer (http://github.com/yvoyer)
*/
final class Contains extends Comparison
{
/**
* @param string $field
* @param mixed|Value $value
*/
public function __construct($field, $value)
{
parent::__construct($field, Comparison::CONTAINS, $value);
}

/**
* @return callable
*/
public function getFilterCallback()
{
$value = $this->getValue()->getValue();
$field = $this->getField();

return function ($object) use ($value, $field) {
return false !== strpos(
ClosureExpressionVisitor::getObjectFieldValue($object, $field),
$value
);
};
}
}
55 changes: 55 additions & 0 deletions lib/Doctrine/Common/Collections/Expr/Comparison/EndsWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Common\Collections\Expr\Comparison;

use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\Value;

/**
* @author Yannick Voyer (http://github.com/yvoyer)
*/
final class EndsWith extends Comparison
{
/**
* @param string $field
* @param mixed|Value $value
*/
public function __construct($field, $value)
{
parent::__construct($field, Comparison::ENDS_WITH, $value);
}

/**
* @return callable
*/
public function getFilterCallback()
{
$value = $this->getValue()->getValue();
$field = $this->getField();

return function ($object) use ($value, $field) {
return $value === substr(
ClosureExpressionVisitor::getObjectFieldValue($object, $field),
-strlen($this->getValue()->getValue())
);
};
}
}
52 changes: 52 additions & 0 deletions lib/Doctrine/Common/Collections/Expr/Comparison/Equal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Common\Collections\Expr\Comparison;

use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\Value;

/**
* @author Yannick Voyer (http://github.com/yvoyer)
*/
final class Equal extends Comparison
{
/**
* @param string $field
* @param mixed|Value $value
*/
public function __construct($field, $value)
{
parent::__construct($field, Comparison::EQ, $value);
}

/**
* @return callable
*/
public function getFilterCallback()
{
$value = $this->getValue()->getValue();
$field = $this->getField();

return function ($object) use ($value, $field) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) === $value;
};
}
}
52 changes: 52 additions & 0 deletions lib/Doctrine/Common/Collections/Expr/Comparison/GreaterThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Common\Collections\Expr\Comparison;

use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\Value;

/**
* @author Yannick Voyer (http://github.com/yvoyer)
*/
final class GreaterThan extends Comparison
{
/**
* @param string $field
* @param mixed|Value $value
*/
public function __construct($field, $value)
{
parent::__construct($field, Comparison::GT, $value);
}

/**
* @return callable
*/
public function getFilterCallback()
{
$value = $this->getValue()->getValue();
$field = $this->getField();

return function ($object) use ($value, $field) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) > $value;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Common\Collections\Expr\Comparison;

use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\Value;

/**
* @author Yannick Voyer (http://github.com/yvoyer)
*/
final class GreaterThanEqual extends Comparison
{
/**
* @param string $field
* @param mixed|Value $value
*/
public function __construct($field, $value)
{
parent::__construct($field, Comparison::GTE, $value);
}

/**
* @return callable
*/
public function getFilterCallback()
{
$value = $this->getValue()->getValue();
$field = $this->getField();

return function ($object) use ($value, $field) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) >= $value;
};
}
}
Loading

0 comments on commit 678664e

Please sign in to comment.