1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\View; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
8
|
|
|
use Sco\Admin\Contracts\View\Extensions\ExtensionInterface; |
9
|
|
|
use Sco\Admin\Contracts\View\ViewInterface; |
10
|
|
|
use Sco\Admin\View\Extensions\Applies; |
11
|
|
|
use Sco\Admin\View\Extensions\Filters; |
12
|
|
|
use Sco\Admin\View\Extensions\Scopes; |
13
|
|
|
|
14
|
|
|
abstract class View implements ViewInterface, Arrayable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $with = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var RepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
protected $repository; |
25
|
|
|
|
26
|
|
|
protected $type; |
27
|
|
|
|
28
|
|
|
protected $extensions; |
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->extensions = new Extensions(); |
33
|
|
|
|
34
|
|
|
$this->extend('scopes', new Scopes()); |
35
|
|
|
$this->extend('applies', new Applies()); |
36
|
|
|
$this->extend('filters', new Filters()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function initialize() |
40
|
|
|
{ |
41
|
|
|
$this->extensions->initialize(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function extend($name, ExtensionInterface $extension) |
45
|
|
|
{ |
46
|
|
|
$this->extensions->put($name, $extension); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setRepository(RepositoryInterface $repository) |
50
|
|
|
{ |
51
|
|
|
$this->repository = $repository; |
52
|
|
|
$this->repository->with($this->getWith()); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getRepository() |
58
|
|
|
{ |
59
|
|
|
return $this->repository; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string[] |
64
|
|
|
*/ |
65
|
|
|
public function getWith() |
66
|
|
|
{ |
67
|
|
|
return $this->with; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function with($relations) |
74
|
|
|
{ |
75
|
|
|
$this->with = array_flatten(func_get_args()); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getQuery() |
81
|
|
|
{ |
82
|
|
|
$repository = $this->getRepository(); |
83
|
|
|
|
84
|
|
|
$builder = $repository->getQuery(); |
85
|
|
|
|
86
|
|
|
if ($repository->isRestorable()) { |
87
|
|
|
$builder->withTrashed(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $builder; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add an "order by" clause to the query. |
95
|
|
|
* |
96
|
|
|
* @param string $column |
97
|
|
|
* @param string $direction |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function orderBy($column, $direction = 'asc') |
102
|
|
|
{ |
103
|
|
|
$this->scopes['orderBy'] = function (Builder $builder) use ($column, $direction) { |
|
|
|
|
104
|
|
|
$builder->orderBy($column, $direction); |
|
|
|
|
105
|
|
|
}; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function toArray() |
111
|
|
|
{ |
112
|
|
|
return [ |
113
|
|
|
'type' => $this->type, |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function __call($name, $parameters) |
118
|
|
|
{ |
119
|
|
|
dump($name, $parameters); |
120
|
|
|
$method = substr($name, 3); |
121
|
|
|
|
122
|
|
|
dump($method, str_plural($method)); |
123
|
|
|
if (starts_with($name, 'get') && $this->extensions->has(str_plural($method))) { |
124
|
|
|
return $this->extensions->get($method); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (starts_with($name, 'set') && $this->extensions->has($method)) { |
128
|
|
|
$extension = $this->extensions->get($method); |
|
|
|
|
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: