Skip to content

Commit e60c325

Browse files
author
Enno Woortmann
committed
extract filter into production lib
1 parent f0ae956 commit e60c325

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"wol-soft/php-json-schema-model-generator-production": "^0.18.0",
14+
"wol-soft/php-json-schema-model-generator-production": "dev-enumPostProcessor",
1515
"wol-soft/php-micro-template": "^1.3.2",
1616

1717
"php": ">=7.2",

src/SchemaProcessor/PostProcessor/EnumFilter.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PHPModelGenerator\SchemaProcessor\PostProcessor;
66

7-
use BackedEnum;
7+
use PHPModelGenerator\Filter\Enum;
88
use PHPModelGenerator\Filter\TransformingFilterInterface;
99

1010
class EnumFilter implements TransformingFilterInterface
@@ -21,22 +21,11 @@ public function getToken(): string
2121

2222
public function getFilter(): array
2323
{
24-
return [self::class, 'filter'];
24+
return [Enum::class, 'filter'];
2525
}
2626

2727
public function getSerializer(): array
2828
{
29-
return [self::class, 'serialize'];
30-
}
31-
32-
// TODO: move to production lib
33-
public static function filter($value, $options): ?BackedEnum
34-
{
35-
return $value === null ? null : $options['fqcn']::from($value);
36-
}
37-
38-
public static function serialize(?BackedEnum $value): null | int | string
39-
{
40-
return $value?->value;
29+
return [Enum::class, 'serialize'];
4130
}
4231
}

src/SchemaProcessor/PostProcessor/EnumPostProcessor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ private function checkForExistingTransformingFilter(PropertyInterface $property)
124124
if ($validator instanceof FilterValidator
125125
&& $validator->getFilter() instanceof TransformingFilterInterface
126126
) {
127-
throw new SchemaException("Can't apply enum filter to an already transformed value");
127+
throw new SchemaException(sprintf(
128+
"Can't apply enum filter to an already transformed value on property %s in file %s",
129+
$property->getName(),
130+
$property->getJsonSchema()->getFile()
131+
));
128132
}
129133
}
130134
}

tests/PostProcessor/EnumPostProcessorTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PHPModelGenerator\Tests\PostProcessor;
66

77
use PHPModelGenerator\Exception\Generic\EnumException;
8+
use PHPModelGenerator\Exception\SchemaException;
89
use PHPModelGenerator\Model\GeneratorConfiguration;
910
use PHPModelGenerator\ModelGenerator;
1011
use PHPModelGenerator\SchemaProcessor\PostProcessor\EnumPostProcessor;
@@ -25,7 +26,10 @@ protected function addPostProcessor(): void
2526
{
2627
$this->modifyModelGenerator = static function (ModelGenerator $generator): void {
2728
$generator->addPostProcessor(
28-
new EnumPostProcessor(join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), 'PHPModelGeneratorTest', 'Enum']), 'Enum')
29+
new EnumPostProcessor(
30+
join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), 'PHPModelGeneratorTest', 'Enum']),
31+
'Enum'
32+
)
2933
);
3034
};
3135
}
@@ -76,4 +80,13 @@ public function testInvalidStringOnlyEnumValue(): void
7680

7781
new $className(['property' => 'Meier']);
7882
}
83+
84+
public function testEnumPropertyWithTransformingFilterThrowsAnException(): void
85+
{
86+
$this->expectException(SchemaException::class);
87+
$this->expectExceptionMessage("Can't apply enum filter to an already transformed value");
88+
89+
$this->addPostProcessor();
90+
$this->generateClassFromFile('EnumPropertyWithTransformingFilter.json');
91+
}
7992
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"property": {
5+
"enum": ["01.01.2000", "01.01.2010"],
6+
"filter": "dateTime"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)