Completed
Push — master ( ed4ac2...97e0c2 )
by Sergei
96:38 queued 31:39
created

CompositeExpression::or()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Query\Expression;
6
7
use Countable;
8
use function array_filter;
9
use function array_merge;
10
use function array_values;
11
use function count;
12
use function implode;
13
14
/**
15
 * Composite expression is responsible to build a group of similar expression.
16
 *
17
 * This class is immutable.
18
 */
19
class CompositeExpression implements Countable
20
{
21
    /**
22
     * Constant that represents an AND composite expression.
23
     */
24
    public const TYPE_AND = 'AND';
25
26
    /**
27
     * Constant that represents an OR composite expression.
28
     */
29
    public const TYPE_OR = 'OR';
30
31
    /**
32
     * The instance type of composite expression.
33
     *
34
     * @var string
35
     */
36
    private $type;
37
38
    /**
39
     * Each expression part of the composite expression.
40
     *
41
     * @var self[]|string[]
42
     */
43
    private $parts = [];
44 891
45
    /**
46 891
     * @internal Use the and() / or() factory methods.
47
     *
48 891
     * @param string          $type  Instance type of composite expression.
49 891
     * @param self[]|string[] $parts Composition of expressions to be joined on composite expression.
50
     */
51
    public function __construct(string $type, array $parts = [])
52
    {
53
        $this->type  = $type;
54
        $this->parts = array_values(array_filter($parts, static function ($part) {
55
            return ! ($part instanceof self && count($part) === 0);
56
        }));
57
    }
58 891
59
    /**
60 891
     * @param self|string $part
61 567
     * @param self|string ...$parts
62
     */
63
    public static function and($part, ...$parts) : self
64 891
    {
65
        return new self(self::TYPE_AND, array_merge([$part], $parts));
66
    }
67
68
    /**
69
     * @param self|string $part
70
     * @param self|string ...$parts
71
     */
72
    public static function or($part, ...$parts) : self
73
    {
74 891
        return new self(self::TYPE_OR, array_merge([$part], $parts));
75
    }
76 891
77
    /**
78
     * Returns a new CompositeExpression with the given parts added.
79
     *
80 891
     * @param self|string $part
81 27
     * @param self|string ...$parts
82
     */
83
    public function with($part, ...$parts) : self
84 891
    {
85
        $that = clone $this;
86 891
87
        $that->parts[] = $part;
88
89
        foreach ($parts as $part) {
0 ignored issues
show
introduced by
$part is overwriting one of the parameters of this function.
Loading history...
90
            $that->parts[] = $part;
91
        }
92 864
93
        return $that;
94 864
    }
95
96
    /**
97
     * Retrieves the amount of expressions on composite expression.
98
     */
99
    public function count() : int
100 810
    {
101
        return count($this->parts);
102 810
    }
103 189
104
    /**
105
     * Retrieves the string representation of this composite expression.
106 621
     */
107
    public function __toString() : string
108
    {
109
        if ($this->count() === 1) {
110
            return (string) $this->parts[0];
111
        }
112 54
113
        return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
114 54
    }
115
116
    /**
117
     * Returns the type of this composite expression (AND/OR).
118
     */
119
    public function getType() : string
120
    {
121
        return $this->type;
122
    }
123
}
124