Skip to content

Commit 7e8ffce

Browse files
committed
add docs and tests
1 parent aa07276 commit 7e8ffce

File tree

16 files changed

+925
-655
lines changed

16 files changed

+925
-655
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The rewrite of this library is heavily inspired by it and reuses some of its bas
1818

1919
## Docs
2020

21-
Coming soon...
21+
[See the docs here](https://github.com/prolic/fpp/tree/master/docs/Home.md)
2222

2323
## Install
2424

docs/Conditions.md

Lines changed: 0 additions & 152 deletions
This file was deleted.

docs/Configuration.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Functional PHP Preprocessor - Immutable data type generator
2+
3+
## Configuration
4+
5+
So far we used FPP without any custom configuration, that's because
6+
FPP ships with a default build-in configuration. If you need to configure
7+
FPP, first you need to generate the default config file, so you can than
8+
adjust it to your needs:
9+
10+
```bash
11+
./vendor/bin/fpp --gen-config
12+
```
13+
14+
This will create a file `fpp-config.php` in your project's root folder.
15+
16+
The first few options are pretty self-explanatory, but we'll go over it anyway:
17+
18+
- `use_strict_types` whether or not to use strict types in the generated PHP code
19+
- `printer` a callback that creates a Printer for FPP to use. Usually the PSR
20+
Printer is exactly what you want, so you most likely won't change this ever.
21+
In case you are working with Nette Framework (we use their PhpGenerator component),
22+
you might want to use the `Nette\PhpGenerator\Printer` class instead. As FPP
23+
doesn't have any type hints on the printer used, you could also use your very
24+
own printer implementation here.
25+
- `file_parser` defines the function that will parse files for you. Unless you know
26+
the internals of FPP and want to mess around a little, you would never touch that
27+
at all.
28+
- `comment` defines the comment that is added on top of every generated file.
29+
You can put any string here or set to `null` to disable comments.
30+
- `types` is the list of available types in FPP as well as the `DateTimeImmutable`
31+
class. That's because FPP can use already `DateTimeImmutable`, as long as you
32+
import that class.
33+
34+
## Custom type configuration
35+
36+
Maybe the easiest way to learn how to configure FPP is by copy & paste the example
37+
of `DateTimeImmutable`, add to the configuration and adjust. However, let's go
38+
through the various steps quickly.
39+
40+
In the `types` section of the configuration, you need to provide a class name in
41+
the key of the config and a `TypeConfiguration` as value. The `TypeConfiguration`
42+
class has the following constructor:
43+
44+
```
45+
public function __construct(
46+
?callable $parse,
47+
?callable $build,
48+
?callable $fromPhpValue,
49+
?callable $toPhpValue,
50+
?callable $validator,
51+
?callable $validationErrorMessage,
52+
?callable $equals
53+
)
54+
```
55+
56+
As you can see, it expects a bunch of callable but all of them are optional.
57+
58+
- `parse` defines the parse function to use in order to parse the given type.
59+
When importing already existing classes, you would provide `null` here.
60+
In case you want to add your very own FPP type, you need to provide the parser
61+
function here.
62+
- `build` defines the function that builds the PHP code that is then printed by
63+
the printer defined. Again, even when we are using the `Nette\PhpGenerator`,
64+
there are no type hints at all. So if you replace all the default builders
65+
shipped with FPP and provide a special printer, you can use FPP to even generate
66+
code in any other language, for example JavaScript.
67+
- `fromPhpValue` defines the function that will be used to transform a scalar or
68+
array PHP value to an object. If there is no function provided, the object will
69+
be required as is in the generated `fromArray` method.
70+
- `toPhpValue` defines the function that will be used to transform your object to
71+
a scalar or array value in PHP. If there is no function provided, the object
72+
will be returned as is in the generated `toArray` method.
73+
- `validator` defines the function that will be used to validate a given PHP scalar
74+
or array value. This will be used in the generated `fromArray` method. If left to
75+
`null` the value will be be validated at all.
76+
- `validationErrorMessage` is used to display the error message in the `fromArray`
77+
method, when the given `validator` fails.
78+
- `equals` defines how to compare of two of those objects are equal.
79+
80+
Let's have an example here real quick:
81+
82+
```
83+
Role::class => new TypeConfiguration(
84+
null,
85+
null,
86+
fn (string $type, string $paramName) => "$type::fromName($$paramName)",
87+
fn (string $paramName) => $paramName . '->getName()',
88+
fn (string $paramName) => "\is_string(\$$paramName)",
89+
fn (string $paramName) => "Error on \"$paramName\", string expected",
90+
fn (string $paramName, string $otherParamName) => "{$paramName}->equals($otherParamName)"
91+
),
92+
```
93+
94+
So far for the configuration, let's head to [Messages](Messages.md) next.

0 commit comments

Comments
 (0)