1 | <?php |
||
33 | trait ConfigureTrait |
||
34 | { |
||
35 | |||
36 | 4 | protected function configure($modelClass, $config) |
|
37 | { |
||
38 | 4 | if (!empty($modelClass)) |
|
39 | { |
||
40 | 4 | if (is_string($modelClass)) |
|
41 | { |
||
42 | $this->setModel(new $modelClass); |
||
43 | } |
||
44 | 4 | elseif (is_object($modelClass)) |
|
45 | { |
||
46 | 4 | $this->setModel($modelClass); |
|
47 | } |
||
48 | else |
||
49 | { |
||
50 | throw new ManganException('Invalid model type for ' . static::class); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | 4 | $model = $this->getModel(); |
|
55 | |||
56 | // Set criteria from model |
||
57 | 4 | $criteria = $this->getCriteria(); |
|
58 | 4 | if (!empty($model) && $criteria instanceof MergeableInterface) |
|
59 | { |
||
60 | // NOTE: WithCriteria and CriteriaAware have just slightly different method names |
||
61 | 4 | if ($model instanceof WithCriteriaInterface) |
|
62 | { |
||
63 | 1 | $criteria->mergeWith($model->getDbCriteria()); |
|
64 | } |
||
65 | 3 | elseif ($model instanceof CriteriaAwareInterface) |
|
66 | { |
||
67 | $criteria->mergeWith($model->getCriteria()); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | // Merge criteria from configuration |
||
72 | 4 | if (isset($config['criteria'])) |
|
73 | { |
||
74 | $criteria->mergeWith($config['criteria']); |
||
75 | unset($config['criteria']); |
||
76 | } |
||
77 | |||
78 | // Merge limit from configuration |
||
79 | 4 | if (isset($config['limit']) && $config['limit'] > 0) |
|
80 | { |
||
81 | 1 | $criteria->setLimit($config['limit']); |
|
82 | 1 | unset($config['limit']); |
|
83 | } |
||
84 | |||
85 | // Merge sorting from configuration |
||
86 | 4 | if (isset($config['sort'])) |
|
87 | { |
||
88 | // Apply default sorting if criteria does not have sort configured |
||
89 | $sort = $criteria->getSort(); |
||
90 | if (isset($config['sort']['defaultOrder']) && empty($sort)) |
||
91 | { |
||
92 | $criteria->setSort($config['sort']['defaultOrder']); |
||
93 | } |
||
94 | unset($config['sort']); |
||
95 | } |
||
96 | |||
97 | 4 | if (isset($config['pagination'])) |
|
98 | { |
||
99 | 2 | $this->setPagination($config['pagination']); |
|
|
|||
100 | 2 | unset($config['pagination']); |
|
101 | } |
||
102 | |||
103 | 4 | if (!empty($model) && !$criteria->getSelect()) |
|
104 | { |
||
105 | 4 | $fields = array_keys(ManganMeta::create($model)->fields()); |
|
106 | 4 | $selected = array_fill_keys($fields, true); |
|
107 | 4 | $criteria->setSelect($selected); |
|
108 | } |
||
109 | |||
110 | 4 | foreach ($config as $key => $value) |
|
111 | { |
||
112 | $this->$key = $value; |
||
113 | } |
||
114 | 4 | } |
|
115 | |||
116 | /** |
||
117 | * Configure limits, sorting for fetching data |
||
118 | * @return CriteriaInterface |
||
119 | */ |
||
120 | 4 | protected function configureFetch() |
|
141 | |||
142 | /** |
||
143 | * Returns the sort object. |
||
144 | * @return SortInterface the sorting object. If this is false, it means the sorting is disabled. |
||
145 | */ |
||
146 | abstract public function getSort(); |
||
147 | |||
148 | /** |
||
149 | * Returns the pagination object. |
||
150 | * @param string $className the pagination object class name, use this param to override default pagination class. |
||
151 | * @return PaginationInterface|Pagination|false the pagination object. If this is false, it means the pagination is disabled. |
||
152 | */ |
||
153 | abstract public function getPagination($className = Pagination::class); |
||
154 | |||
155 | /** |
||
156 | * Returns the total number of data items. |
||
157 | * When {@link pagination} is set false, this returns the same value as {@link itemCount}. |
||
158 | * @return integer total number of possible data items. |
||
159 | */ |
||
160 | abstract public function getTotalItemCount(); |
||
161 | |||
162 | /** |
||
163 | * @return CriteriaInterface |
||
164 | */ |
||
165 | abstract public function getCriteria(); |
||
166 | |||
167 | /** |
||
168 | * @return AnnotatedInterface |
||
169 | */ |
||
170 | abstract public function getModel(); |
||
171 | |||
172 | /** |
||
173 | * @param $model AnnotatedInterface |
||
174 | * @return static |
||
175 | */ |
||
176 | abstract public function setModel(AnnotatedInterface $model); |
||
177 | } |
||
178 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.