1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Repository\Compilers; |
4
|
|
|
|
5
|
|
|
use Recca0120\Repository\Criteria; |
6
|
|
|
use Recca0120\Repository\Expression; |
7
|
|
|
|
8
|
|
|
abstract class Compiler |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* $items. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $items = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* $model. |
19
|
|
|
* |
20
|
|
|
* @var mixed |
21
|
|
|
*/ |
22
|
|
|
protected $model; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* __construct. |
26
|
|
|
* |
27
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
28
|
|
|
*/ |
29
|
39 |
|
public function __construct($model) |
30
|
|
|
{ |
31
|
39 |
|
$this->model = $model; |
32
|
39 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* push. |
36
|
|
|
* |
37
|
|
|
* @param \Recca0120\Repository\Criteria $criteria |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
39 |
|
public function push($criteria) |
41
|
|
|
{ |
42
|
39 |
|
$items = is_array($criteria) ? $criteria : [$criteria]; |
43
|
|
|
|
44
|
39 |
|
foreach ($items as $key => $value) { |
45
|
22 |
|
if (($value instanceof Criteria) === true) { |
46
|
20 |
|
$this->items[] = $value; |
47
|
20 |
|
} else { |
48
|
4 |
|
$value = is_array($value) ? $value : [$key, $value]; |
49
|
4 |
|
$criteria = call_user_func_array([Criteria::create(), 'where'], $value); |
50
|
4 |
|
$this->items[] = $criteria; |
51
|
|
|
} |
52
|
39 |
|
} |
53
|
|
|
|
54
|
39 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* apply. |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
39 |
|
public function apply() |
63
|
|
|
{ |
64
|
39 |
|
foreach ($this->group() as $type => $actions) { |
65
|
22 |
|
$method = method_exists($this, $type) === true ? $type : 'defaults'; |
66
|
22 |
|
$this->model = call_user_func_array([$this, $method], [$this->model, $actions]); |
67
|
38 |
|
} |
68
|
|
|
|
69
|
38 |
|
return $this->model; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* defaults apply. |
74
|
|
|
* |
75
|
|
|
* @param mixed $model |
76
|
|
|
* @param array $actions |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
14 |
|
public function defaults($model, $actions) |
80
|
|
|
{ |
81
|
|
|
return array_reduce($actions, function($model, $action) { |
82
|
13 |
|
return call_user_func_array([$model, $action['method']], $action['parameters']); |
83
|
14 |
|
}, $model); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* isExpression. |
88
|
|
|
* |
89
|
|
|
* @param mixed$param |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
13 |
|
protected function isExpression($param) |
93
|
|
|
{ |
94
|
13 |
|
return $param instanceof Expression; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* group. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function group() { |
103
|
39 |
|
return array_reduce($this->items, function($allows, $criteria) { |
104
|
22 |
|
foreach ($criteria->all() as $action) { |
105
|
22 |
|
$allows[$action->type][] = [ |
106
|
22 |
|
'method' => $action->method, |
107
|
22 |
|
'parameters' => $this->compileParameters($action->parameters), |
108
|
|
|
]; |
109
|
22 |
|
} |
110
|
|
|
|
111
|
22 |
|
return $allows; |
112
|
39 |
|
}, []); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* transformParameters. |
117
|
|
|
* |
118
|
|
|
* @param array $parameters |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
abstract protected function compileParameters($parameters); |
122
|
|
|
} |
123
|
|
|
|