https://github.com/acelot/struct
Declarative structure builder for PHP 7
https://github.com/acelot/struct
class dto model strongly-typed struct structure validated value-object
Last synced: 2 months ago
JSON representation
Declarative structure builder for PHP 7
- Host: GitHub
- URL: https://github.com/acelot/struct
- Owner: acelot
- License: mit
- Created: 2018-08-02T05:14:16.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-18T07:34:33.000Z (almost 7 years ago)
- Last Synced: 2025-09-28T13:41:53.277Z (5 months ago)
- Topics: class, dto, model, strongly-typed, struct, structure, validated, value-object
- Language: PHP
- Size: 27.3 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Struct
[](https://travis-ci.org/acelot/struct)
[](https://codeclimate.com/github/acelot/struct)


Declarative structure builder for PHP 7.
## Usage
Create some model:
```php
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
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',
// ))
```