Completed
Push — master ( 43819a...28fa94 )
by Sergei
05:48 queued 05:44
created

CompositeExpression::add()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

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