phpstream/objective

Simple domain transfer objects.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/phpstream/objective

0.0.1 2025-12-09 13:11 UTC

This package is auto-updated.

Last update: 2026-01-13 13:20:04 UTC


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());