Completed
Push — master ( 00b18d...cd26d4 )
by BENOIT
03:31
created

Expression::as()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BenTools\Where\Expression;
5
6
abstract class Expression
7
{
8
9
    /**
10
     * @param       $expression
11
     * @param array ...$values
12
     * @return CompositeExpression
13
     * @throws \InvalidArgumentException
14
     */
15
    final public function and($expression, ...$values): CompositeExpression
16
    {
17
        $expression = $this->where($expression, ...$values);
18
        return new CompositeExpression(' AND ', $this, $expression);
19
    }
20
21
    /**
22
     * @param       $expression
23
     * @param array ...$values
24
     * @return CompositeExpression
25
     * @throws \InvalidArgumentException
26
     */
27
    final public function or($expression, ...$values): CompositeExpression
28
    {
29
        $expression = $this->where($expression, ...$values);
30
        return new CompositeExpression(' OR ', $this, $expression);
31
    }
32
33
    /**
34
     * @param       $expression
35
     * @param array ...$values
36
     * @return CompositeExpression
37
     * @throws \InvalidArgumentException
38
     */
39
    final public function plus($expression, ...$values): CompositeExpression
40
    {
41
        $expression = $this->where($expression, ...$values);
42
        return new CompositeExpression(', ', $this, $expression);
43
    }
44
45
    /**
46
     * @param       $expression
47
     * @param array ...$values
48
     * @return CompositeExpression
49
     * @throws \InvalidArgumentException
50
     */
51
    final public function as($expression, ...$values): CompositeExpression
52
    {
53
        $expression = $this->where($expression, ...$values);
54
        return new CompositeExpression(' AS ', $this, $expression);
55
    }
56
57
    /**
58
     * @return GroupExpression
59
     */
60
    final public function asGroup(): GroupExpression
61
    {
62
        return $this instanceof GroupExpression ? $this : new GroupExpression($this);
63
    }
64
65
    /**
66
     * @return Expression|NegatedExpression
67
     */
68
    final public function negate(): Expression
69
    {
70
        return $this instanceof NegatedExpression ? clone $this->expression : new NegatedExpression($this);
0 ignored issues
show
Bug introduced by
The property expression does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
    }
72
73
    /**
74
     * @param       $expression
75
     * @param array ...$values
76
     * @return Expression
77
     * @throws \InvalidArgumentException
78
     */
79
    final public static function where($expression, ...$values): self
80
    {
81
        if (is_scalar($expression)) {
82
            return new Condition($expression, self::valuesFactory($values));
83
        }
84
        if ($expression instanceof self) {
85
            if (1 !== func_num_args()) {
86
                throw new \InvalidArgumentException("Cannot pass values to an existing Expression object.");
87
            }
88
            return $expression;
89
        }
90
        throw new \InvalidArgumentException(sprintf('Expected string or Expression object, %s given', is_object($expression) ? get_class($expression) : gettype($expression)));
91
    }
92
93
    /**
94
     * @param       $expression
95
     * @param array ...$values
96
     * @return GroupExpression
97
     * @throws \InvalidArgumentException
98
     */
99
    final public static function group($expression, ...$values): GroupExpression
100
    {
101
        return new GroupExpression(self::where($expression, ...$values));
102
    }
103
104
    /**
105
     * @param       $expression
106
     * @param array ...$values
107
     * @return NegatedExpression
108
     * @throws \InvalidArgumentException
109
     */
110
    final public static function not($expression, ...$values): NegatedExpression
111
    {
112
        return new NegatedExpression(self::where($expression, ...$values));
113
    }
114
115
    /**
116
     * @param array $values
117
     * @return array
118
     * @throws \InvalidArgumentException
119
     */
120
    final private static function valuesFactory(array $values): array
121
    {
122
        if (0 === count($values)) {
123
            return [];
124
        }
125
        if (1 === count($values) && is_array($values[0])) {
126
            return $values[0];
127
        }
128
        foreach ($values as $value) {
129
            if (is_array($value)) {
130
                throw new \InvalidArgumentException("Cannot construct expression with multiple array values.");
131
            }
132
        }
133
        return $values;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    abstract public function __toString(): string;
140
141
    /**
142
     * @return array
143
     */
144
    abstract public function getValues(): array;
145
146
    /**
147
     * @param Expression[] ...$expressions
148
     * @return array
149
     */
150
    final public static function valuesOf(self ...$expressions): array
151
    {
152
        $generator = function (Expression ...$expressions) {
153
            foreach ($expressions as $expression) {
154
                foreach ($expression->getValues() as $key => $value) {
155
                    if (is_numeric($key)) {
156
                        yield $value;
157
                    } else {
158
                        yield $key => $value;
159
                    }
160
                }
161
            }
162
        };
163
        return iterator_to_array($generator(...$expressions));
164
    }
165
}
166