1 | <?php |
||
5 | class ConfigCollection extends Base |
||
6 | { |
||
7 | /** |
||
8 | * @var \Iris\Mapping\Image |
||
9 | */ |
||
10 | private $imageMapping; |
||
11 | |||
12 | /** |
||
13 | * @var \Iris\Mapping\SimpleCollection |
||
14 | */ |
||
15 | private $simpleCollectionMapping; |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | public function assign(array $externalData) |
||
21 | { |
||
22 | $configCollection = new \Iris\Transfer\Catalog\ConfigCollection(); |
||
23 | |||
24 | foreach ($externalData as $c) { |
||
25 | |||
26 | $data[] = new \Iris\Transfer\Catalog\Config([ |
||
|
|||
27 | 'sku' => $c['sku'], |
||
28 | 'name' => $c['name'], |
||
29 | 'description' => $c['description'], |
||
30 | 'brand' => $c['brand'], |
||
31 | 'price' => $c['price'], |
||
32 | 'special_price' => $c['special_price'], |
||
33 | 'special_from_date' => $c['special_from_date'], |
||
34 | 'special_to_date' => $c['special_to_date'], |
||
35 | 'attributes' => $c['attributes'], |
||
36 | 'attribute_set' => $c['attribute_set'], |
||
37 | 'images' => $this->getImageMapping() |
||
38 | ->assign($c['images']), |
||
39 | 'simple_collection' => $this->getSimpleCollectionMapping() |
||
40 | ->assign($c['simple_collection']) |
||
41 | ]); |
||
42 | } |
||
43 | |||
44 | return $configCollection; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | * @param \Iris\Transfer\Catalog\ConfigCollection $internalData |
||
50 | */ |
||
51 | public function map($internalData) |
||
64 | |||
65 | /** |
||
66 | * @return \Iris\Mapping\Image |
||
67 | */ |
||
68 | public function getImageMapping() |
||
73 | |||
74 | /** |
||
75 | * @param \Iris\Mapping\Image $map |
||
76 | * @return \Iris\Mapping\ConfigCollection |
||
77 | */ |
||
78 | public function setImageMapping(Image $map) |
||
83 | |||
84 | /** |
||
85 | * @return \Iris\Mapping\SimpleCollection |
||
86 | */ |
||
87 | public function getSimpleCollectionMapping() |
||
94 | |||
95 | /** |
||
96 | * @param \Iris\Mapping\SimpleCollection $map |
||
97 | * @return \Iris\Mapping\ConfigCollection |
||
98 | */ |
||
99 | public function setSimpleCollectionMapping(SimpleCollection $map) |
||
104 | } |
||
105 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.