diff options
Diffstat (limited to 'lib/private/DB/QueryBuilder/CompositeExpression.php')
-rw-r--r-- | lib/private/DB/QueryBuilder/CompositeExpression.php | 75 |
1 files changed, 37 insertions, 38 deletions
diff --git a/lib/private/DB/QueryBuilder/CompositeExpression.php b/lib/private/DB/QueryBuilder/CompositeExpression.php index 512343662ad..122998036ae 100644 --- a/lib/private/DB/QueryBuilder/CompositeExpression.php +++ b/lib/private/DB/QueryBuilder/CompositeExpression.php @@ -1,24 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\DB\QueryBuilder; @@ -26,16 +11,13 @@ namespace OC\DB\QueryBuilder; use OCP\DB\QueryBuilder\ICompositeExpression; class CompositeExpression implements ICompositeExpression, \Countable { - /** @var \Doctrine\DBAL\Query\Expression\CompositeExpression */ - protected $compositeExpression; + public const TYPE_AND = 'AND'; + public const TYPE_OR = 'OR'; - /** - * Constructor. - * - * @param \Doctrine\DBAL\Query\Expression\CompositeExpression $compositeExpression - */ - public function __construct(\Doctrine\DBAL\Query\Expression\CompositeExpression $compositeExpression) { - $this->compositeExpression = $compositeExpression; + public function __construct( + private string $type, + private array $parts = [], + ) { } /** @@ -45,8 +27,10 @@ class CompositeExpression implements ICompositeExpression, \Countable { * * @return \OCP\DB\QueryBuilder\ICompositeExpression */ - public function addMultiple(array $parts = []) { - $this->compositeExpression->addMultiple($parts); + public function addMultiple(array $parts = []): ICompositeExpression { + foreach ($parts as $part) { + $this->add($part); + } return $this; } @@ -58,8 +42,16 @@ class CompositeExpression implements ICompositeExpression, \Countable { * * @return \OCP\DB\QueryBuilder\ICompositeExpression */ - public function add($part) { - $this->compositeExpression->add($part); + public function add($part): ICompositeExpression { + if ($part === null) { + return $this; + } + + if ($part instanceof self && count($part) === 0) { + return $this; + } + + $this->parts[] = $part; return $this; } @@ -69,8 +61,8 @@ class CompositeExpression implements ICompositeExpression, \Countable { * * @return integer */ - public function count() { - return $this->compositeExpression->count(); + public function count(): int { + return count($this->parts); } /** @@ -78,8 +70,8 @@ class CompositeExpression implements ICompositeExpression, \Countable { * * @return string */ - public function getType() { - return $this->compositeExpression->getType(); + public function getType(): string { + return $this->type; } /** @@ -87,7 +79,14 @@ class CompositeExpression implements ICompositeExpression, \Countable { * * @return string */ - public function __toString() { - return (string) $this->compositeExpression; + public function __toString(): string { + if ($this->count() === 1) { + return (string)$this->parts[0]; + } + return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; + } + + public function getParts(): array { + return $this->parts; } } |