1 | <?php declare(strict_types=1); |
||
19 | abstract class ModelAbstract extends Model |
||
|
|||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | public static $sortingOptions = []; |
||
25 | |||
26 | /** |
||
27 | * Sets the URL attribute with a URL to this object |
||
28 | * |
||
29 | * @param string $pageName |
||
30 | * @param Controller $controller |
||
31 | * @param array $params |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function setUrl($pageName, Controller $controller, array $params = array()) |
||
36 | { |
||
37 | $params = $this->getModelUrlParams($params); |
||
38 | |||
39 | $this->url = $controller->pageUrl($pageName, $params, false); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param array $params |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | abstract protected function getModelUrlParams(array $params): array; |
||
48 | |||
49 | /** |
||
50 | * Gets a list of items related to Posts for frontend use |
||
51 | * |
||
52 | * @param $query |
||
53 | * @param array $options Available options are "sort", "displayEmpty", "limit", "post" |
||
54 | * |
||
55 | * @return mixed |
||
56 | */ |
||
57 | public function scopeListFrontend(Builder $query, array $options = []) |
||
58 | { |
||
59 | $this->withRelation($query, $options); |
||
60 | |||
61 | $sortField = $this->queryOrderBy($query, $options); |
||
62 | |||
63 | $this->queryDisplayEmpty($query, $options); |
||
64 | |||
65 | $this->queryPostSlug($query, $options); |
||
66 | |||
67 | $this->queryLimit($query, $options); |
||
68 | |||
69 | // GROUP BY is required for SQLite to deal with HAVING |
||
70 | // We use it for all connections just to keep implementation |
||
71 | // independent from the connection being used |
||
72 | $this->queryGroupBy($query, $sortField); |
||
73 | |||
74 | return $query->get(); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param Builder $query |
||
79 | * @param string $property |
||
80 | * @param mixed $value |
||
81 | */ |
||
82 | public function scopeWhereTranslatable(Builder $query, string $property, $value) |
||
83 | { |
||
84 | self::whereTranslatableProperty($query, $property, $value); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param Builder $query |
||
89 | * @param string $property |
||
90 | * @param $value |
||
91 | */ |
||
92 | public static function whereTranslatableProperty(Builder $query, string $property, $value) |
||
93 | { |
||
94 | $query->getModel()->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel') |
||
95 | ? $query->transWhere($property, $value) |
||
96 | : $query->where($property, $value); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param Builder $query |
||
101 | * @param array $options |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | protected function queryDisplayEmpty(Builder $query, array $options) |
||
106 | { |
||
107 | if (empty($options['displayEmpty'])) { |
||
108 | $query |
||
109 | ->having('posts_count', '>', 0); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param Builder $query |
||
115 | * @param array $options |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | protected function queryPostSlug(Builder $query, array $options) |
||
130 | |||
131 | /** |
||
132 | * @param Builder $query |
||
133 | * @param array $options |
||
134 | * |
||
135 | * @return void |
||
136 | */ |
||
137 | private function queryLimit(Builder $query, array $options) |
||
143 | |||
144 | /** |
||
145 | * @param Builder $query |
||
146 | * @param array $options |
||
147 | * |
||
148 | * @return string|null |
||
149 | */ |
||
150 | private function queryOrderBy(Builder $query, array $options): ?string |
||
166 | |||
167 | /** |
||
168 | * @param Builder $query |
||
169 | * @param array $options |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | protected function withRelation(Builder $query, array $options) |
||
197 | |||
198 | /** |
||
199 | * @param Builder $query |
||
200 | * @param string|null $sortField |
||
201 | */ |
||
202 | private function queryGroupBy(Builder $query, ?string $sortField = null) |
||
212 | |||
213 | /** |
||
214 | * @param Builder $query |
||
215 | * @param array $options |
||
216 | */ |
||
217 | protected static function handleExceptions(Builder $query, array $options) |
||
231 | } |
||
232 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.