0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-01-29 22:14:24 +00:00
nextcloud_server/lib/private/DB/QueryBuilder/CompositeExpression.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
2015-07-07 15:30:26 +00:00
namespace OC\DB\QueryBuilder;
2015-07-07 15:30:26 +00:00
use OCP\DB\QueryBuilder\ICompositeExpression;
class CompositeExpression implements ICompositeExpression, \Countable {
public const TYPE_AND = 'AND';
public const TYPE_OR = 'OR';
public function __construct(
private string $type,
private array $parts = [],
) {
}
/**
* Adds multiple parts to composite expression.
*
* @param array $parts
*
2015-07-07 15:30:26 +00:00
* @return \OCP\DB\QueryBuilder\ICompositeExpression
*/
public function addMultiple(array $parts = []): ICompositeExpression {
foreach ($parts as $part) {
$this->add($part);
}
return $this;
}
/**
* Adds an expression to composite expression.
*
* @param mixed $part
*
2015-07-07 15:30:26 +00:00
* @return \OCP\DB\QueryBuilder\ICompositeExpression
*/
public function add($part): ICompositeExpression {
if ($part === null) {
return $this;
}
if ($part instanceof self && count($part) === 0) {
return $this;
}
$this->parts[] = $part;
return $this;
}
/**
* Retrieves the amount of expressions on composite expression.
*
* @return integer
*/
public function count(): int {
return count($this->parts);
}
/**
* Returns the type of this composite expression (AND/OR).
*
* @return string
*/
public function getType(): string {
return $this->type;
}
/**
* Retrieves the string representation of this composite expression.
*
* @return string
*/
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;
}
}