2015-07-06 10:00:01 +00:00
|
|
|
<?php
|
2024-05-23 07:26:56 +00:00
|
|
|
|
2015-07-06 10:00:01 +00:00
|
|
|
/**
|
2024-05-23 07:26:56 +00:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-07-06 10:00:01 +00:00
|
|
|
*/
|
2024-07-05 13:03:50 +00:00
|
|
|
|
2015-07-07 15:30:26 +00:00
|
|
|
namespace OC\DB\QueryBuilder;
|
2015-07-06 10:00:01 +00:00
|
|
|
|
2015-07-07 15:30:26 +00:00
|
|
|
use OCP\DB\QueryBuilder\ICompositeExpression;
|
2015-07-06 10:00:01 +00:00
|
|
|
|
|
|
|
class CompositeExpression implements ICompositeExpression, \Countable {
|
2024-07-05 13:03:50 +00:00
|
|
|
public const TYPE_AND = 'AND';
|
|
|
|
public const TYPE_OR = 'OR';
|
2015-07-06 10:00:01 +00:00
|
|
|
|
2024-07-05 13:03:50 +00:00
|
|
|
public function __construct(
|
|
|
|
private string $type,
|
2024-10-19 02:46:25 +00:00
|
|
|
private array $parts = [],
|
2024-07-05 13:03:50 +00:00
|
|
|
) {
|
2015-07-06 10:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds multiple parts to composite expression.
|
|
|
|
*
|
|
|
|
* @param array $parts
|
|
|
|
*
|
2015-07-07 15:30:26 +00:00
|
|
|
* @return \OCP\DB\QueryBuilder\ICompositeExpression
|
2015-07-06 10:00:01 +00:00
|
|
|
*/
|
2021-03-04 20:47:44 +00:00
|
|
|
public function addMultiple(array $parts = []): ICompositeExpression {
|
2024-07-05 13:03:50 +00:00
|
|
|
foreach ($parts as $part) {
|
|
|
|
$this->add($part);
|
|
|
|
}
|
2015-07-06 10:00:01 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an expression to composite expression.
|
|
|
|
*
|
|
|
|
* @param mixed $part
|
|
|
|
*
|
2015-07-07 15:30:26 +00:00
|
|
|
* @return \OCP\DB\QueryBuilder\ICompositeExpression
|
2015-07-06 10:00:01 +00:00
|
|
|
*/
|
2021-03-04 20:47:44 +00:00
|
|
|
public function add($part): ICompositeExpression {
|
2024-07-05 13:03:50 +00:00
|
|
|
if ($part === null) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($part instanceof self && count($part) === 0) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->parts[] = $part;
|
2015-07-06 10:00:01 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the amount of expressions on composite expression.
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
2021-03-04 20:47:44 +00:00
|
|
|
public function count(): int {
|
2024-07-05 13:03:50 +00:00
|
|
|
return count($this->parts);
|
2015-07-06 10:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the type of this composite expression (AND/OR).
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-03-04 20:47:44 +00:00
|
|
|
public function getType(): string {
|
2024-07-05 13:03:50 +00:00
|
|
|
return $this->type;
|
2015-07-06 10:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the string representation of this composite expression.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-03-04 20:47:44 +00:00
|
|
|
public function __toString(): string {
|
2024-07-05 13:03:50 +00:00
|
|
|
if ($this->count() === 1) {
|
|
|
|
return (string)$this->parts[0];
|
|
|
|
}
|
|
|
|
return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getParts(): array {
|
|
|
|
return $this->parts;
|
2015-07-06 10:00:01 +00:00
|
|
|
}
|
|
|
|
}
|