This library can generate immutable data types based on fpp definitions, the syntax is inspired by Haskell.
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);
}
}
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 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
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.
- 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