Coder Social home page Coder Social logo

fpp's Introduction

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

fpp's People

Contributors

jeromegamez avatar prolic avatar sevavietl avatar

Watchers

 avatar  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.