Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ImmutableRecordLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ private function setNativeData(array $nativeData): void
$recordData[$key] = $val;
break;
case ImmutableRecord::PHP_TYPE_ARRAY:
if (\array_key_exists($key, $arrayPropItemTypeMap) && ! self::isScalarType($arrayPropItemTypeMap[$key])) {
$recordData[$key] = \array_map(function ($item) use ($key, &$arrayPropItemTypeMap) {
return $this->fromType($item, $arrayPropItemTypeMap[$key]);
if (\array_key_exists($specialKey, $arrayPropItemTypeMap) && ! self::isScalarType($arrayPropItemTypeMap[$specialKey])) {
$recordData[$key] = \array_map(function ($item) use ($specialKey, &$arrayPropItemTypeMap) {
return $this->fromType($item, $arrayPropItemTypeMap[$specialKey]);
}, $val);
} else {
$recordData[$key] = $val;
Expand Down
8 changes: 8 additions & 0 deletions tests/ImmutableRecordLogicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public function it_supports_special_keys(): void
RecordWithSpecialKey::BANK_ACCOUNT => '12324434',
RecordWithSpecialKey::SUCCESS_RATE => 33.33,
RecordWithSpecialKey::ITEM_LIST => [['name' => 'Awesome tester'], ['name' => 'John Smith']],
RecordWithSpecialKey::ITEM_ARRAY => [['name' => 'Awesome tester array'], ['name' => 'John Smith array']],

];
$specialKey = RecordWithSpecialKey::fromArray($recordArray);
$this->assertSame($recordArray, $specialKey->toArray());
Expand All @@ -171,6 +173,12 @@ public function it_supports_special_keys(): void
RecordWithSpecialKey::BANK_ACCOUNT => $recordArray[RecordWithSpecialKey::BANK_ACCOUNT],
RecordWithSpecialKey::SUCCESS_RATE => Percentage::fromFloat($recordArray[RecordWithSpecialKey::SUCCESS_RATE]),
RecordWithSpecialKey::ITEM_LIST => ItemList::fromArray($recordArray[RecordWithSpecialKey::ITEM_LIST]),
RecordWithSpecialKey::ITEM_ARRAY => array_map(
static function (array $item) {
return ImmutableItem::fromArray($item);
},
$recordArray[RecordWithSpecialKey::ITEM_ARRAY]
),
]);
$this->assertSame($recordArray, $specialKey->toArray());
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Stub/RecordWithSpecialKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class RecordWithSpecialKey implements ImmutableRecord, SpecialKeySupport
public const BANK_ACCOUNT = 'bank_account';
public const SUCCESS_RATE = 'success_rate';
public const ITEM_LIST = 'item_list';
public const ITEM_ARRAY = 'item_array';

/**
* @var string
Expand All @@ -40,6 +41,11 @@ final class RecordWithSpecialKey implements ImmutableRecord, SpecialKeySupport
*/
private $itemList;

/**
* @var array<ImmutableItem>
*/
private $itemArray;

/**
* @return mixed
*/
Expand All @@ -64,13 +70,23 @@ public function itemList(): ItemList
return $this->itemList;
}

/**
* @return array<ImmutableItem>
*/
public function itemArray(): array
{
return $this->itemArray;
}

public function convertKeyForRecord(string $key): string
{
switch ($key) {
case self::SUCCESS_RATE:
return 'successRate';
case self::ITEM_LIST:
return 'itemList';
case self::ITEM_ARRAY:
return 'itemArray';
default:
return 'bankAccount';
}
Expand All @@ -83,8 +99,17 @@ public function convertKeyForArray(string $key): string
return self::SUCCESS_RATE;
case 'itemList':
return self::ITEM_LIST;
case 'itemArray':
return self::ITEM_ARRAY;
default:
return self::BANK_ACCOUNT;
}
}

private static function arrayPropItemTypeMap(): array
{
return [
'itemArray' => ImmutableItem::class,
];
}
}