CompositeExpression.php 3.42 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Query\Expression;

5
use Countable;
6

7
use function array_merge;
8 9 10
use function count;
use function implode;

11 12 13
/**
 * Composite expression is responsible to build a group of similar expression.
 */
14
class CompositeExpression implements Countable
15 16
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
17
     * Constant that represents an AND composite expression.
18
     */
19
    public const TYPE_AND = 'AND';
20

21
    /**
Benjamin Morel's avatar
Benjamin Morel committed
22
     * Constant that represents an OR composite expression.
23
     */
24
    public const TYPE_OR = 'OR';
25

26
    /**
Benjamin Morel's avatar
Benjamin Morel committed
27 28 29
     * The instance type of composite expression.
     *
     * @var string
30 31
     */
    private $type;
32

33
    /**
Benjamin Morel's avatar
Benjamin Morel committed
34 35
     * Each expression part of the composite expression.
     *
36
     * @var self[]|string[]
37
     */
38
    private $parts = [];
39

40
    /**
41 42
     * @internal Use the and() / or() factory methods.
     *
43 44
     * @param string          $type  Instance type of composite expression.
     * @param self[]|string[] $parts Composition of expressions to be joined on composite expression.
45
     */
46
    public function __construct($type, array $parts = [])
47 48
    {
        $this->type = $type;
49

50 51
        $this->addMultiple($parts);
    }
52

53 54 55 56
    /**
     * @param self|string $part
     * @param self|string ...$parts
     */
57
    public static function and($part, ...$parts): self
58 59 60 61
    {
        return new self(self::TYPE_AND, array_merge([$part], $parts));
    }

62 63 64 65
    /**
     * @param self|string $part
     * @param self|string ...$parts
     */
66
    public static function or($part, ...$parts): self
67 68 69 70
    {
        return new self(self::TYPE_OR, array_merge([$part], $parts));
    }

71 72
    /**
     * Adds multiple parts to composite expression.
73
     *
74 75
     * @deprecated This class will be made immutable. Use with() instead.
     *
76
     * @param self[]|string[] $parts
77
     *
Grégoire Paris's avatar
Grégoire Paris committed
78
     * @return CompositeExpression
79
     */
80
    public function addMultiple(array $parts = [])
81
    {
82
        foreach ($parts as $part) {
83 84
            $this->add($part);
        }
85

86 87
        return $this;
    }
88

89 90
    /**
     * Adds an expression to composite expression.
91
     *
92 93
     * @deprecated This class will be made immutable. Use with() instead.
     *
94
     * @param mixed $part
Benjamin Morel's avatar
Benjamin Morel committed
95
     *
Grégoire Paris's avatar
Grégoire Paris committed
96
     * @return CompositeExpression
97 98 99
     */
    public function add($part)
    {
100
        if ($part === null) {
101
            return $this;
102
        }
103

104
        if ($part instanceof self && count($part) === 0) {
105 106 107 108 109
            return $this;
        }

        $this->parts[] = $part;

110 111
        return $this;
    }
112

113 114 115 116 117 118
    /**
     * Returns a new CompositeExpression with the given parts added.
     *
     * @param self|string $part
     * @param self|string ...$parts
     */
119
    public function with($part, ...$parts): self
120 121 122
    {
        $that = clone $this;

123
        $that->parts = array_merge($that->parts, [$part], $parts);
124 125 126 127

        return $that;
    }

128 129
    /**
     * Retrieves the amount of expressions on composite expression.
130
     *
131
     * @return int
132 133 134 135 136
     */
    public function count()
    {
        return count($this->parts);
    }
137

138
    /**
Benjamin Morel's avatar
Benjamin Morel committed
139
     * Retrieves the string representation of this composite expression.
140
     *
141 142 143 144
     * @return string
     */
    public function __toString()
    {
145
        if ($this->count() === 1) {
146 147
            return (string) $this->parts[0];
        }
148

149 150
        return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
    }
151

152
    /**
Benjamin Morel's avatar
Benjamin Morel committed
153
     * Returns the type of this composite expression (AND/OR).
154
     *
155 156 157 158 159 160
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
161
}