Exception
1 day ago
SpecificationFactory
1 day ago
AggregateSpecification.php
1 day ago
AndSpecification.php
1 day ago
CollectionOperators.php
1 day ago
CollectionSpecification.php
1 day ago
ComparisonOperators.php
1 day ago
ComparisonSpecification.php
1 day ago
CompositeSpecification.php
1 day ago
ContainsSpecification.php
1 day ago
Context.php
1 day ago
ContextAwareSpecification.php
1 day ago
DateComparisonSpecification.php
1 day ago
DateOperators.php
1 day ago
DateRangeSpecification.php
1 day ago
DateRelativeDaysSpecification.php
1 day ago
DateRelativeDeductedSpecification.php
1 day ago
DateSpecification.php
1 day ago
DateTimeFactory.php
1 day ago
DateTrait.php
1 day ago
EndsWithSpecification.php
1 day ago
FactSpecification.php
1 day ago
FactTrait.php
1 day ago
FloatComparisonSpecification.php
1 day ago
FloatRangeSpecification.php
1 day ago
IntegerComparisonSpecification.php
1 day ago
IntegerRangeSpecification.php
1 day ago
NotSpecification.php
1 day ago
NullSpecification.php
1 day ago
OperatorExpression.php
1 day ago
OrSpecification.php
1 day ago
RangeOperators.php
1 day ago
RangeSpecification.php
1 day ago
Specification.php
1 day ago
SpecificationFactory.php
1 day ago
StartsWithSpecification.php
1 day ago
StringComparisonSpecification.php
1 day ago
StringMatchSpecification.php
1 day ago
StringOperators.php
1 day ago
Type.php
1 day ago
TypeSpecification.php
1 day ago
TypeTrait.php
1 day ago
Types.php
1 day ago
RangeSpecification.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Expression; |
| 6 | |
| 7 | use AC\Expression\Exception\OperatorNotFoundException; |
| 8 | |
| 9 | class RangeSpecification extends OperatorExpression |
| 10 | { |
| 11 | public const A = 'a'; |
| 12 | public const B = 'b'; |
| 13 | |
| 14 | /** |
| 15 | * @var mixed |
| 16 | */ |
| 17 | protected $a; |
| 18 | |
| 19 | /** |
| 20 | * @var mixed |
| 21 | */ |
| 22 | protected $b; |
| 23 | |
| 24 | public function __construct(string $operator, $a, $b) |
| 25 | { |
| 26 | parent::__construct($operator); |
| 27 | |
| 28 | $this->a = $a; |
| 29 | $this->b = $b; |
| 30 | } |
| 31 | |
| 32 | public function is_satisfied_by($value): bool |
| 33 | { |
| 34 | switch ($this->operator) { |
| 35 | case RangeOperators::BETWEEN_EXCLUSIVE: |
| 36 | case RangeOperators::NOT_BETWEEN_EXCLUSIVE: |
| 37 | case RangeOperators::BETWEEN: |
| 38 | case RangeOperators::NOT_BETWEEN: |
| 39 | $operator_a = ComparisonOperators::GREATER_THAN_EQUAL; |
| 40 | $operator_b = ComparisonOperators::LESS_THAN_EQUAL; |
| 41 | |
| 42 | $exclusive = [ |
| 43 | RangeOperators::BETWEEN_EXCLUSIVE, |
| 44 | RangeOperators::NOT_BETWEEN_EXCLUSIVE, |
| 45 | ]; |
| 46 | |
| 47 | if (in_array($this->operator, $exclusive, true)) { |
| 48 | $operator_a = ComparisonOperators::GREATER_THAN; |
| 49 | $operator_b = ComparisonOperators::LESS_THAN; |
| 50 | } |
| 51 | |
| 52 | $specification = new AndSpecification([ |
| 53 | new ComparisonSpecification($operator_a, $this->a), |
| 54 | new ComparisonSpecification($operator_b, $this->b), |
| 55 | ]); |
| 56 | |
| 57 | $not = [ |
| 58 | RangeOperators::NOT_BETWEEN, |
| 59 | RangeOperators::NOT_BETWEEN_EXCLUSIVE, |
| 60 | ]; |
| 61 | |
| 62 | if (in_array($this->operator, $not, true)) { |
| 63 | $specification = $specification->not(); |
| 64 | } |
| 65 | |
| 66 | return $specification->is_satisfied_by($value); |
| 67 | } |
| 68 | |
| 69 | throw new OperatorNotFoundException($this->operator); |
| 70 | } |
| 71 | |
| 72 | public function export(): array |
| 73 | { |
| 74 | return array_merge([ |
| 75 | self::A => $this->a, |
| 76 | self::B => $this->b, |
| 77 | ], parent::export()); |
| 78 | } |
| 79 | |
| 80 | } |
| 81 |