AggregateFactory.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Expression\SpecificationFactory; |
| 6 | |
| 7 | use AC\Expression\AggregateSpecification; |
| 8 | use AC\Expression\AndSpecification; |
| 9 | use AC\Expression\CollectionSpecification; |
| 10 | use AC\Expression\ContainsSpecification; |
| 11 | use AC\Expression\DateComparisonSpecification; |
| 12 | use AC\Expression\DateRangeSpecification; |
| 13 | use AC\Expression\DateRelativeDaysSpecification; |
| 14 | use AC\Expression\DateRelativeDeductedSpecification; |
| 15 | use AC\Expression\DateSpecification; |
| 16 | use AC\Expression\EndsWithSpecification; |
| 17 | use AC\Expression\Exception\InvalidDateFormatException; |
| 18 | use AC\Expression\Exception\SpecificationNotFoundException; |
| 19 | use AC\Expression\FactSpecification; |
| 20 | use AC\Expression\FloatComparisonSpecification; |
| 21 | use AC\Expression\FloatRangeSpecification; |
| 22 | use AC\Expression\IntegerComparisonSpecification; |
| 23 | use AC\Expression\IntegerRangeSpecification; |
| 24 | use AC\Expression\OperatorExpression; |
| 25 | use AC\Expression\OrSpecification; |
| 26 | use AC\Expression\RangeSpecification; |
| 27 | use AC\Expression\Specification; |
| 28 | use AC\Expression\SpecificationFactory; |
| 29 | use AC\Expression\StartsWithSpecification; |
| 30 | use InvalidArgumentException; |
| 31 | |
| 32 | final class AggregateFactory implements SpecificationFactory |
| 33 | { |
| 34 | |
| 35 | /** |
| 36 | * @throws InvalidDateFormatException |
| 37 | */ |
| 38 | public function create(array $rule): Specification |
| 39 | { |
| 40 | if ( ! ($rule[Specification::SPECIFICATION] ?? null)) { |
| 41 | throw new InvalidArgumentException('Missing specification.'); |
| 42 | } |
| 43 | |
| 44 | $specification = $rule[Specification::SPECIFICATION]; |
| 45 | $operator = $rule[OperatorExpression::OPERATOR] ?? null; |
| 46 | $fact = $rule[FactSpecification::FACT] ?? null; |
| 47 | $format = $rule[DateSpecification::FORMAT] ?? null; |
| 48 | $timezone = $rule[DateSpecification::TIMEZONE] ?? null; |
| 49 | $a = $rule[RangeSpecification::A] ?? null; |
| 50 | $b = $rule[RangeSpecification::B] ?? null; |
| 51 | |
| 52 | switch ($specification) { |
| 53 | case 'starts_with': |
| 54 | return new StartsWithSpecification($fact); |
| 55 | case 'ends_with': |
| 56 | return new EndsWithSpecification($fact); |
| 57 | case 'contains': |
| 58 | return new ContainsSpecification($fact); |
| 59 | case 'not_contains': |
| 60 | return (new ContainsSpecification($fact))->not(); |
| 61 | case 'float_comparison': |
| 62 | return new FloatComparisonSpecification($operator, (string)$fact); |
| 63 | case 'integer_comparison': |
| 64 | return new IntegerComparisonSpecification($operator, (int)$fact); |
| 65 | case 'date_comparison': |
| 66 | return new DateComparisonSpecification($operator, (string)$fact, $format, $timezone); |
| 67 | case 'date_relative_days': |
| 68 | return new DateRelativeDaysSpecification($operator, (int)$fact, $format, $timezone); |
| 69 | case 'date_relative_deducted': |
| 70 | return new DateRelativeDeductedSpecification($operator, $format, $timezone); |
| 71 | case 'range_float': |
| 72 | return new FloatRangeSpecification($operator, (float)$a, (float)$b); |
| 73 | case 'range_integer': |
| 74 | return new IntegerRangeSpecification($operator, (int)$a, (int)$b); |
| 75 | case 'range_date_time': |
| 76 | return new DateRangeSpecification($operator, (string)$a, (string)$b, $format, $timezone); |
| 77 | case 'collection': |
| 78 | return new CollectionSpecification($operator, $fact); |
| 79 | case 'and': |
| 80 | case 'or': |
| 81 | $rules = []; |
| 82 | |
| 83 | foreach ($rule[AggregateSpecification::RULES] as $aggregate_rule) { |
| 84 | $rules[] = $this->create($aggregate_rule); |
| 85 | } |
| 86 | |
| 87 | return $specification === 'and' |
| 88 | ? new AndSpecification($rules) |
| 89 | : new OrSpecification($rules); |
| 90 | } |
| 91 | |
| 92 | throw new SpecificationNotFoundException($specification); |
| 93 | } |
| 94 | |
| 95 | } |