acelot / struct
Declarative structure builder with validator and automapper for PHP 7
Installs: 2 139
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^7.2
- ext-json: *
- acelot/automapper: ^1.1
- acelot/helpers: ^1.0
- respect/validation: ^1.1
Requires (Dev)
- phpunit/phpunit: ^7.0
README
Declarative structure builder for PHP 7.
Usage
Create some model:
<?php declare(strict_types=1); namespace MyNamespace; use Acelot\Struct\Struct; use Acelot\Struct\Schema; use Acelot\Struct\Schema\Prop; use function Acelot\AutoMapper\from; use Respect\Validation\Rules\{ AllOf, StringType, Alnum, NoWhitespace, Length, Instance }; /** * @property-read string $login * @property-read string $password * @property-read string $name * @property-read \DateTimeInterface $birthday */ class CreateUserModel extends Struct { public static function getSchema() : Schema { return new Schema( Prop::create('login') ->withValidator(new AllOf( new StringType(), new Alnum(), new NoWhitespace(), new Length(0, 64) )), Prop::create('password') ->withValidator(new AllOf( new StringType(), new Length(0, 256) )), Prop::create('name') ->withValidator(new AllOf( new StringType(), new Length(0, 256) )) ->withMapper(from('name')->trim()->default('John Doe'), 'json') ->notRequired(), Prop::create('birthday') ->withValidator(new Instance(\DateTimeInterface::class)) ->withMapper(from('birthday')->convert(function ($value) { return new \DateTimeImmutable($value); }), 'json') ->notRequired() ); } }
Use model:
<?php declare(strict_types=1); namespace MyNamespace; $json = <<<JSON { "login": "superhacker", "password": "correcthorsebatterystaple", "birthday": "1988-08-08" } JSON; $model = CreateUserModel::mapFrom(json_decode($json), 'json'); echo $model->login; // "superhacker" echo $model->password; // "correcthorsebatterystaple" echo $model->name; // "John Doe" var_export($model->birthday); // DateTime::__set_state(array( // 'date' => '1988-08-08 00:00:00.000000', // 'timezone_type' => 3, // 'timezone' => 'UTC', // ))