Completed
Push — master ( 906125...5fd544 )
by BENOIT
01:10
created

Expression::where()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 2
dl 0
loc 13
rs 8.8571
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 Expression
37
     * @throws \InvalidArgumentException
38
     */
39
    final public static function where($expression, ...$values): self
40
    {
41
        if (is_string($expression)) {
42
            return new Condition($expression, self::valuesFactory($values));
43
        }
44
        if ($expression instanceof self) {
45
            if (1 !== func_num_args()) {
46
                throw new \InvalidArgumentException("Cannot pass values to an existing Expression object.");
47
            }
48
            return $expression;
49
        }
50
        throw new \InvalidArgumentException(sprintf('Expected string or Expression object, %s given', is_object($expression) ? get_class($expression) : gettype($expression)));
51
    }
52
53
    /**
54
     * @param       $expression
55
     * @param array ...$values
56
     * @return GroupExpression
57
     * @throws \InvalidArgumentException
58
     */
59
    final public static function group($expression, ...$values): GroupExpression
60
    {
61
        return new GroupExpression(self::where($expression, ...$values));
62
    }
63
64
    /**
65
     * @param       $expression
66
     * @param array ...$values
67
     * @return NegatedExpression
68
     * @throws \InvalidArgumentException
69
     */
70
    final public static function not($expression, ...$values): NegatedExpression
71
    {
72
        return new NegatedExpression(self::where($expression, ...$values));
73
    }
74
75
    /**
76
     * @param array $values
77
     * @return array
78
     * @throws \InvalidArgumentException
79
     */
80
    final private static function valuesFactory(array $values): array
81
    {
82
        if (0 === count($values)) {
83
            return [];
84
        }
85
        if (1 === count($values) && is_array($values[0])) {
86
            return $values[0];
87
        }
88
        foreach ($values as $value) {
89
            if (is_array($value)) {
90
                throw new \InvalidArgumentException("Cannot construct expression with multiple array values.");
91
            }
92
        }
93
        return $values;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    abstract public function __toString(): string;
100
101
    /**
102
     * @return array
103
     */
104
    abstract public function getValues(): array;
105
}
106