phpstream/objective
最新稳定版本:0.0.1
Composer 安装命令:
composer require phpstream/objective
包简介
Simple domain transfer objects.
README 文档
README
Installation
composer require phpstream/objective
Example Setup
use Phpstream\Objective\Traits\Objective; use Symfony\Component\Validator\Constraints; class Comment { use Objective; #[Constraints\NotBlank] #[Constraints\Length(min: 6)] public string $message; }
use Phpstream\Objective\Traits\Objective; use Symfony\Component\Validator\Constraints; class Post { use Objective; #[Constraints\NotBlank] #[Constraints\Length(min: 12)] public string $title; #[Constraints\NotBlank] #[Constraints\Length(min: 128)] public string $body; /** @var array<Comment> */ public array $comments = []; }
Constraints are optional. See Symfony Validation Documentation for available constraints.
Deserialization
$array = [ 'title' => 'Hello World', 'body' => 'Salutations!', 'comments' => [ ['message' => 'First!'], ['message' => 'Second!'] ], ]; /* make from array */ $post = Post::make($array); /* make from object */ $post = Post::make($post->toObject()); /* make from JSON */ $post = Post::make($post->toJson()); /* make from YAML */ $post = Post::make($post->toYaml());
Serialization
use Phpstream\Objective\Enums\Output; /* serialize to array */ $array = $post->toArray(); /* serialize to object */ $object = $post->toObject(); /* serialize to JSON */ $json = $post->toJson(); /* serialize to YAML */ $yaml = $post->toYaml(); // OR /* serialize to array */ $array = $post->to(Output::Array); /* serialize to object */ $object = $post->to(Output::Object); /* serialize to JSON */ $json = $post->to(Output::JSON); /* serialize to YAML */ $yaml = $post->to(Output::YAML);
Validation
$validation = Post::validate([ 'title' => 'This title is way too long for the validation rule...', 'body' => 'This body is way too short for the validation rule...', ]); if ($validation->fails()) foreach($validation->errors() as $error) echo "$error->getProperty(): $error->getMessage()";
You may also validate from serialization formats.
/* Or validate from other data formats */ $post = Post::make($input); /* validate from class */ $validation = Post::validate($post); /* validate from object */ $validation = Post::validate($post->toObject()); /* validate from JSON */ $validation = Post::validate($post->toJson()); /* validate from YAML */ $validation = Post::validate($post->toYaml());
统计信息
- 总下载量: 82
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-13
