wearelaradev/serializable
Composer 安装命令:
composer require wearelaradev/serializable
包简介
Serializable trait
README 文档
README
This php package implements a serialization feature. This serialization trait, named "Serializable", allows you to serialize any php object into an array or json. It also enables any object implementing this trait to become a serializer in its own right.
Getting started
composer require wearelaradev/serializable
Usage
Properties must be public or have an associated getter to be serialized.
Basic Usage
use Laraved\Serializable\Serializable; class MyObject { use Serializable; public string $foo = "foo"; public string $bar = "bar"; } var_dump((new MyObject())->toArray()); // output // ["foo" => "foo", "bar" => "bar"] var_dump((new MyObject())->toJson()); // output // "{"foo": "foo", "bar": "bar"}"
Used with of getters
use Laradev\Serializable\Serializable; class MyObject { use Serializable; private string $foo = "foo"; protected string $bar = "bar"; public function getFoo(): string { return $this->foo; } } var_dump((new MyObject())->toArray()); // output // ["foo" => "foo"] var_dump((new MyObject())->toJson()); // output // "{"foo": "foo"}"
Used with a whitelist and blacklist strategy
You can apply a whitelist or blacklist strategy to your serializer properties using a property or function.
With a property:
Whitelist
use Laradev\Serializable\Serializable; class MyObject { use Serializable; private array $whitelist = [ "foo" ]; public string $foo = "foo"; public string $bar = "bar"; } var_dump((new MyObject())->toArray()); // output // ["foo" => "foo"] var_dump((new MyObject())->toJson()); // output // "{"foo": "foo"}"
Blacklist
use Laradev\Serializable\Serializable; class MyObject { use Serializable; private array $blacklist = [ "foo" ]; public string $foo = "foo"; public string $bar = "bar"; } var_dump((new MyObject())->toArray()); // output // ["bar" => "bar"] var_dump((new MyObject())->toJson()); // output // "{"bar": "bar"}"
With a function:
Whitelist
use Laradev\Serializable\Serializable; class MyObject { use Serializable; public string $foo = "foo"; public string $bar = "bar"; protected function whitelist(): array { // your logic return [ "foo" ]; } } var_dump((new MyObject())->toArray()); // output // ["foo" => "foo"] var_dump((new MyObject())->toJson()); // output // "{"foo": "foo"}"
Blacklist
use Laradev\Serializable\Serializable; class MyObject { use Serializable; public string $foo = "foo"; public string $bar = "bar"; protected function blacklist(): array { // your logic return [ "foo" ]; } } var_dump((new MyObject())->toArray()); // output // ["bar" => "bar"] var_dump((new MyObject())->toJson()); // output // "{"bar": "bar"}"
Additional information
If you encounter a bug or have any ideas for improvement, don't hesitate to send me a PR or contact me via email at florian@laradev.ca :)
统计信息
- 总下载量: 508
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: mit
- 更新时间: 2024-04-11