42

GitHub - prolic/fpp: Functional PHP Preprocessor - Generate Immutable Data Types

 6 years ago
source link: https://github.com/prolic/fpp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

FPP

Functional PHP Preprocessor - Immutable data type generator

What it this?

This library can generate immutable data types based on fpp definitions, the syntax is inspired by Haskell.

YouTube Video Tutorial

YouTube Video Tutorial

So what really is it?

Create a file and put this in it:

namespace Model\Foo;

data Person = Person { string $name, ?int $age };

This will generate the following php code:

namespace Model\Foo;
final class Person
{
    private $name;
    private $age;

    public function __construct(string $name, ?int $age)
    {
        $this->name = $name;
        $this->age = $age;
    }

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

    public function age(): ?int
    {
        return $this->age;
    }

    public function withName(string $name): Person
    {
        return new self($name, $this->age);
    }

    public function withAge(?int $age): Person
    {
        return new self($this->name, $age);
    }
}

Enums?

No problem

namespace MyEnum;

data Color = Red | Blue | Green | Yellow deriving (Enum);
$blue = Color::blue();
var_dump($blue->equals(Color::blue())); // true
var_dump($blue->equals(Color::red())); // false

function (MyEnum\Color $color): string
{
    return $color->value();
}

Enums with value mappings

namespace MyEnum;

data Color = Red | Blue deriving (Enum) with (Red:'someThing', Blue: 13);

var_dump(Color::red()->value()); // 'someThing' var_dump(Color::blue()->value()); // 13

Derivings

Derivings are kind of PHP's extends keyword, the following rules apply:

  • It's possible to derive multiple times
  • Some derivings are not compatible to each other (f.e. Command and ToArray cannot be mixed)

There are 14 deriving types for now:

  • AggregateChanged
  • Command
  • DomainEvent
  • Enum
  • Equals
  • FromArray
  • FromScalar
  • FromString
  • Query
  • MicroAggregateChanged (not extending from prooph/eventsourcing, f.e. for prooph/micro)
  • ToArray
  • ToScalar
  • ToString
  • Uuid

Deriving Equals + ToArray

namespace Model\Foo;

data Person = Person { string $name, ?int $age } deriving (ToArray, Equals);

Now you can do this:

$p = new Model\Foo\Person(['name' => 'sasa', 'age' => 36]);
var_dump($p->toArray()); // ['name' => 'sasa', 'age' => 36]
$p->equals($p) // true

Usage

php bin/fpp.php <source dir or file>

It will try to find your composer autoload and fetch psr-4 and psr-0 prefixes from it. You'll get an exception, if you want to dump a class, where you have no composer autoload definition.

Wiki

See the wiki here

Features

  • Create immutable data types with ease
  • Strict types always
  • Generate prooph commands
  • Generate prooph events
  • Generate prooph queries
  • Generate prooph aggregate changed events
  • Ability to switch dumper implementation for custom output
  • Allow composite data objects
  • Allow composite prooph objects
  • Constructor validation
  • Allow creating of custom constructors
  • Dump files according to psr-4 and psr-0 autoloading rules
  • Array notation for objects and scalar types
  • Enum value mappings

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK