定制 phpstream/objective 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpstream/objective

最新稳定版本:0.0.1

Composer 安装命令:

composer require phpstream/objective

包简介

Simple domain transfer objects.

README 文档

README

banner

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固