moirei/objects
最新稳定版本:1.1.0
Composer 安装命令:
composer require moirei/objects
包简介
Simple data & array objects.
README 文档
README
A package for simple and lightweight data/array objects.
Install
composer require moirei/objects
Note: this package does not automatically cast nested objects.
Usage
Object
final class User extends BaseObject { public string $name; public string $email; }
$user = new User([ 'name' => 'Joe', 'email' => 'top_lad@mail.com', ]); // or $user = User::make([ 'name' => 'Joe', 'email' => 'top_lad@mail.com', ]); // or $user = User::make(); $user->name = 'Joe'; $user['email'] = 'top_lad@mail.com'; ... dump($user->toArray()); dump($user);
Accessing or mutation undefined properties throws an exception.
Non-strict Object
/** * @property string|null $city */ final class User extends BaseObject { protected $strict = false; public string $name; public string $email; }
$user = User::make([ 'name' => 'Joe', 'email' => 'top_lad@mail.com', ]); ... $user->city = 'Adelaide';
Accessing or mutation undefined properties is allowed.
Rationale
If you're no longer comfortable passing or returning data as untyped array to your app logic, and want a simple solution, then this package is for you.
For the below example, we can be confident of the data type being returned from the action.
final class InstallationStatus extends BaseObject { public bool $completed = false; public ?string $key; /** @var string[] */ public array $errors = []; }
class InstallAppAction{ use AsAction; public function handle(string $code): InstallationStatus{ $status = InstallationStatus::make(); try{ // logic $status->completed = true; $status->key = '...'; }catch(\Exception $e){ $status->errors = [ $e->getMessage() ]; } return $status; } }
License
Special thanks to Eduardo San Martin Morote (posva) for encoding utlities
统计信息
- 总下载量: 131
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-12