1 | <?php |
||
14 | trait EntityExtractorTrait |
||
15 | { |
||
16 | /** |
||
17 | * List of entities to extract |
||
18 | * [ |
||
19 | * 'entity_code' => 'Model Classname' |
||
20 | * ] |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $entitiesToProcess = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $entities = []; |
||
29 | |||
30 | /** |
||
31 | * @var PropertyAccessor |
||
32 | */ |
||
33 | protected $accessor; |
||
34 | |||
35 | /** |
||
36 | * @param string $entityType |
||
37 | * @param string$entityClass |
||
38 | * @param function $identifierCallback |
||
39 | * @return $this |
||
40 | * @throws \Exception |
||
41 | */ |
||
42 | 6 | public function addEntityToProcess($entityType, $entityClass, $identifierCallback) |
|
56 | |||
57 | /** |
||
58 | * @inheritDoc |
||
59 | */ |
||
60 | 5 | public function extract() |
|
61 | { |
||
62 | try { |
||
63 | 5 | $this->init(); |
|
64 | 5 | $this->check(); |
|
65 | |||
66 | 5 | $files = $this->getFiles($this->getFileExtension()); |
|
67 | 5 | foreach ($this->entitiesToProcess as $entityType => $data) { |
|
68 | 5 | if (!isset($files[$entityType])) { |
|
69 | 1 | continue; |
|
70 | } |
||
71 | 4 | $this->processFile($entityType); |
|
72 | } |
||
73 | |||
74 | 3 | return $this->entities; |
|
75 | 2 | } catch (\Exception $e) { |
|
76 | 2 | throw $e; |
|
77 | } |
||
78 | } |
||
79 | |||
80 | 5 | protected function check() |
|
88 | |||
89 | 5 | protected function init() |
|
93 | |||
94 | /** |
||
95 | * @param string $entityType |
||
96 | * @param array $data |
||
97 | * @return mixed |
||
98 | * @throws \Exception |
||
99 | */ |
||
100 | 4 | protected function processObject($entityType, array $data) |
|
126 | |||
127 | /** |
||
128 | * @param string $identifier |
||
129 | * @return mixed |
||
130 | * @throws \Exception |
||
131 | */ |
||
132 | 3 | protected function getEntity($identifier) |
|
143 | |||
144 | /** |
||
145 | * @param string $filename |
||
146 | * @throws \Exception |
||
147 | */ |
||
148 | 4 | protected function processFile($filename) |
|
167 | } |
||
168 |
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.