An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Struct

[![Build Status](https://travis-ci.org/acelot/struct.svg?branch=master)](https://travis-ci.org/acelot/struct)
[![Code Climate](https://img.shields.io/codeclimate/coverage/acelot/struct.svg)](https://codeclimate.com/github/acelot/struct)
![](https://img.shields.io/badge/dependencies-zero-blue.svg)
![](https://img.shields.io/badge/license-MIT-green.svg)

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',
// ))
```