Completed
Pull Request — master (#3850)
by Benjamin
61:13
created

CompositeExpression::with()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
ccs 2
cts 2
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 CompositeExpression with the given part added.
58 891
     *
59
     * This instance is immutable and unaffected by this method call.
60 891
     *
61 567
     * @param self|string ...$parts
62
     */
63
    public function with(...$parts) : self
64 891
    {
65
        $that = clone $this;
66
67
        foreach ($parts as $part) {
68
            if ($part instanceof self && count($part) === 0) {
69
                continue;
70
            }
71
72
            $that->parts[] = $part;
73
        }
74 891
75
        return $that;
76 891
    }
77
78
    /**
79
     * Retrieves the amount of expressions on composite expression.
80 891
     */
81 27
    public function count() : int
82
    {
83
        return count($this->parts);
84 891
    }
85
86 891
    /**
87
     * Retrieves the string representation of this composite expression.
88
     */
89
    public function __toString() : string
90
    {
91
        if ($this->count() === 1) {
92 864
            return (string) $this->parts[0];
93
        }
94 864
95
        return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
96
    }
97
98
    /**
99
     * Returns the type of this composite expression (AND/OR).
100 810
     */
101
    public function getType() : string
102 810
    {
103 189
        return $this->type;
104
    }
105
}
106