Skip to content

Commit a2dfc79

Browse files
committed
Fix error importing builtin types for transforming filters
1 parent 07c725e commit a2dfc79

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

docs/source/nonStandardExtensions/filter.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Transforming filter
8080

8181
You may keep it simple and skip this for your first tries and only experiment with non-transforming filters like the trim filter
8282

83-
Filters may change the type of the property. For example the builtin filter **dateTime** creates a DateTime object. Consequently further type-related validations like pattern checks for the string property won't be performed. Additionally enum validations will not be executed if an already transformed value is provided.
83+
Filters may change the type of the property. For example the builtin filter **dateTime** creates a DateTime object. Consequently further type-related validations like pattern checks for the string property won't be performed. If you use a transforming filter which transforms the value into another accepted type (eg. your property accepts ['string', 'integer'] and your transforming filter transforms provided strings into integers) the additional provided validators for integers (like minimum or maximum checks) will be executed (only if your property accepts integer values; if the property only accepts strings and the transforming filter converts them to integer values integer validators won't be added to the property). Additionally enum validations will not be executed if an already transformed value is provided.
8484

8585
As the required check is executed before the filter a filter may transform a required value into a null value. Be aware when writing custom filters which transform values to not break your validation rules by adding filters to a property.
8686

@@ -393,7 +393,7 @@ Custom transforming filter
393393

394394
If you want to provide a custom filter which transforms a value (eg. redirect data into a manually written model, transforming between data types [eg. accepting values as an integer but handle them internally as binary strings]) you must implement the **PHPModelGenerator\\PropertyProcessor\\Filter\\TransformingFilterInterface**. This interface adds the **getSerializer** method to your filter. The method is similar to the **getFilter** method. It must return a callable which is available during the render process as well as during code execution. The returned callable must return null or a string and undo a transformation (eg. the serializer method of the builtin **dateTime** filter transforms a DateTime object back into a formatted string). The serializer method will be called with the current value of the property as the first argument and with the (optionally provided) additional options of the filter as the second argument. Your custom transforming filter might look like:
395395

396-
The custom serializer method will be called if the model utilizing the custom filter is generated with `serialization methods<../gettingStarted.html#serialization-methods>`__ and *toArray* or *toJSON* is called.
396+
The custom serializer method will be called if the model utilizing the custom filter is generated with `serialization methods <../gettingStarted.html#serialization-methods>`__ and *toArray* or *toJSON* is called.
397397

398398
.. code-block:: php
399399

src/PropertyProcessor/Filter/FilterProcessor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ public function process(
9797
(new RenderHelper($generatorConfiguration))->getSimpleClassName($typeAfterFilter->getName())
9898
);
9999

100-
$schema
101-
->addUsedClass($typeAfterFilter->getName())
102-
->addCustomSerializer(
100+
if (!$typeAfterFilter->isBuiltin()) {
101+
$schema->addUsedClass($typeAfterFilter->getName());
102+
}
103+
104+
$schema->addCustomSerializer(
103105
$property->getAttribute(),
104106
new TransformingFilterSerializer($property->getAttribute(), $filter, $filterOptions)
105107
);

tests/Basic/FilterTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ public function testTransformingToScalarType()
571571
$className = $this->generateClassFromFile(
572572
'TransformingScalarFilter.json',
573573
(new GeneratorConfiguration())
574+
->setNamespacePrefix('\MyApp\Model')
574575
->setSerialization(true)
575576
->setImmutable(false)
576577
->addFilter(
@@ -583,7 +584,8 @@ public function testTransformingToScalarType()
583584
)
584585
);
585586

586-
$object = new $className(['value' => 9]);
587+
$fqcn = "\\MyApp\\Model\\$className";
588+
$object = new $fqcn(['value' => 9]);
587589

588590
$this->assertSame('1001', $object->getValue());
589591
$this->assertSame('1010', $object->setValue('1010')->getValue());

0 commit comments

Comments
 (0)