Skip to content

Commit a5425ea

Browse files
authored
maint/add-docs-for-enum-support (#29)
* maint/add-docs-for-enum-support Updated `README.md`. * maint/add-docs-for-enum-support Updated `tests/Unit/MapOf/Array/ArrayTest.php`. * maint/add-docs-for-enum-support Created `tests/Unit/MapOf/Array/Name.php`. * maint/add-docs-for-enum-support Updated `tests/Unit/MapOf/Array/User.php`.
1 parent ddc881a commit a5425ea

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class User
119119

120120
In this case the `mapOf()` method returns an array of `Alias` instances.
121121

122+
This method will also work with enums.
123+
122124
```php
123125
use Zerotoprod\DataModel\Describe;
124126

@@ -134,6 +136,13 @@ class User
134136
'required', // Throws PropertyRequiredException when value not present
135137
])]
136138
public array $Aliases;
139+
140+
/** @var Name[] $Names */
141+
#[Describe([
142+
'cast' => [self::class, 'mapOf'],
143+
'type' => Name::class,
144+
])]
145+
public ?array $Names;
137146
}
138147

139148
class Alias
@@ -143,15 +152,27 @@ class Alias
143152
public string $name;
144153
}
145154

155+
enum Name: string
156+
{
157+
case Tom = 'Tom';
158+
case John = 'John';
159+
}
160+
146161
$User = User::from([
147162
'Aliases' => [
148163
['name' => 'John Doe'],
149164
['name' => 'John Smith'],
165+
],
166+
'Names' => [
167+
'Tom',
168+
'John',
150169
]
151170
]);
152171

153172
echo $User->Aliases[0]->name; // Outputs: John Doe
154173
echo $User->Aliases[1]->name; // Outputs: John Smith
174+
echo $User->Names[0]; // Enum Name::Tom
175+
echo $User->Names[1]; // Enum Name::John
155176
```
156177

157178
#### Laravel Collection Example

tests/Unit/MapOf/Array/ArrayTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ class ArrayTest extends TestCase
1313
'Aliases' => [
1414
['name' => 'John Doe'],
1515
['name' => 'John Smith'],
16+
],
17+
'Names' => [
18+
Name::Tom->value,
19+
Name::John->value,
1620
]
1721
]);
1822

1923
self::assertEquals('John Doe', $User->Aliases[0]->name);
2024
self::assertEquals('John Smith', $User->Aliases[1]->name);
25+
self::assertEquals(Name::Tom, $User->Names[0]);
2126
}
2227

2328
#[Test] public function nested(): void

tests/Unit/MapOf/Array/Name.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Tests\Unit\MapOf\Array;
4+
5+
enum Name: string
6+
{
7+
case Tom = 'Tom';
8+
case John = 'John';
9+
}

tests/Unit/MapOf/Array/User.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,11 @@ class User
2525
'level' => 2,
2626
])]
2727
public ?array $AliasesNested;
28+
29+
/** @var Name[] $Names */
30+
#[Describe([
31+
'cast' => [self::class, 'mapOf'],
32+
'type' => Name::class,
33+
])]
34+
public ?array $Names;
2835
}

0 commit comments

Comments
 (0)