ea87c2f3ccfb65773841bf5e70516046e978f052
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Database / Query / Expression / CompositeExpression.php
1 <?php
2 declare (strict_types = 1);
3 namespace TYPO3\CMS\Core\Database\Query\Expression;
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
18 /**
19 * Facade of the Doctrine DBAL CompositeExpression to have
20 * all Query related classes with in TYPO3\CMS namespace.
21 */
22 class CompositeExpression extends \Doctrine\DBAL\Query\Expression\CompositeExpression
23 {
24 /**
25 * Retrieves the string representation of this composite expression.
26 * If expression is empty, just return an empty string.
27 * Native Doctrine expression would return () instead.
28 *
29 * @return string
30 */
31 public function __toString()
32 {
33 if ($this->count() === 0) {
34 return '';
35 }
36 return parent::__toString();
37 }
38
39 /**
40 * Adds an expression to composite expression.
41 *
42 * @param mixed $part
43 *
44 * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
45 */
46 public function add($part)
47 {
48 // Due to a bug in Doctrine DBAL, we must add our own check here,
49 // which we luckily can, as we use a subclass anyway.
50 // @see https://github.com/doctrine/dbal/issues/2388
51 $isEmpty = $part instanceof self ? $part->count() === 0 : empty($part);
52 if (!$isEmpty) {
53 parent::add($part);
54 }
55
56 return $this;
57 }
58 }