1 | <?php |
||
30 | trait Accessor |
||
31 | { |
||
32 | use QueryAccessor { |
||
33 | queryOne as parentQueryOne; |
||
34 | queryAll as parentQueryAll; |
||
35 | } |
||
36 | |||
37 | /******************************************* |
||
38 | * OBJECT CLASSES |
||
39 | *******************************************/ |
||
40 | |||
41 | /** |
||
42 | * @return string|null |
||
43 | */ |
||
44 | abstract public static function objectClass(); |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | public static function objectClassInstance(): string |
||
53 | |||
54 | /******************************************* |
||
55 | * CREATE |
||
56 | *******************************************/ |
||
57 | |||
58 | /** |
||
59 | * @param mixed $config |
||
60 | * @return BaseObject |
||
61 | * @throws \yii\base\InvalidConfigException |
||
62 | */ |
||
63 | public function create($config = []) |
||
64 | { |
||
65 | if ($config instanceof Record) { |
||
66 | $config = $this->prepareConfigFromRecord($config); |
||
67 | } |
||
68 | |||
69 | if (!is_array($config)) { |
||
70 | $config = ArrayHelper::toArray($config, [], false); |
||
71 | } |
||
72 | |||
73 | return ObjectHelper::create( |
||
74 | $this->prepareConfig($config), |
||
75 | static::objectClassInstance() |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param array $config |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function prepareConfig(array $config = []): array |
||
84 | { |
||
85 | // Auto-set the class |
||
86 | $class = static::objectClass(); |
||
87 | if ($class !== null) { |
||
88 | $config['class'] = $class; |
||
89 | } |
||
90 | |||
91 | return $config; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param Record $record |
||
96 | * @return array |
||
97 | */ |
||
98 | protected function prepareConfigFromRecord(Record $record): array |
||
105 | |||
106 | /******************************************* |
||
107 | * FIND / GET |
||
108 | *******************************************/ |
||
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | */ |
||
113 | public function find($identifier) |
||
122 | |||
123 | /******************************************* |
||
124 | * CACHE |
||
125 | *******************************************/ |
||
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | * @throws \yii\base\InvalidConfigException |
||
130 | */ |
||
131 | protected function queryOne(QueryInterface $query) |
||
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | * @throws \yii\base\InvalidConfigException |
||
141 | */ |
||
142 | protected function queryAll(QueryInterface $query) |
||
148 | |||
149 | /** |
||
150 | * @param $result |
||
151 | * @return null|BaseObject |
||
152 | */ |
||
153 | protected function createFromQueryResult($result) |
||
161 | |||
162 | /** |
||
163 | * @param array $results |
||
164 | * @return array |
||
165 | */ |
||
166 | protected function createAllFromQueryResults(array $results): array |
||
175 | |||
176 | /******************************************* |
||
177 | * EXCEPTIONS |
||
178 | *******************************************/ |
||
179 | |||
180 | /** |
||
181 | * @throws ObjectNotFoundException |
||
182 | */ |
||
183 | protected function notFoundException() |
||
191 | } |
||
192 |