CompositeExpression.php
73 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\DBAL\Query\Expression; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use Countable; |
| 5 | use MailPoetVendor\Doctrine\Deprecations\Deprecation; |
| 6 | use ReturnTypeWillChange; |
| 7 | use function array_merge; |
| 8 | use function count; |
| 9 | use function implode; |
| 10 | class CompositeExpression implements Countable |
| 11 | { |
| 12 | public const TYPE_AND = 'AND'; |
| 13 | public const TYPE_OR = 'OR'; |
| 14 | private $type; |
| 15 | private array $parts = []; |
| 16 | public function __construct($type, array $parts = []) |
| 17 | { |
| 18 | $this->type = $type; |
| 19 | $this->addMultiple($parts); |
| 20 | Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/pull/3864', 'Do not use CompositeExpression constructor directly, use static and() and or() factory methods.'); |
| 21 | } |
| 22 | public static function and($part, ...$parts) : self |
| 23 | { |
| 24 | return new self(self::TYPE_AND, array_merge([$part], $parts)); |
| 25 | } |
| 26 | public static function or($part, ...$parts) : self |
| 27 | { |
| 28 | return new self(self::TYPE_OR, array_merge([$part], $parts)); |
| 29 | } |
| 30 | public function addMultiple(array $parts = []) |
| 31 | { |
| 32 | Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/3844', 'CompositeExpression::addMultiple() is deprecated, use CompositeExpression::with() instead.'); |
| 33 | foreach ($parts as $part) { |
| 34 | $this->add($part); |
| 35 | } |
| 36 | return $this; |
| 37 | } |
| 38 | public function add($part) |
| 39 | { |
| 40 | Deprecation::triggerIfCalledFromOutside('doctrine/dbal', 'https://github.com/doctrine/dbal/issues/3844', 'CompositeExpression::add() is deprecated, use CompositeExpression::with() instead.'); |
| 41 | if ($part === null) { |
| 42 | return $this; |
| 43 | } |
| 44 | if ($part instanceof self && count($part) === 0) { |
| 45 | return $this; |
| 46 | } |
| 47 | $this->parts[] = $part; |
| 48 | return $this; |
| 49 | } |
| 50 | public function with($part, ...$parts) : self |
| 51 | { |
| 52 | $that = clone $this; |
| 53 | $that->parts = array_merge($that->parts, [$part], $parts); |
| 54 | return $that; |
| 55 | } |
| 56 | #[\ReturnTypeWillChange] |
| 57 | public function count() |
| 58 | { |
| 59 | return count($this->parts); |
| 60 | } |
| 61 | public function __toString() |
| 62 | { |
| 63 | if ($this->count() === 1) { |
| 64 | return (string) $this->parts[0]; |
| 65 | } |
| 66 | return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; |
| 67 | } |
| 68 | public function getType() |
| 69 | { |
| 70 | return $this->type; |
| 71 | } |
| 72 | } |
| 73 |