Git Product home page Git Product logo

php-dto's Introduction

PHP-DTO

DTOs for PHP!

DTOs can be used to transform Incoming Psr7 server requests, json and random arrays to typed classes with validation built in.

Does not use reflection and handles everything with good old classes and methods!

Install

composer require rahimi-ali/php-dto

Types

  • int(bool $strict = false): IntType
  • float(bool $strict = false): FloatType
  • string(bool $strict = false): StringType
  • bool(bool $strict = false): BoolType
  • datetime(string $format = DateTimeInterface::ATOM, string|null $timezone = null, bool $immutable = true): DateTimeType
  • embedded(string $class): EmbeddedType
  • dynamicEmbedded(string|Closure $discriminator, array $types = []): DynamicEmbeddedType
  • collection(TypeInterface $type): CollectionType

Rules

  • int(bool $strict = false): IntRule Automatically added when declaring a field with IntType
  • float(bool $strict = false): FloatRule Automatically added when declaring a field with FloatType
  • string(bool $strict = false): StringRule Automatically added when declaring a field with StringType
  • bool(bool $strict = false): BoolRule Automatically added when declaring a field with BoolType
  • array(): ArrayRule Should be an array with sequential int keys
  • object(): ObjectRule Should be an object or an array with string keys or non-sequential int keys
  • min(int $min, bool $strict = false): MinRule
  • max(int $max, bool $strict = false): MaxRule
  • minLength(int $length, bool $strict = false): MinLengthRule
  • maxLength(int $length, bool $strict = false): MaxLengthRule
  • in(array $values, bool $strict = false): InRule
  • notIn(array $values, bool $strict = false): NotInRule
  • equals(mixed $value, bool $strict = false): EqualsRule
  • notEqual(mixed $value, bool $strict = false): NotEqual

Example

class AddressDto extends Dto
{
    private string $city;
    private string $street;
    private int $number;
    
    public static function fields(array $data): array
    {
        return [
            'city' => new Field('city', Type::string(true), rules: [Rule::in(['London', 'Paris', 'New York'])]),
            'street' => new Field('street', Type::string(true), rules: [Rule::min(5)]),
            'number' => new Field('number', Type::int(true), rules: [Rule::min(1), Rule::max(100)]),
        ];
    }
    
    public function getCity(): string
    {
        return $this->city;
    }
    
    public function getStreet(): string
    {
        return $this->street;
    }
    
    public function getNumber(): int
    {
        return $this->number;
    }
}

class UserDto extends Dto
{
    private string $name;
    private int $age;
    private AddressDto $address;
    
    public static function fields(array $data): array
    {
        return [
            'name' => new Field('name', Type::string(true), rules: [Rule::min(5)]),
            'age' => new Field('age', Type::int(true), rules: [Rule::min(18)]),
            'address' => new Field('address', Type::embedded(AddressDto::class)),
        ];
    }
    
    public function getName(): string
    {
        return $this->name;
    }
    
    public function getAge(): int
    {
        return $this->age;
    }
    
    public function getAddress(): AddressDto
    {
        return $this->address;
    }
}

Notes

  • Because reflection and magic methods were not used private and readonly properties cannot be used because the parent class cannot access them by default. If you really want private and readonly properties, override the setProperty method in you DTO:
protected function setProperty(string $property, mixed $value): void
{
    $this->{$property} = $value;
}

Standards

  • Code Coverage: 100%
  • PHPStan Level: 9
  • Infection MSI: 94%

License

MIT

php-dto's People

Contributors

rahimi-ali avatar

Stargazers

alireza abdolmaleki avatar Farzin Firoozi avatar Amir Reza Mehrbakhsh avatar

Watchers

 avatar

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.