Skip to content

Commit 8d285d1

Browse files
committed
Each generated class implements the JSONModelInterface
1 parent 377111f commit 8d285d1

File tree

6 files changed

+16
-7
lines changed

6 files changed

+16
-7
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 wol-soft
3+
Copyright (c) 2020 wol-soft
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"symplify/easy-coding-standard": "^6.0.4",
15-
"wol-soft/php-json-schema-model-generator-production": "^0.5.0",
15+
"wol-soft/php-json-schema-model-generator-production": "^0.6.0",
1616
"wol-soft/php-micro-template": "^1.3.1",
1717

1818
"php": ">=7.2",

docs/source/gettingStarted.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Now let's have a look at the behaviour of the generated model:
107107
// Exception: 'Value for age must not be smaller than 0'
108108
$person->setAge(-10);
109109
110+
Each generated class will implement the interface **PHPModelGenerator\\Interfaces\\JSONModelInterface** implemented in the php-json-schema-model-generator-production repository and thus provide the method *getRawModelDataInput*.
111+
110112
Configuring the generator
111113
-------------------------
112114

src/Model/Validator/FilterValidator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct(FilterInterface $filter, PropertyInterface $property
4040
? '($value !== null && (!is_' . implode('($value) && !is_', $filter->getAcceptedTypes()) . '($value)))'
4141
: ''
4242
) . sprintf(
43+
// Call the filter, afterwards make sure the condition is false so no exception will be thrown
4344
' || (($value = call_user_func([\%s::class, "%s"], $value)) && false)',
4445
$filter->getFilter()[0],
4546
$filter->getFilter()[1]

src/Templates/Model.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ declare(strict_types = 1);
1616
* Class {{ class }}
1717
{% if namespace %} * @package {{ namespace }} {% endif %}
1818
*/
19-
class {{ class }}
20-
{% if generatorConfiguration.hasSerializationEnabled() %}implements PHPModelGenerator\Interfaces\SerializationInterface{% endif %}
19+
class {{ class }} implements \PHPModelGenerator\Interfaces\JSONModelInterface
20+
{% if generatorConfiguration.hasSerializationEnabled() %}, \PHPModelGenerator\Interfaces\SerializationInterface{% endif %}
2121
{
2222
{% foreach properties as property %}
2323
/** @var {{ property.getTypeHint() }}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %} */

tests/Basic/BasicSchemaGenerationTest.php

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

55
use PHPModelGenerator\Exception\FileSystemException;
66
use PHPModelGenerator\Exception\SchemaException;
7+
use PHPModelGenerator\Interfaces\JSONModelInterface;
78
use PHPModelGenerator\Interfaces\SerializationInterface;
89
use PHPModelGenerator\Model\GeneratorConfiguration;
910
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
@@ -69,21 +70,25 @@ public function testSetterChangeTheInternalState(): void
6970

7071
public function testSerializationFunctionsAreNotGeneratedByDefault(): void
7172
{
72-
$className = $this->generateClassFromFile('BasicSchema.json');
73+
$className = '\\MyApp\\' . $this->generateClassFromFile(
74+
'BasicSchema.json',
75+
(new GeneratorConfiguration())->setNamespacePrefix('\\MyApp\\')
76+
);
7377

7478
$object = new $className(['property' => 'Hello']);
7579

7680
$this->assertFalse(is_callable([$object, 'toArray']));
7781
$this->assertFalse(is_callable([$object, 'toJSON']));
7882

7983
$this->assertFalse($object instanceof SerializationInterface);
84+
$this->assertTrue($object instanceof JSONModelInterface);
8085
}
8186

8287
public function testSerializationFunctionsAreGeneratedWithEnabledSerialization(): void
8388
{
84-
$className = $this->generateClassFromFile(
89+
$className = '\\MyApp\\' . $this->generateClassFromFile(
8590
'BasicSchema.json',
86-
(new GeneratorConfiguration())->setSerialization(true)
91+
(new GeneratorConfiguration())->setSerialization(true)->setNamespacePrefix('\\MyApp\\')
8792
);
8893

8994
$object = new $className(['property' => 'Hello']);
@@ -92,6 +97,7 @@ public function testSerializationFunctionsAreGeneratedWithEnabledSerialization()
9297
$this->assertEquals('{"property":"Hello"}', $object->toJSON());
9398

9499
$this->assertTrue($object instanceof SerializationInterface);
100+
$this->assertTrue($object instanceof JSONModelInterface);
95101
}
96102

97103
/**

0 commit comments

Comments
 (0)