Coder Social home page Coder Social logo

struct's Introduction

Struct

Build Status Code Climate

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

struct's People

Contributors

acelot avatar sparfenov avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

sparfenov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.