1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Iris\Mapping; |
4
|
|
|
|
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) |
52
|
|
|
{ |
53
|
|
|
$externalData = []; |
54
|
|
|
foreach ($internalData as $config) { |
55
|
|
|
$externalData[] = [ |
56
|
|
|
'sku' => $config->getSku(), |
57
|
|
|
'simple_collection' => $this->getSimpleCollectionMapping() |
58
|
|
|
->map($config->getSimpleCollection()) |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $externalData; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \Iris\Mapping\Image |
67
|
|
|
*/ |
68
|
|
|
public function getImageMapping() |
69
|
|
|
{ |
70
|
|
|
$this->imageMapping || $this->setImageMapping(Image::getInstance()); |
71
|
|
|
return $this->imageMapping; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param \Iris\Mapping\Image $map |
76
|
|
|
* @return \Iris\Mapping\ConfigCollection |
77
|
|
|
*/ |
78
|
|
|
public function setImageMapping(Image $map) |
79
|
|
|
{ |
80
|
|
|
$this->imageMapping = $map; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return \Iris\Mapping\SimpleCollection |
86
|
|
|
*/ |
87
|
|
|
public function getSimpleCollectionMapping() |
88
|
|
|
{ |
89
|
|
|
$this->simpleCollectionMapping || |
90
|
|
|
$this->setSimpleCollectionMapping(SimpleCollection::getInstance()); |
91
|
|
|
|
92
|
|
|
return $this->simpleCollectionMapping; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \Iris\Mapping\SimpleCollection $map |
97
|
|
|
* @return \Iris\Mapping\ConfigCollection |
98
|
|
|
*/ |
99
|
|
|
public function setSimpleCollectionMapping(SimpleCollection $map) |
100
|
|
|
{ |
101
|
|
|
$this->simpleCollectionMapping = $map; |
102
|
|
|
return $this; |
103
|
|
|
} |
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.