Passed
Push — master ( c65027...a39d50 )
by Wilmer
18:53 queued 16:02
created

NotCondition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Expression\ExpressionInterface;
9
use Yiisoft\Db\QueryBuilder\Condition\Interface\NotConditionInterface;
10
11
use function array_shift;
12
use function count;
13
14
/**
15
 * Condition that inverts passed {@see condition}.
16
 */
17
final class NotCondition implements NotConditionInterface
18
{
19
    public function __construct(private ExpressionInterface|array|null|string $condition)
20
    {
21
    }
22
23
    public function getCondition(): ExpressionInterface|array|null|string
24
    {
25
        return $this->condition;
26
    }
27
28
    /**
29
     * @throws InvalidArgumentException
30
     */
31
    public static function fromArrayDefinition(string $operator, array $operands): self
32
    {
33
        return new self(self::validateCondition($operator, $operands));
34
    }
35
36
    private static function validateCondition(string $operator, array $condition): ExpressionInterface|array|null|string
37
    {
38
        if (count($condition) !== 1) {
39
            throw new InvalidArgumentException("Operator '$operator' requires exactly one operand.");
40
        }
41
42
        /** @var mixed $condition */
43
        $condition = array_shift($condition);
44
45
        if (
46
            !is_array($condition) &&
47
            !($condition instanceof ExpressionInterface) &&
48
            !is_string($condition) &&
49
            $condition !== null
50
        ) {
51
            throw new InvalidArgumentException(
52
                "Operator '$operator' requires condition to be array, string, null or ExpressionInterface."
53
            );
54
        }
55
56
        return $condition;
57
    }
58
}
59