|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\ComposerConfigManager\Application\Updater; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationItemInterface; |
|
5
|
|
|
|
|
6
|
|
|
class ListUpdater |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @param ConfigurationItemInterface[] $newEntityList |
|
10
|
|
|
* @param ConfigurationItemInterface[] $oldEntityList |
|
11
|
|
|
* |
|
12
|
|
|
* @return ConfigurationItemInterface[] |
|
13
|
|
|
*/ |
|
14
|
3 |
|
public function update(array $newEntityList, array $oldEntityList) |
|
15
|
|
|
{ |
|
16
|
3 |
|
$existingEntityIdList = []; |
|
17
|
3 |
|
foreach ($newEntityList as $entity) { |
|
18
|
3 |
|
$existingEntityIdList[$entity->getItemId()] = true; |
|
19
|
3 |
|
} |
|
20
|
3 |
|
$normalizedOldEntityList = []; |
|
21
|
3 |
|
foreach ($oldEntityList as $oldEntity) { |
|
22
|
2 |
|
if (!array_key_exists($oldEntity->getItemId(), $existingEntityIdList)) { |
|
23
|
2 |
|
$normalizedOldEntityList[] = $oldEntity; |
|
24
|
2 |
|
} else { |
|
25
|
|
|
// A new entity have been defined, loop over new entity list and append all entities with the same id |
|
26
|
1 |
|
$oldEntityId = $oldEntity->getItemId(); |
|
27
|
1 |
|
foreach ($newEntityList as $newEntityKey => $newEntity) { |
|
28
|
1 |
|
if ($newEntity->getItemId() == $oldEntityId) { |
|
29
|
1 |
|
$normalizedOldEntityList[] = $newEntity; |
|
30
|
1 |
|
unset($newEntityList[$newEntityKey]); |
|
31
|
1 |
|
} |
|
32
|
1 |
|
} |
|
33
|
|
|
} |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
return array_merge($normalizedOldEntityList, $newEntityList); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $newPropertyList |
|
41
|
|
|
* @param array $oldPropertyList |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function updateRaw(array $newPropertyList, array $oldPropertyList) |
|
46
|
|
|
{ |
|
47
|
3 |
|
$newPropertyKeyList = []; |
|
48
|
3 |
|
foreach ($newPropertyList as $propertyKey => $value) { |
|
49
|
3 |
|
$newPropertyKeyList[$propertyKey] = true; |
|
50
|
3 |
|
} |
|
51
|
3 |
|
$normalizedOldPropertyList = []; |
|
52
|
3 |
|
foreach ($oldPropertyList as $propertyKey => $oldPropertyValue) { |
|
53
|
2 |
|
if (!isset($newPropertyKeyList[$propertyKey])) { |
|
54
|
2 |
|
$normalizedOldPropertyList[$propertyKey] = $oldPropertyValue; |
|
55
|
2 |
|
} else { |
|
56
|
|
|
// A new value have been defined |
|
57
|
1 |
|
$normalizedOldPropertyList[$propertyKey] = $this->mergeRawValue( |
|
58
|
1 |
|
$newPropertyList[$propertyKey], |
|
59
|
|
|
$oldPropertyValue |
|
60
|
1 |
|
); |
|
61
|
1 |
|
unset($newPropertyList[$propertyKey]); |
|
62
|
|
|
} |
|
63
|
3 |
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
return array_merge($normalizedOldPropertyList, $newPropertyList); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
protected function mergeRawValue($newPropertyValue, $oldPropertyValue) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$isArray = is_array($newPropertyValue) && is_array($oldPropertyValue); |
|
71
|
|
|
|
|
72
|
1 |
|
return (false === $isArray) |
|
73
|
1 |
|
? $newPropertyValue |
|
74
|
1 |
|
: $this->updateRaw($newPropertyValue, $oldPropertyValue) |
|
75
|
1 |
|
; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|