at the end of the day, it was inevitable
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
|
||||
|
||||
/**
|
||||
* Class AbstractChainExpander
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
abstract class AbstractChainExpander extends AbstractExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PatternExpander[]
|
||||
*/
|
||||
protected $expanders = [];
|
||||
|
||||
/**
|
||||
* @param PatternExpander $expander A PatternExpander instance.
|
||||
* @param PatternExpander $expander,... A PatternExpander's instances.
|
||||
*/
|
||||
public function __construct(PatternExpander $expander)
|
||||
{
|
||||
$this->expanders[] = $expander;
|
||||
|
||||
if (func_num_args() > 1) {
|
||||
$arguments = func_get_args();
|
||||
$length = count($arguments);
|
||||
for ($i = 1; $i < $length; ++$i) {
|
||||
if (!$arguments[$i] instanceof PatternExpander) {
|
||||
throw new \InvalidArgumentException('Has invalid expander.');
|
||||
}
|
||||
|
||||
$this->expanders[] = $arguments[$i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
foreach ($this->expanders as $expander) {
|
||||
if (! $expander->match($value)) {
|
||||
$className = get_class($expander);
|
||||
$className = substr($className, strrpos($className, '\\'));
|
||||
|
||||
$this->error = "Expander {$className} don't matches value: ".
|
||||
$expander->getError();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
|
||||
|
||||
/**
|
||||
* Class AbstractExpander
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
abstract class AbstractExpander implements PatternExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $error;
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Common\Util\Converter\DateConverter;
|
||||
|
||||
/**
|
||||
* Class BetweenExpander
|
||||
* Check that value is between specified bounds.
|
||||
* Except integers, float and datetime values.
|
||||
*
|
||||
* Example:
|
||||
* - integer.between(10, 20)
|
||||
* - date.between('2017-10-01', '2017-11-01')
|
||||
* - .field('date', between('2017-10-01', '2017-11-01'))
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class BetweenExpander extends AbstractExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @var integer|float|string
|
||||
*/
|
||||
private $start;
|
||||
|
||||
/**
|
||||
* @var integer|float|string
|
||||
*/
|
||||
private $end;
|
||||
|
||||
/**
|
||||
* @param integer|float|string $start Start bound.
|
||||
* @param integer|float|string $end End bound.
|
||||
*/
|
||||
public function __construct($start, $end)
|
||||
{
|
||||
$this->start = $start;
|
||||
$this->end = $end;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
if (! is_numeric($value) && ! is_int($value) && ! is_float($value)
|
||||
&& ! is_string($value)) {
|
||||
$this->error = 'Can match only integers, float and datetime values';
|
||||
return false;
|
||||
}
|
||||
|
||||
$start = $this->start;
|
||||
$end = $this->end;
|
||||
if (DateConverter::can($value)) {
|
||||
// For string which represent date try to convert it into \DateTime
|
||||
// instances.
|
||||
try {
|
||||
$value = DateConverter::convert($value);
|
||||
$start = DateConverter::convert($start);
|
||||
$end = DateConverter::convert($end);
|
||||
|
||||
$value->setTimezone($start->getTimezone());
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// For scalar types convert all values to the same type.
|
||||
$type = gettype($start);
|
||||
settype($value, $type);
|
||||
}
|
||||
|
||||
return ($value >= $start) && ($value <= $end);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Common\Util\Matcher\AppMatcher;
|
||||
|
||||
/**
|
||||
* Class EntityExpander
|
||||
* Check that expanded object is serialized application entity.
|
||||
*
|
||||
* Example: object.entity('AppBundle:User', 'user, post')
|
||||
*
|
||||
* @package Util\Matcher\Expander
|
||||
*/
|
||||
class EntityExpander extends AbstractExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $entityName;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $serializationGroups;
|
||||
|
||||
/**
|
||||
* @param string $entityName Entity name like
|
||||
* 'BundleName:EntityName'.
|
||||
* @param string|array $serializationGroups Serialization groups in string
|
||||
* format delimited by ','.
|
||||
*/
|
||||
public function __construct($entityName, $serializationGroups = [])
|
||||
{
|
||||
$this->entityName = $entityName;
|
||||
|
||||
// Split serialization groups string into array.
|
||||
$serializationGroups = explode(',', $serializationGroups);
|
||||
// Trim values and remove empty.
|
||||
$serializationGroups = array_filter(
|
||||
array_map('trim', $serializationGroups)
|
||||
);
|
||||
|
||||
$this->serializationGroups = $serializationGroups;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
// Get entity metadata for specified entity.
|
||||
$metadata = AppMatcher::getEntityMetadata($this->entityName);
|
||||
// Get patter fot specified entity with given serialization group.
|
||||
$pattern = $metadata->getPattern($this->serializationGroups);
|
||||
|
||||
if ($pattern && ! AppMatcher::match($value, $pattern, $this->error)) {
|
||||
$this->error =
|
||||
"Invalid entity {$this->entityName}: {$this->error}";
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Coduo\ToString\StringConverter;
|
||||
|
||||
/**
|
||||
* Class EveryExpander
|
||||
* Check that every array elements matches specified expander.
|
||||
*
|
||||
* Example:
|
||||
* - array.every(field('property', value))
|
||||
* - array.every(entity('AppBundle:User', 'user, post'))
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class EveryExpander extends AbstractChainExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
if (! is_array($value)) {
|
||||
$this->error = 'Every expander require "array", got '.
|
||||
new StringConverter($value) .'.';
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($value as $row) {
|
||||
if (! parent::match($row)) {
|
||||
$this->error = 'Checked value '. new StringConverter($row)
|
||||
.' is invalid: '. $this->error;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
|
||||
|
||||
/**
|
||||
* Class FieldExpander
|
||||
* Check that expanded object or array has specific field which matched
|
||||
* given matcher expander.
|
||||
*
|
||||
* Example:
|
||||
* - .field('username', contains('admin'))
|
||||
* - .field('user', entity('AppBundle:User', 'user'), field('id', 1))
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class FieldExpander extends AbstractExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $fieldName;
|
||||
|
||||
/**
|
||||
* @var mixed|PatternExpander[]
|
||||
*/
|
||||
private $expander;
|
||||
|
||||
/**
|
||||
* @param string $fieldName Field name.
|
||||
* @param mixed|PatternExpander $expander A PatternExpander instance.
|
||||
* @param PatternExpander $expander,... A PatternExpander's instances.
|
||||
*/
|
||||
public function __construct($fieldName, $expander)
|
||||
{
|
||||
$this->fieldName = $fieldName;
|
||||
$this->expander = $expander;
|
||||
|
||||
if ($expander instanceof PatternExpander) {
|
||||
$expander = func_get_args();
|
||||
$length = count($expander);
|
||||
// Process all except first argument which contains field name.
|
||||
$this->expander = [];
|
||||
for ($i = 1; $i < $length; ++$i) {
|
||||
if (!$expander[$i] instanceof PatternExpander) {
|
||||
throw new \InvalidArgumentException('Has invalid expander.');
|
||||
}
|
||||
|
||||
$this->expander[] = $expander[$i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
if (! is_array($value) && !isset($value[$this->fieldName])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_array($this->expander)) {
|
||||
// Match all expanders.
|
||||
foreach ($this->expander as $expander) {
|
||||
if (! $expander->match($value[$this->fieldName])) {
|
||||
$this->error = "Field {$this->fieldName}: expander don't matches value. ".
|
||||
$expander->getError();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// All expanders successfully matches.
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($value[$this->fieldName] !== $this->expander) {
|
||||
$this->error = "Field {$this->fieldName}: don't equal to {$this->expander}";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Common\Util\Converter\DateConverter;
|
||||
|
||||
/**
|
||||
* Class GteExpander
|
||||
* Check that value is greater or equal to another value.
|
||||
* Except integers, float and datetime values.
|
||||
*
|
||||
* Example:
|
||||
* - integer.gte(10)
|
||||
* - date.gte('2017-10-01')
|
||||
* - .field('date', gte('2017-10-01'))
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class GteExpander extends AbstractExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @var integer|string|\DateTimeInterface|float
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @param integer|string|\DateTimeInterface|float $value Expected value.
|
||||
*/
|
||||
public function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
if (! is_numeric($value) && ! is_int($value) && ! is_float($value)
|
||||
&& ! is_string($value)) {
|
||||
$this->error = 'Can match only integers, float and datetime values';
|
||||
return false;
|
||||
}
|
||||
|
||||
$bound = $this->value;
|
||||
if (DateConverter::can($value)) {
|
||||
// For string which represent date try to convert it into \DateTime
|
||||
// instances.
|
||||
try {
|
||||
$value = DateConverter::convert($value);
|
||||
$bound = DateConverter::convert($bound);
|
||||
|
||||
$bound->setTimezone($value->getTimezone());
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// For scalar types convert all values to the same type.
|
||||
$type = gettype($bound);
|
||||
settype($value, $type);
|
||||
}
|
||||
|
||||
if (! ($matched = $value >= $bound)) {
|
||||
if ($value instanceof \DateTime) {
|
||||
$value = $value->format('c');
|
||||
$bound = $bound->format('c');
|
||||
}
|
||||
|
||||
$this->error = "Checked value {$value} less than {$bound}.";
|
||||
}
|
||||
|
||||
return $matched;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
|
||||
|
||||
/**
|
||||
* Class LengthExpander
|
||||
* Check that expanded array contains specified number of elements.
|
||||
*
|
||||
* Example: array.length(2)
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class LengthExpander extends AbstractExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
private $count;
|
||||
|
||||
/**
|
||||
* @param PatternExpander|integer $count Expected number of elements or
|
||||
* appropriate pattern expander.
|
||||
*/
|
||||
public function __construct($count)
|
||||
{
|
||||
if (! $count instanceof PatternExpander) {
|
||||
$count = (int) $count;
|
||||
}
|
||||
$this->count = $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
if (! is_array($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->count instanceof PatternExpander) {
|
||||
return $this->count->match(count($value));
|
||||
}
|
||||
|
||||
return count($value) === $this->count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
|
||||
|
||||
/**
|
||||
* Class FieldExpander
|
||||
* Check that inner expander not match.
|
||||
*
|
||||
* Example: .not(contains('some'))
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class NotExpander extends AbstractExpander
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $expander;
|
||||
|
||||
/**
|
||||
* @param PatternExpander $expander A PatternExpander instance.
|
||||
*/
|
||||
public function __construct(PatternExpander $expander)
|
||||
{
|
||||
$this->expander = $expander;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
if ($this->expander->match($value)) {
|
||||
$this->error = 'Expander match this value.';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Coduo\ToString\StringConverter;
|
||||
|
||||
/**
|
||||
* Class OneExpander
|
||||
* Check that only one array element matches specified expander.
|
||||
*
|
||||
* Example:
|
||||
* - array.one(field('property', value))
|
||||
* - array.one(entity('AppBundle:User'), field('id', 1))
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class OneExpander extends AbstractChainExpander
|
||||
{
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
if (! is_array($value)) {
|
||||
$this->error = 'One expander require "array", got '.
|
||||
new StringConverter($value) .'.';
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($value as $row) {
|
||||
if (parent::match($row)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
use Coduo\ToString\StringConverter;
|
||||
|
||||
/**
|
||||
* Class SomeExpander
|
||||
* Check that some array elements matches specified expander.
|
||||
*
|
||||
* Example:
|
||||
* - array.some(field('property', value))
|
||||
* - array.some(entity('AppBundle:User'), field('id', 1))
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class SomeExpander extends AbstractChainExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
if (! is_array($value)) {
|
||||
$this->error = 'Some expander require "array", got '.
|
||||
new StringConverter($value) .'.';
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($value as $row) {
|
||||
if (parent::match($row)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$this->error = 'No element does not match specified expanders';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Util\Matcher\Expander;
|
||||
|
||||
/**
|
||||
* Class TypeExpander
|
||||
* Check that value has specified type.
|
||||
*
|
||||
* Example:
|
||||
* - wildcart.oneOf(isEmpty(), type('double'))
|
||||
* - wildcart.type('string')
|
||||
*
|
||||
* @package Common\Util\Matcher\Expander
|
||||
*/
|
||||
class TypeExpander extends AbstractExpander
|
||||
{
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
private $type;
|
||||
|
||||
/**
|
||||
* @param string $type Expected value type.
|
||||
*/
|
||||
public function __construct($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Value to match.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function match($value)
|
||||
{
|
||||
$valueType = gettype($value);
|
||||
|
||||
if ($valueType === 'float') {
|
||||
$valueType = 'double';
|
||||
}
|
||||
|
||||
return $valueType === $this->type;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user