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