|
| 1 | +## Name Mapping |
| 2 | + |
| 3 | +### Basic Usage |
| 4 | + |
| 5 | +```php |
| 6 | +use Astral\Serialize\Attributes\InputName; |
| 7 | +use Astral\Serialize\Attributes\OutputName; |
| 8 | +use Astral\Serialize\Serialize; |
| 9 | + |
| 10 | +class User extends Serialize { |
| 11 | + // Use a different property name for input |
| 12 | + #[InputName('user_name')] |
| 13 | + public string $name; |
| 14 | + |
| 15 | + // Use a different property name for output |
| 16 | + #[OutputName('user_id')] |
| 17 | + public int $id; |
| 18 | + |
| 19 | + // Support different names for both input and output |
| 20 | + #[InputName('register_time')] |
| 21 | + #[OutputName('registeredAt')] |
| 22 | + public DateTime $createdAt; |
| 23 | +} |
| 24 | + |
| 25 | +// Input data with different names |
| 26 | +$user = User::from([ |
| 27 | + 'user_name' => 'Job', // Mapped to $name |
| 28 | + 'id' => 123, // Remains unchanged |
| 29 | + 'register_time' => '2023-01-01 10:00:00' // Mapped to $createdAt |
| 30 | +]); |
| 31 | + |
| 32 | +// Output data |
| 33 | +$userArray = $user->toArray(); |
| 34 | +// $userArray toArray: |
| 35 | +// [ |
| 36 | +// 'name' => 'Job', |
| 37 | +// 'user_id' => 123, |
| 38 | +// 'registeredAt' => '2023-01-01 10:00:00' |
| 39 | +// ] |
| 40 | +``` |
| 41 | + |
| 42 | +### Handling Multiple Input/Output Names |
| 43 | + |
| 44 | +```php |
| 45 | +use Astral\Serialize\Attributes\InputName; |
| 46 | +use Astral\Serialize\Attributes\OutputName; |
| 47 | +use Astral\Serialize\Serialize; |
| 48 | + |
| 49 | +class MultiOutputUser extends Serialize { |
| 50 | + // output multiple names |
| 51 | + #[OutputName('user_id')] |
| 52 | + #[OutputName('id')] |
| 53 | + #[OutputName('userId')] |
| 54 | + public int $id; |
| 55 | + |
| 56 | + // Multiple input names, the first matching name in declaration order will be used |
| 57 | + #[InputName('user_name')] |
| 58 | + #[InputName('other_name')] |
| 59 | + #[InputName('userName')] |
| 60 | + public int $name; |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +// Scenario 1: Use the first matching input name |
| 65 | +$user1 = MultiInputUser::from([ |
| 66 | + 'user_name' => 'Job' // Use 'user_name' |
| 67 | +]); |
| 68 | +echo $user1->name; // Output 'Job' |
| 69 | + |
| 70 | +// Scenario 2: Use the second matching input name |
| 71 | +$user2 = MultiInputUser::from([ |
| 72 | + 'other_name' => 'Tom' // Use 'Tom' |
| 73 | +]); |
| 74 | +echo $user2->name; // Output 'Tom' |
| 75 | + |
| 76 | +// Scenario 3: Use the last input name |
| 77 | +$user3 = MultiInputUser::from([ |
| 78 | + 'userName' => 'Lin' // Use 'userName' |
| 79 | +]); |
| 80 | +echo $user3->name; // Output 'Lin' |
| 81 | + |
| 82 | +// Scenario 4: When multiple are passed, the first matching name in declaration order is used |
| 83 | +$user4 = MultiInputUser::from([ |
| 84 | + 'userName' => 'Job', |
| 85 | + 'other_name' => 'Tom', |
| 86 | + 'user_name' => 'Lin', |
| 87 | +]); |
| 88 | +echo $user4->name; // Output 'Job' |
| 89 | + |
| 90 | +// Create user object |
| 91 | +$user = MultiOutputUser::from([ |
| 92 | + 'id' => 123, |
| 93 | + 'name' => 'Job' |
| 94 | +]); |
| 95 | + |
| 96 | +// Convert to array |
| 97 | +// tips: Since id has multiple output names, the output includes ['user_id','id','userId'] |
| 98 | +$userArray = $user->toArray(); |
| 99 | +// $userArray toArray: |
| 100 | +// [ |
| 101 | +// 'user_id' => 123, |
| 102 | +// 'id' => 123, |
| 103 | +// 'userId' => 123, |
| 104 | +// ] |
| 105 | +``` |
| 106 | + |
| 107 | +### Complex Mapping Scenarios |
| 108 | + |
| 109 | +```php |
| 110 | +use Astral\Serialize\Serialize; |
| 111 | + |
| 112 | +class ComplexUser extends Serialize { |
| 113 | + // Name mapping for nested objects |
| 114 | + #[InputName('user_profile')] |
| 115 | + public UserProfile $profile; |
| 116 | + |
| 117 | + // Name mapping for array elements |
| 118 | + #[InputName('user_tags')] |
| 119 | + public array $tags; |
| 120 | +} |
| 121 | + |
| 122 | +// Handle complex input structure |
| 123 | +$complexUser = ComplexUser::from([ |
| 124 | + 'user_profile' => [ |
| 125 | + 'nickname' => 'job', |
| 126 | + 'age' => 25 |
| 127 | + ], |
| 128 | + 'user_tags' => ['developer', 'programmer'] |
| 129 | +]); |
| 130 | + |
| 131 | +// Convert to standard array |
| 132 | +$complexUserArray = $complexUser->toArray(); |
| 133 | +// $complexUserArray toArray: |
| 134 | +// [ |
| 135 | +// 'profile' => UserProfile Object ([ |
| 136 | +// 'nickname' => 'job', |
| 137 | +// 'age' => 25 |
| 138 | +// ]), |
| 139 | +// 'tags' => ['developer', 'programmer'] |
| 140 | +// ] |
| 141 | +``` |
0 commit comments