Skip to content

Commit 3db2352

Browse files
author
Alexander Miertsch
authored
Merge pull request #20 from arnedesmedt/hotfix/specialKeyForArrayPropItemMap
arrayPropItemTypeMap should be indexed with a specialKey
2 parents 562df91 + 2ec9e91 commit 3db2352

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/ImmutableRecordLogic.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ private function setNativeData(array $nativeData): void
190190
$recordData[$key] = $val;
191191
break;
192192
case ImmutableRecord::PHP_TYPE_ARRAY:
193-
if (\array_key_exists($key, $arrayPropItemTypeMap) && ! self::isScalarType($arrayPropItemTypeMap[$key])) {
194-
$recordData[$key] = \array_map(function ($item) use ($key, &$arrayPropItemTypeMap) {
195-
return $this->fromType($item, $arrayPropItemTypeMap[$key]);
193+
if (\array_key_exists($specialKey, $arrayPropItemTypeMap) && ! self::isScalarType($arrayPropItemTypeMap[$specialKey])) {
194+
$recordData[$key] = \array_map(function ($item) use ($specialKey, &$arrayPropItemTypeMap) {
195+
return $this->fromType($item, $arrayPropItemTypeMap[$specialKey]);
196196
}, $val);
197197
} else {
198198
$recordData[$key] = $val;

tests/ImmutableRecordLogicTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ public function it_supports_special_keys(): void
163163
RecordWithSpecialKey::BANK_ACCOUNT => '12324434',
164164
RecordWithSpecialKey::SUCCESS_RATE => 33.33,
165165
RecordWithSpecialKey::ITEM_LIST => [['name' => 'Awesome tester'], ['name' => 'John Smith']],
166+
RecordWithSpecialKey::ITEM_ARRAY => [['name' => 'Awesome tester array'], ['name' => 'John Smith array']],
167+
166168
];
167169
$specialKey = RecordWithSpecialKey::fromArray($recordArray);
168170
$this->assertSame($recordArray, $specialKey->toArray());
@@ -171,6 +173,12 @@ public function it_supports_special_keys(): void
171173
RecordWithSpecialKey::BANK_ACCOUNT => $recordArray[RecordWithSpecialKey::BANK_ACCOUNT],
172174
RecordWithSpecialKey::SUCCESS_RATE => Percentage::fromFloat($recordArray[RecordWithSpecialKey::SUCCESS_RATE]),
173175
RecordWithSpecialKey::ITEM_LIST => ItemList::fromArray($recordArray[RecordWithSpecialKey::ITEM_LIST]),
176+
RecordWithSpecialKey::ITEM_ARRAY => array_map(
177+
static function (array $item) {
178+
return ImmutableItem::fromArray($item);
179+
},
180+
$recordArray[RecordWithSpecialKey::ITEM_ARRAY]
181+
),
174182
]);
175183
$this->assertSame($recordArray, $specialKey->toArray());
176184
}

tests/Stub/RecordWithSpecialKey.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class RecordWithSpecialKey implements ImmutableRecord, SpecialKeySupport
2424
public const BANK_ACCOUNT = 'bank_account';
2525
public const SUCCESS_RATE = 'success_rate';
2626
public const ITEM_LIST = 'item_list';
27+
public const ITEM_ARRAY = 'item_array';
2728

2829
/**
2930
* @var string
@@ -40,6 +41,11 @@ final class RecordWithSpecialKey implements ImmutableRecord, SpecialKeySupport
4041
*/
4142
private $itemList;
4243

44+
/**
45+
* @var array<ImmutableItem>
46+
*/
47+
private $itemArray;
48+
4349
/**
4450
* @return mixed
4551
*/
@@ -64,13 +70,23 @@ public function itemList(): ItemList
6470
return $this->itemList;
6571
}
6672

73+
/**
74+
* @return array<ImmutableItem>
75+
*/
76+
public function itemArray(): array
77+
{
78+
return $this->itemArray;
79+
}
80+
6781
public function convertKeyForRecord(string $key): string
6882
{
6983
switch ($key) {
7084
case self::SUCCESS_RATE:
7185
return 'successRate';
7286
case self::ITEM_LIST:
7387
return 'itemList';
88+
case self::ITEM_ARRAY:
89+
return 'itemArray';
7490
default:
7591
return 'bankAccount';
7692
}
@@ -83,8 +99,17 @@ public function convertKeyForArray(string $key): string
8399
return self::SUCCESS_RATE;
84100
case 'itemList':
85101
return self::ITEM_LIST;
102+
case 'itemArray':
103+
return self::ITEM_ARRAY;
86104
default:
87105
return self::BANK_ACCOUNT;
88106
}
89107
}
108+
109+
private static function arrayPropItemTypeMap(): array
110+
{
111+
return [
112+
'itemArray' => ImmutableItem::class,
113+
];
114+
}
90115
}

0 commit comments

Comments
 (0)