krzar / array-dto
v1.2.0
2024-09-18 08:04 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^9.5
README
此包允许您根据数组数据生成对象。
这对于与某些API集成很有用,例如。
支持
安装
composer require krzar/array-dto
用法
简单对象
use KrZar\ArrayDto\ArrayDto; class UserData extends ArrayDto { public string $name; public string $email; public int $age; public float $money; public bool $isActive = false; public array $roles = []; }
要从数组创建此对象,请调用
$data = [ 'name' => 'Test', 'email' => 'test@test.com', 'age' => 99, 'money' => 1520.50, 'isActive' => true, 'roles' => ['ADMIN'] ]; UserData::create($data);
如果未传递任何参数,将分配默认值,因此应为此类情况设置默认值。
嵌套对象
class CompanyData extends ArrayObject { public string $name; public string $city; public string $street; } class UserData extends ArrayObject { public string $name; public string $email; public int $age; public float $money; public bool $isActive = false; public array $roles = []; public CompanyData $company; }
要从数组创建此对象,请调用
$data = [ 'name' => 'Test', 'email' => 'test@test.com', 'age' => 99, 'money' => 1520.50, 'isActive' => true, 'roles' => ['ADMIN'], 'company' => [ 'name' => 'Test Company', 'city' => 'Test', 'street' => 'Test Street 1' ] ]; UserData::create($data);
嵌套多维对象
如果您想创建对象数组,您需要使用$arrayMap
属性进行配置。
class UserData extends ArrayObject { public string $name; public string $email; public int $age; public float $money; public bool $isActive = false; public array $roles = []; public CompanyData $company; public array $children; protected function casts(): array { return [ 'children' => new \KrZar\ArrayDto\Casts\MultidimensionalCast(UserData::class), ]; } }
要从数组创建此对象,请调用
$data = [ 'name' => 'Test', 'email' => 'test@test.com', 'age' => 99, 'money' => 1520.50, 'isActive' => true, 'roles' => ['ADMIN'], 'company' => [ 'name' => 'Test Company', 'city' => 'Test', 'street' => 'Test Street 1' ], 'children' => [ [ 'name' => 'Test 2', 'email' => 'test2@test.com', 'age' => 98, 'money' => 2400, 'isActive' => true, 'roles' => ['MODERATOR'] ] ] ]; UserData::create($data);
联合类型
您可以在对象中使用联合类型,但有一些限制
- 内置类型可以以任何方式组合
- ArrayObject类型只能与内置类型组合,不能与其他ArrayObject组合
例如
public CompanyData|string $company;
CompanyData
将在数组中company
索引为数组类型时创建。
名称映射
您可以使用$namesMap
数组映射参数名称
class UserData extends ArrayObject { public string $name; public string $email; public int $age; public float $money; public bool $isActive = false; public array $roles = []; public CompanyData $company; public array $children; protected function casts(): array { return [ 'children' => new \KrZar\ArrayDto\Casts\MultidimensionalCast(UserData::class), 'is_active' => new \KrZar\ArrayDto\Casts\NameCast('isActive') ]; } }
类型映射
类型会自动映射。
自定义转换
您可以创建任何自定义转换和映射到参数。您还可以提供转换数组。
class UserData extends ArrayObject { public string $name; public string $email; public int $age; public float $money; public bool $isActive = false; public array $roles = []; public CompanyData $company; public array $children; public int $agePlusTen; protected function casts(): array { return [ 'children' => new \KrZar\ArrayDto\Casts\MultidimensionalCast(UserData::class), 'is_active' => new \KrZar\ArrayDto\Casts\NameCast('isActive'), 'agePlusTen' => new \KrZar\ArrayDto\Casts\CustomCast( fn(mixed $value, array $raw) => $raw['age'] + 10 ), ]; } }