1 | <?php |
||
35 | trait ExportableTrait |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * The array containing the data for product type configuration (configurables, bundles, etc). |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $artefacts = array(); |
||
44 | |||
45 | /** |
||
46 | * The export adapter instance. |
||
47 | * |
||
48 | * @var \TechDivision\Import\Adapter\ExportAdapterInterface |
||
49 | */ |
||
50 | protected $exportAdapter; |
||
51 | |||
52 | /** |
||
53 | * The ID of the product that has been created recently. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $lastEntityId; |
||
58 | |||
59 | /** |
||
60 | * Return's the artefacts for post-processing. |
||
61 | * |
||
62 | * @return array The artefacts |
||
63 | */ |
||
64 | 4 | public function getArtefacts() |
|
68 | |||
69 | /** |
||
70 | * Reset the array with the artefacts to free the memory. |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | 1 | protected function resetArtefacts() |
|
78 | |||
79 | /** |
||
80 | * Add the passed product type artefacts to the product with the |
||
81 | * last entity ID. |
||
82 | * |
||
83 | * @param string $type The artefact type, e. g. configurable |
||
84 | * @param array $artefacts The product type artefacts |
||
85 | * @param boolean $override Whether or not the artefacts for the actual entity ID has to be overwritten |
||
86 | * |
||
87 | * @return void |
||
88 | * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId() |
||
89 | */ |
||
90 | 5 | public function addArtefacts($type, array $artefacts, $override = true) |
|
91 | { |
||
92 | |||
93 | // query whether or not, any artefacts are available |
||
94 | 5 | if (sizeof($artefacts) === 0) { |
|
95 | 1 | return; |
|
96 | } |
||
97 | |||
98 | // serialize the original data, if we're in debug mode |
||
99 | 4 | $keys = array_keys($artefacts); |
|
100 | 4 | foreach ($keys as $key) { |
|
101 | 4 | if (isset($artefacts[$key][ColumnKeys::ORIGINAL_DATA])) { |
|
102 | 4 | $artefacts[$key][ColumnKeys::ORIGINAL_DATA] = $this->isDebugMode() ? serialize($artefacts[$key][ColumnKeys::ORIGINAL_DATA]) : null; |
|
103 | } |
||
104 | } |
||
105 | |||
106 | // query whether or not, existing artefacts has to be overwritten |
||
107 | 4 | if ($override === true) { |
|
108 | 4 | $this->overrideArtefacts($type, $artefacts); |
|
109 | } else { |
||
110 | $this->appendArtefacts($type, $artefacts); |
||
111 | } |
||
112 | 4 | } |
|
113 | |||
114 | /** |
||
115 | * Add the passed product type artefacts to the product with the |
||
116 | * last entity ID and overrides existing ones with the same key. |
||
117 | * |
||
118 | * @param string $type The artefact type, e. g. configurable |
||
119 | * @param array $artefacts The product type artefacts |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | 4 | protected function overrideArtefacts($type, array $artefacts) |
|
129 | |||
130 | /** |
||
131 | * Append's the passed product type artefacts to the product with the |
||
132 | * last entity ID. |
||
133 | * |
||
134 | * @param string $type The artefact type, e. g. configurable |
||
135 | * @param array $artefacts The product type artefacts |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | protected function appendArtefacts($type, array $artefacts) |
||
145 | |||
146 | /** |
||
147 | * Return the artefacts for the passed type and entity ID. |
||
148 | * |
||
149 | * @param string $type The artefact type, e. g. configurable |
||
150 | * @param string $entityId The entity ID to return the artefacts for |
||
151 | * |
||
152 | * @return array The array with the artefacts |
||
153 | * @throws \Exception Is thrown, if no artefacts are available |
||
154 | */ |
||
155 | 2 | public function getArtefactsByTypeAndEntityId($type, $entityId) |
|
156 | { |
||
157 | |||
158 | // query whether or not, artefacts for the passed params are available |
||
159 | 2 | if (isset($this->artefacts[$type][$entityId])) { |
|
160 | // load the artefacts |
||
161 | 1 | $artefacts = $this->artefacts[$type][$entityId]; |
|
162 | |||
163 | // unserialize the original data, if we're in debug mode, if we're in debug mode |
||
164 | 1 | $keys = array_keys($artefacts); |
|
165 | 1 | foreach ($keys as $key) { |
|
166 | 1 | if (isset($artefacts[$key][ColumnKeys::ORIGINAL_DATA])) { |
|
167 | 1 | $artefacts[$key][ColumnKeys::ORIGINAL_DATA] = $this->isDebugMode() ? unserialize($artefacts[$key][ColumnKeys::ORIGINAL_DATA]) : null; |
|
168 | } |
||
169 | } |
||
170 | |||
171 | // return the artefacts |
||
172 | 1 | return $artefacts; |
|
173 | } |
||
174 | |||
175 | // throw an exception if not |
||
176 | 1 | throw new \Exception( |
|
177 | 1 | sprintf( |
|
178 | 1 | 'Cant\'t load artefacts for type %s and entity ID %d', |
|
179 | 1 | $type, |
|
180 | 1 | $entityId |
|
181 | ) |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Queries whether or not artefacts for the passed type and entity ID are available. |
||
187 | * |
||
188 | * @param string $type The artefact type, e. g. configurable |
||
189 | * @param string $entityId The entity ID to return the artefacts for |
||
190 | * |
||
191 | * @return boolean TRUE if artefacts are available, else FALSE |
||
192 | */ |
||
193 | public function hasArtefactsByTypeAndEntityId($type, $entityId) |
||
197 | |||
198 | /** |
||
199 | * Create's and return's a new empty artefact entity. |
||
200 | * |
||
201 | * @param array $columns The array with the column data |
||
202 | * @param array $originalColumnNames The array with a mapping from the old to the new column names |
||
203 | * |
||
204 | * @return array The new artefact entity |
||
205 | */ |
||
206 | 1 | public function newArtefact(array $columns, array $originalColumnNames = array()) |
|
227 | |||
228 | /** |
||
229 | * Export's the artefacts to CSV files and resets the array with the artefacts to free the memory. |
||
230 | * |
||
231 | * @param integer $timestamp The timestamp part of the original import file |
||
232 | * @param string $counter The counter part of the origin import file |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | 1 | public function export($timestamp, $counter) |
|
245 | |||
246 | /** |
||
247 | * Set's the exporter adapter instance. |
||
248 | * |
||
249 | * @param \TechDivision\Import\Adapter\ExportAdapterInterface $exportAdapter The exporter adapter instance |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | 2 | public function setExportAdapter(ExportAdapterInterface $exportAdapter) |
|
257 | |||
258 | /** |
||
259 | * Return's the exporter adapter instance. |
||
260 | * |
||
261 | * @return \TechDivision\Import\Adapter\ExportAdapterInterface The exporter adapter instance |
||
262 | */ |
||
263 | 2 | public function getExportAdapter() |
|
267 | |||
268 | /** |
||
269 | * Set's the ID of the product that has been created recently. |
||
270 | * |
||
271 | * @param string $lastEntityId The entity ID |
||
272 | * |
||
273 | * @return void |
||
274 | */ |
||
275 | 4 | public function setLastEntityId($lastEntityId) |
|
279 | |||
280 | /** |
||
281 | * Return's the ID of the product that has been created recently. |
||
282 | * |
||
283 | * @return string The entity Id |
||
284 | */ |
||
285 | 4 | public function getLastEntityId() |
|
289 | |||
290 | /** |
||
291 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
292 | * |
||
293 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
294 | */ |
||
295 | abstract public function isDebugMode(); |
||
296 | } |
||
297 |
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.