Immutable, Statically-typed, Cloneable and Serializable Auto-generated Plain-old PHP Objects
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Run PHPUnit from vendor Feb 4, 2019
bin Add bin triggers and example Feb 4, 2019
example Add bin triggers and example Feb 4, 2019
src Allow default values Feb 4, 2019
tests
.editorconfig Add bin triggers and example Feb 4, 2019
.gitignore Remove generated test file (#2) Feb 4, 2019
LICENSE Initial commit Feb 3, 2019
README.md Update README.md Feb 4, 2019
composer.json Proper SPDX License Feb 4, 2019
composer.lock Update README Feb 4, 2019
ippo
phpunit.xml.dist Add PHPUnit config file and upgrade CI PHP version Feb 4, 2019

README.md

I.P.P.O CircleCI

  • Immutable - Uses withs instead of setters
  • Statically-typed - Your tooling loves it
  • Cloneable - No reference sharing
  • Serializable - To JSON, to Array and to String

― Auto-generated Plain-old PHP Objects.

Usage

A definition file looks like:

namespace: App

definitions:
  - User:
    id: int
    name: string
    email: string
    isAdmin: [bool, 'false']
    birthDate: [?\DateTime, 'null']

It's self explanatory, it declares an User class on the App namespace with member: type or member: [type, default]. Simple like that.

You're encoraged to install it as a development dependency in your project:

$ composer install --dev leocavalcante/ippo dev-master

It will place a binary file at vendor/bin that requires only two arguments: the definition file and the output directory.

$ vendor/bin/ippo definitions.yml src/generated/

Output examples

From the definitions above, you get an User class:

class User implements \JsonSerializable

With a construtor like:

public function __construct(
    int $id,
    string $name,
    string $email,
    bool $isAdmin = false,
    ?\DateTime $birthDate = null
) {
    $this->id = $id;
    $this->name = $name;
    $this->email = $email;
    $this->isAdmin = $isAdmin;
    $this->birthDate = $birthDate;
}

Getters and withs for each declared attribute, like:

public function getName(): string
{
    return $this->name;
}

public function withName(string $name): User
{
    return new User(
        $this->id,
        $name,
        $this->email,
        $this->isAdmin,
        $this->birthDate
    );
}

And serialization methods like toArray:

public function toArray(): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'is_admin' => $this->isAdmin,
        'birth_date' => $this->birthDate,
    ];
}

Or __toString():

public function __toString()
{
    $id = json_encode($this->id);
    $name = json_encode($this->name);
    $email = json_encode($this->email);
    $isAdmin = json_encode($this->isAdmin);
    $birthDate = json_encode($this->birthDate);

    return "User(\n\tid => {$id};\n\tname => {$name};\n\temail => {$email};\n\tisAdmin => {$isAdmin};\n\tbirthDate => {$birthDate};\n)";
}

Check out the complete output here