ExistsCondition::getQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\QueryBuilder\Condition;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
use Yiisoft\Db\QueryBuilder\Condition\Interface\ExistConditionInterface;
9
use Yiisoft\Db\Query\QueryInterface;
10
11
/**
12
 * Condition that represents `EXISTS` operator.
13
 */
14
final class ExistsCondition implements ExistConditionInterface
15
{
16
    public function __construct(private string $operator, private QueryInterface $query)
17
    {
18
    }
19
20
    public function getOperator(): string
21
    {
22
        return $this->operator;
23
    }
24
25
    public function getQuery(): QueryInterface
26
    {
27
        return $this->query;
28
    }
29
30
    /**
31
     * Creates a condition based on the given operator and operands.
32
     *
33
     * @throws InvalidArgumentException If the number of operands isn't 1, and the first operand isn't a query object.
34
     */
35
    public static function fromArrayDefinition(string $operator, array $operands): self
36
    {
37
        if (isset($operands[0]) && $operands[0] instanceof QueryInterface) {
38
            return new self($operator, $operands[0]);
39
        }
40
41
        throw new InvalidArgumentException('Sub query for EXISTS operator must be a Query object.');
42
    }
43
}
44