Skip to content

Commit

Permalink
Return null for mapOf (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: david_smith <[email protected]>
  • Loading branch information
zero-to-prod and david_smith authored Nov 8, 2024
1 parent 7af0a86 commit 079464e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/DataModelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ trait DataModelHelper
*/
public static function mapOf(mixed $value, array $context, ?ReflectionAttribute $Attribute, ReflectionProperty $Property)
{
if (!$value) {
return null;
}

$args = $Attribute?->getArguments()[0];
$value = isset($args['coerce']) && !isset($value[0]) ? [$value] : $value;

Expand Down Expand Up @@ -122,7 +126,7 @@ public static function pregReplace(mixed $value, array $context, ?ReflectionAttr
*/
public static function isUrl(mixed $value, array $context, ?ReflectionAttribute $Attribute, ReflectionProperty $Property): ?string
{
if(!$value){
if (!$value) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/MapOf/Array/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class User
'cast' => [self::class, 'mapOf'],
'type' => Alias::class,
])]
public array $Aliases;
public ?array $Aliases;

/** @var Alias[][] $AliasesNested */
#[Describe([
'cast' => [self::class, 'mapOf'],
'type' => Alias::class,
'level' => 2,
])]
public array $AliasesNested;
public ?array $AliasesNested;
}
4 changes: 2 additions & 2 deletions tests/Unit/MapOf/KeyBy/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class User
'type' => Alias::class,
'key_by' => 'id'
])]
public array $Aliases;
public ?array $Aliases;

/** @var Alias[][] $AliasesNested */
#[Describe([
Expand All @@ -26,5 +26,5 @@ class User
'level' => 2,
'key_by' => 'id'
])]
public array $AliasesNested;
public ?array $AliasesNested;
}
2 changes: 1 addition & 1 deletion tests/Unit/MapOf/MapVia/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ class User
'map_via' => 'mapper',
'level' => 2,
])]
public Collection $AliasesNested;
public ?Collection $AliasesNested;
}

0 comments on commit 079464e

Please sign in to comment.