‪TYPO3CMS  ‪main
CompositeExpression.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Query\Expression\CompositeExpression as DoctrineCompositeExpression;
21 
26 class ‪CompositeExpression extends DoctrineCompositeExpression
27 {
33  private array ‪$parts = [];
34 
38  private string ‪$type;
39 
45  public function ‪__construct(‪$type, array ‪$parts = [])
46  {
47  // Pass empty parts to parent constructor as we have borrowed nearly all method to this level because of their
48  // private visibility nature.
49  parent::__construct((string)‪$type, []);
50  $this->type = (string)‪$type;
51  if (‪$parts !== []) {
52  // doctrine/dbal solved the issue to avoid empty parts by making it mandatory to avoid instantiating this
53  // class without a part. As we allow this and handle empty parts later on, we apply the empty check here.
54  // @see https://github.com/doctrine/dbal/issues/2388
55  array_filter(‪$parts, static fn(‪CompositeExpression|DoctrineCompositeExpression|string $value): bool => !(($value instanceof DoctrineCompositeExpression) ? $value->count() === 0 : empty($value)));
56  }
57  $this->parts = ‪$parts;
58  }
59 
64  public static function ‪and($part = null, ...‪$parts): self
65  {
66  $mergedParts = array_merge([$part], ‪$parts);
67  array_filter($mergedParts, static fn(‪CompositeExpression|DoctrineCompositeExpression|string|null $value): bool => !(($value instanceof DoctrineCompositeExpression) ? $value->count() === 0 : empty($value)));
68  return (new self(self::TYPE_AND, []))->with(...$mergedParts);
69  }
70 
75  public static function ‪or($part = null, ...‪$parts): self
76  {
77  $mergedParts = array_merge([$part], ‪$parts);
78  array_filter($mergedParts, static fn(‪CompositeExpression|DoctrineCompositeExpression|string|null $value): bool => !(($value instanceof DoctrineCompositeExpression) ? $value->count() === 0 : empty($value)));
79  return (new self(self::TYPE_OR, []))->with(...$mergedParts);
80  }
81 
88  public function ‪with($part = null, ...‪$parts): self
89  {
90  $mergedParts = array_merge([$part], ‪$parts);
91  $that = clone $this;
92  foreach ($mergedParts as $singlePart) {
93  // Due to a bug in Doctrine DBAL, we must add our own check here,
94  // which we luckily can, as we use a subclass anyway.
95  // @see https://github.com/doctrine/dbal/issues/2388
96  $isEmpty = (($singlePart instanceof DoctrineCompositeExpression) ? $singlePart->count() === 0 : empty($singlePart));
97  if (!$isEmpty) {
98  $that->parts[] = $singlePart;
99  }
100  }
101 
102  return $that;
103  }
104 
108  public function ‪count(): int
109  {
110  return ‪count($this->parts);
111  }
112 
116  public function ‪getType(): string
117  {
118  return ‪$this->type;
119  }
120 
126  public function ‪__toString(): string
127  {
128  if ($this->‪count() === 0) {
129  return '';
130  }
131  if ($this->‪count() === 1) {
132  return (string)$this->parts[0];
133  }
134  return '((' . implode(') ' . $this->type . ' (', $this->parts) . '))';
135  }
136 }
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\or
‪static or($part=null,... $parts)
Definition: CompositeExpression.php:75
‪TYPO3\CMS\Core\Database\Query\Expression
Definition: CompositeExpression.php:18
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\__toString
‪__toString()
Definition: CompositeExpression.php:126
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\and
‪static and($part=null,... $parts)
Definition: CompositeExpression.php:64
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\__construct
‪__construct($type, array $parts=[])
Definition: CompositeExpression.php:45
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression
Definition: CompositeExpression.php:27
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\$parts
‪array $parts
Definition: CompositeExpression.php:33
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\count
‪count()
Definition: CompositeExpression.php:108
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\with
‪with($part=null,... $parts)
Definition: CompositeExpression.php:88
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\$type
‪string $type
Definition: CompositeExpression.php:38
‪TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression\getType
‪getType()
Definition: CompositeExpression.php:116