ap-lib/caster-scheme

AP\Caster\Scheme is a plugins for caster for casting objects based on scheme

dev-main 2025-03-14 18:22 UTC

This package is auto-updated.

Last update: 2025-03-14 18:23:49 UTC


README

MIT License

AP\Caster\Scheme is a plugins for caster for casting objects based on scheme

Installation

composer require ap-lib/caster-scheme

Features

  • Caster uses the ToObject::toObject(mixed $data) method to construct objects.

Requirements

  • PHP 8.3 or higher

Getting started

Here's a quick example demonstrating how to use AP\Caster.

Initialize the PrimaryCaster with SchemeCaster

$toObject = new ToObject(
    new \AP\ToObject\ObjectParser\ByConstructor(),
    new PrimaryCaster([
        new SchemeCaster,
    ])
);

class Guid implements \AP\Scheme\ToObject
{
    public function __construct(public string $bites) {
    
    }

    public static function toObject(array|string|int|float|bool|null $data): static
    {
        if (is_string($data)) {
            return new static(pack("h*", str_replace('-', '', $data)));
        }
        throw ThrowableErrors::one('value is not valid uuid bytes');
    }
}

class Request {
    public function __constructor(
        public User $user,
        public Guid $guid, 
    ){}
}

$obj = $toObject->makeObject(
    [
        "user" => [
            "name" => "John", 
            "age" => 12
        ], 
        "guid" => "6B29FC40-CA47-1067-B31D-00DD010662DA"
    ],
    Request::class
);