|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * Copyright © Thomas Klein, All rights reserved. |
| 4 | + * See LICENSE bundled with this library for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace LogicTree\Formatter; |
| 8 | + |
| 9 | +use LogicTree\DataSource; |
| 10 | +use LogicTree\Formatter\Natural\Comparator\CompareListConverter; |
| 11 | +use LogicTree\Formatter\Natural\Comparator\CompareOneConverter; |
| 12 | +use LogicTree\Formatter\Natural\Comparator\CompareTwoConverter; |
| 13 | +use LogicTree\Formatter\Natural\Logical\LogicalOperatorConverter; |
| 14 | +use LogicTree\Node\CombineInterface; |
| 15 | +use LogicTree\Node\ConditionInterface; |
| 16 | +use LogicTree\Node\NodeInterface; |
| 17 | + |
| 18 | +use function array_map; |
| 19 | + |
| 20 | +final class NaturalFormatter implements FormatterInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var ConverterInterface[] |
| 24 | + */ |
| 25 | + private array $converters = []; |
| 26 | + |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + $this->register('empty', new CompareOneConverter('IS EMPTY')); |
| 30 | + $this->register('eq', new CompareTwoConverter('==')); |
| 31 | + $this->register('gteq', new CompareTwoConverter('>=')); |
| 32 | + $this->register('gt', new CompareTwoConverter('>')); |
| 33 | + $this->register('iden', new CompareTwoConverter('===')); |
| 34 | + $this->register('iniden', new CompareTwoConverter('IN STRICT')); |
| 35 | + $this->register('in', new CompareTwoConverter('IN')); |
| 36 | + $this->register('lteq', new CompareTwoConverter('<=')); |
| 37 | + $this->register('lt', new CompareTwoConverter('<')); |
| 38 | + $this->register('neq', new CompareTwoConverter('!=')); |
| 39 | + $this->register('niden', new CompareTwoConverter('!==')); |
| 40 | + $this->register('niniden', new CompareTwoConverter('NOT IN STRICT')); |
| 41 | + $this->register('nin', new CompareListConverter('NOT IN')); |
| 42 | + $this->register('notnull', new CompareOneConverter('IS NOT NULL')); |
| 43 | + $this->register('null', new CompareOneConverter('IS NULL')); |
| 44 | + $this->register('regexp', new CompareTwoConverter('MATCH REGEX')); |
| 45 | + $this->register('and', new LogicalOperatorConverter('AND')); |
| 46 | + $this->register('nand', new LogicalOperatorConverter('NAND')); |
| 47 | + $this->register('nor', new LogicalOperatorConverter('NOR')); |
| 48 | + $this->register('or', new LogicalOperatorConverter('OR')); |
| 49 | + $this->register('xnor', new LogicalOperatorConverter('XNOR')); |
| 50 | + $this->register('xor', new LogicalOperatorConverter('XOR')); |
| 51 | + } |
| 52 | + |
| 53 | + public function register(string $operator, ConverterInterface $converter): void |
| 54 | + { |
| 55 | + $this->converters[$operator] = $converter; |
| 56 | + } |
| 57 | + |
| 58 | + public function format(NodeInterface $node, DataSource $dataSource): string |
| 59 | + { |
| 60 | + return match (true) { |
| 61 | + $node instanceof CombineInterface => $this->formatCombine($node, $dataSource), |
| 62 | + $node instanceof ConditionInterface => $this->formatCondition($node, $dataSource), |
| 63 | + default => throw new \Exception()//ToDo |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + private function formatCombine(CombineInterface $combine, DataSource $dataSource): string |
| 68 | + { |
| 69 | + return ($combine->isInvert() ? 'NOT ' : '') . $this->converters[$combine->getOperator()]->convert( |
| 70 | + ...array_map(fn (NodeInterface $node) => $this->format($node, $dataSource), $combine->getChildren()) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private function formatCondition(ConditionInterface $condition, DataSource $dataSource): string |
| 75 | + {//ToDo exception if not exists |
| 76 | + return $this->converters[$condition->getOperator()]->convert( |
| 77 | + $dataSource->getValue($condition->getValueIdentifier()), |
| 78 | + $condition->getValueCompare() |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
0 commit comments