Skip to content

Commit a17b40f

Browse files
committed
add enum support, remove mabe enum
1 parent 8fdffbb commit a17b40f

21 files changed

+465
-87
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@
88
.php_cs.cache
99
nbproject
1010
composer.lock
11-

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ echo \Model\Foo\Person\age($p2); // 36
3333
var_dump($p === $p2); // false
3434
```
3535

36+
### Enums?
37+
38+
No problem
39+
40+
```console
41+
namespace MyEnum;
42+
43+
enum Color = Red | Blue | Green | Yellow
44+
```
45+
46+
```php
47+
$blue = Blue();
48+
var_dump($blue->sameAs(Blue())); // true
49+
var_dump($blue->sameAs(Red())); // false
50+
51+
function (Color $color): string
52+
{
53+
return $color->value();
54+
}
55+
```
56+
3657
### Derivings
3758

3859
There are 4 deriving types for now:
@@ -60,17 +81,23 @@ $p->sameAs($p) // true
6081

6182
### Usage
6283

63-
`php bin/fpp.php <source dir> <target file>`
84+
`php bin/fpp.php <source dir or file> <target file>`
6485

6586
### Demo
6687

67-
```
88+
```console
6889
git clone https://github.com/prolic/fpp
6990
cd fpp
7091
composer install
7192
php bin/fpp.php demo demo/generated.php
7293
```
7394

95+
or for a single file:
96+
97+
```console
98+
php bin/enum.php demo demo/generated.php
99+
```
100+
74101
### Features
75102

76103
- [x] Create immutable data types with ease
@@ -80,7 +107,8 @@ php bin/fpp.php demo demo/generated.php
80107
- [x] Generate prooph events
81108
- [x] Generate prooph queries
82109
- [x] Generate prooph aggregate changed events
83-
- [ ] Allow creating of or-types (f.e. data Color = Red | Blue | Green)
110+
- [x] Generate enums
111+
- [ ] Allow creating of custom constructors
84112
- [ ] Show deriving feature
85113
- [ ] Make parser more robust
86114
- [ ] More to come

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
}
99
],
1010
"require": {
11-
"php": "^7.1",
12-
"marc-mabe/php-enum": "^3.0",
13-
"nikic/php-parser": "^3.1.4"
11+
"php": "^7.1"
1412
},
1513
"require-dev": {
1614
"phpunit/phpunit": "^7.0",

demo/enum.fpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace Enum;
2+
3+
enum Color = Red | Blue | Green

src/AggregateChanged.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fpp;
6+
7+
final class AggregateChanged extends Type
8+
{
9+
protected $value = 'AggregateChanged';
10+
}

src/ArrayConverter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fpp;
6+
7+
final class ArrayConverter extends Deriving
8+
{
9+
protected $value = 'ArrayConverter';
10+
}

src/Command.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fpp;
6+
7+
final class Command extends Type
8+
{
9+
protected $value = 'Command';
10+
}

src/Data.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fpp;
6+
7+
final class Data extends Type
8+
{
9+
protected $value = 'Data';
10+
}

src/Definition.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,31 @@ public function __construct(
4141
string $namespace,
4242
string $name,
4343
array $arguments = [],
44-
DerivingSet $derivings = null,
44+
array $derivings = [],
4545
string $messageName = null
4646
) {
4747
if (empty($name)) {
4848
throw new \InvalidArgumentException('Name cannot be empty string');
4949
}
5050

51-
if ($type->is(Type::DATA()) && null !== $messageName) {
51+
if ($type->sameAs(new Data())
52+
&& null !== $messageName
53+
) {
5254
throw new \InvalidArgumentException('Message name cannot be passed to data type');
53-
} elseif (! $type->is(Type::DATA()) && empty($messageName)) {
55+
} elseif (! $type->sameAs(new Data())
56+
&& ! $type->sameAs(new Enum())
57+
&& empty($messageName)
58+
) {
5459
throw new \InvalidArgumentException('Message name cannot be empty string');
5560
}
5661

57-
if (null === $derivings) {
58-
$derivings = new DerivingSet();
62+
if ($type->sameAs(new Enum()) && empty($arguments)) {
63+
throw new \InvalidArgumentException('Enums need at least one implementation');
5964
}
6065

61-
if (count($arguments) > 1 && $derivings->contains(Deriving::STRING_CONVERTER())) {
66+
if (count($arguments) > 1
67+
&& in_array((new StringConverter)->value(), $derivings)
68+
) {
6269
throw new \InvalidArgumentException(sprintf(
6370
'Cannot derive from StringConverter using more than one argument for %s\\%s',
6471
$namespace,
@@ -77,10 +84,15 @@ public function __construct(
7784
if ($argument->name() === $name) {
7885
throw new \InvalidArgumentException('Argument name is not allowed to be same as object name');
7986
}
87+
if ($argument->typehint() !== null
88+
&& $type->sameAs(new Enum())
89+
) {
90+
throw new \InvalidArgumentException('Argument typehint is not allowed for enums');
91+
}
8092
$this->arguments[] = $argument;
8193
}
8294

83-
$this->derivings = $derivings;
95+
$this->derivings = array_unique($derivings);
8496
$this->messageName = $messageName;
8597
}
8698

@@ -116,7 +128,7 @@ public function arguments(): array
116128
return $this->arguments;
117129
}
118130

119-
public function derivings(): DerivingSet
131+
public function derivings(): array
120132
{
121133
return $this->derivings;
122134
}

src/Deriving.php

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,53 @@
44

55
namespace Fpp;
66

7-
use MabeEnum\Enum;
8-
9-
/**
10-
* @method static Deriving SHOW()
11-
* @method static Deriving STRING_CONVERTER()
12-
* @method static Deriving ARRAY_CONVERTER()
13-
* @method static Deriving VALUE_OBJECT()
14-
*/
15-
final class Deriving extends Enum
7+
abstract class Deriving
168
{
17-
const SHOW = 'Show';
18-
const STRING_CONVERTER = 'StringConverter';
19-
const ARRAY_CONVERTER = 'ArrayConverter';
20-
const VALUE_OBJECT = 'ValueObject';
9+
const OPTIONS = [
10+
Show::class,
11+
StringConverter::class,
12+
ArrayConverter::class,
13+
ValueObject::class,
14+
];
15+
16+
const OPTION_VALUES = [
17+
'Show',
18+
'StringConverter',
19+
'ArrayConverter',
20+
'ValueObject',
21+
];
22+
23+
protected $value;
24+
25+
final public function __construct()
26+
{
27+
$valid = false;
28+
29+
foreach(self::OPTIONS as $value) {
30+
if ($this instanceof $value) {
31+
$valid = true;
32+
break;
33+
}
34+
}
35+
36+
if (! $valid) {
37+
$self = get_class($this);
38+
throw new \LogicException("Invalid Deriving '$self' given");
39+
}
40+
}
41+
42+
public function value(): string
43+
{
44+
return $this->value;
45+
}
46+
47+
public function sameAs(Deriving $other): bool
48+
{
49+
return get_class($this) === get_class($other);
50+
}
51+
52+
public function __toString(): string
53+
{
54+
return $this->value;
55+
}
2156
}

0 commit comments

Comments
 (0)