ap-lib / caster-scheme
AP\Caster\Scheme is a plugins for caster for casting objects based on scheme
dev-main
2025-04-15 01:41 UTC
Requires
- php: ^8.3
- ap-lib/caster: dev-main
- ap-lib/scheme: dev-main
Requires (Dev)
- phpunit/phpunit: 10.5.*
This package is auto-updated.
Last update: 2025-07-15 02:23:28 UTC
README
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
);