Completed
Push — master ( f0a67b...3b5eef )
by recca
02:02
created

Criteria::push()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Recca0120\Repository;
4
5
use ReflectionClass;
6
use BadMethodCallException;
7
8
class Criteria
9
{
10
    /**
11
     * $actions.
12
     *
13
     * @var array
14
     */
15
    protected $actions = [];
16
17
    /**
18
     * $allowTypes.
19
     *
20
     * @var array
21
     */
22
    protected $allowTypes = [
23
        'from',
24
        'select',
25
        'where',
26
        'join',
27
        'having',
28
        'order',
29
        'union',
30
        'groupBy',
31
        'on',
32
        'with',
33
        'take',
34
        'skip',
35
        'has',
36
    ];
37
38
    /**
39
     *  _call.
40
     *
41
     * @param string $method
42
     * @param mixed $parameters
43
     * @return \Recca0120\Repository\Criteria
44
     */
45 25
    public function __call($method, $parameters)
46
    {
47 25
        if (preg_match('/'.implode('|', $this->allowTypes).'/i', $method, $match)) {
48 24
            $this->actions[] = new Action($match[0], $method, $parameters);
49
50 24
            return $this;
51
        }
52
53 1
        throw new BadMethodCallException('Call to undefined method '.static::class."::{$method}()");
54
    }
55
56
    /**
57
     * __callStatic.
58
     *
59
     * @param string $method
60
     * @param array $parameters
61
     * @return static
62
     */
63 1
    public static function __callStatic($method, $parameters)
64
    {
65 1
        $criteria = new static();
66
67 1
        return call_user_func_array([$criteria, $method], $parameters);
68
    }
69
70
    /**
71
     * all.
72
     *
73
     * @return array
74
     */
75 22
    public function all()
76
    {
77 22
        return $this->actions;
78
    }
79
80
    /**
81
     * alias raw.
82
     *
83
     * @param mixed $value
84
     * @return Expression
85
     */
86 2
    public static function expr($value)
87
    {
88 2
        return static::raw($value);
89
    }
90
91
    /**
92
     * @param mixed $value
93
     * @return \Recca0120\Repository\Expression
94
     */
95 2
    public static function raw($value)
96
    {
97 2
        return new Expression($value);
98
    }
99
100
    /**
101
     * Creates an instance of the class.
102
     *
103
     * @return Criteria
104
     */
105 24
    public static function create()
106
    {
107 24
        $args = func_get_args();
108
109 24
        switch (count($args)) {
110 24
            case 0:
111 21
                return new static();
112 3
            case 1:
113 1
                return new static($args[0]);
114 3
            case 2:
115 3
                return new static($args[0], $args[1]);
116 1
            case 3:
117 1
                return new static($args[0], $args[1], $args[2]);
118 1
            case 4:
119 1
                return new static($args[0], $args[1], $args[2], $args[3]);
120 1
            default:
121 1
                $reflectionClass = new ReflectionClass(static::class);
122
123 1
                return $reflectionClass->newInstanceArgs(func_get_args());
124 1
        }
125
    }
126
}
127