Skip to content

Commit fe02fd4

Browse files
committed
Added optional serialization functions
1 parent 2ece5de commit fe02fd4

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Method | Configuration | Default
7171
``` setImmutable(bool $immutable) ``` <br><br>Example:<br> ``` setImmutable(false) ``` | If set to true the generated model classes will be delivered without setter methods for the object properties. | true
7272
``` setCollectErrors(bool $collectErrors) ``` <br><br>Example:<br> ``` setCollectErrors(false) ``` | By default the complete input is validated and in case of failing validations all error messages will be thrown in a single exception. If set to false the first failing validation will throw an exception. | true
7373
``` setPrettyPrint(bool $prettyPrint) ``` <br><br>Example:<br> ``` setPrettyPrint(false) ``` | If set to false, the generated model classes won't follow coding gudelines (but the generation is faster). If enabled the package [Symplify/EasyCodingStandard](https://github.com/Symplify/EasyCodingStandard) will be used to clean up the generated code. | true
74+
``` setSerialization(bool $serialization) ``` <br><br>Example:<br> ``` setSerialization(true) ``` | If set to true the serialization methods `toArray` and `toJSON` will be added to the public interface of the generated classes. | false
7475
``` setOutputEnabled(bool $prettyPrint) ``` <br><br>Example:<br> ``` setOutputEnabled(false) ``` | Enable or disable output of the generation process to STDOUT | true
7576
``` setErrorRegistryClass(string $exceptionClass) ``` <br><br>Example:<br> ``` setErrorRegistryClass(CustomException::class) ``` | Define a custom exception implementing the ErrorRegistryExceptionInterface to decouple the generated code from the library (if you want to declare the library as a dev-dependency). The exception will be thrown if a validation fails error collection is **enabled** | ErrorRegistryException::class
7677
``` setExceptionClass(bool $prettyPrint) ``` <br><br>Example:<br> ``` setExceptionClass(CustomException::class) ``` | Define a custom exception to decouple the generated code from the library (if you want to declare the library as a dev-dependency). The exception will be thrown if a validation fails error collection is **disabled** | ValidationException::class

src/Model/GeneratorConfiguration.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class GeneratorConfiguration
2828
protected $errorRegistryClass = ErrorRegistryException::class;
2929
/** @var string */
3030
protected $exceptionClass = ValidationException::class;
31+
/** @var bool */
32+
protected $serialization = false;
3133

3234
/**
3335
* @return string
@@ -45,6 +47,7 @@ public function getNamespacePrefix(): string
4547
public function setNamespacePrefix(string $namespacePrefix): self
4648
{
4749
$this->namespacePrefix = $namespacePrefix;
50+
4851
return $this;
4952
}
5053

@@ -64,6 +67,7 @@ public function isImmutable(): bool
6467
public function setImmutable(bool $immutable): self
6568
{
6669
$this->immutable = $immutable;
70+
6771
return $this;
6872
}
6973

@@ -83,6 +87,27 @@ public function hasPrettyPrintEnabled(): bool
8387
public function setPrettyPrint(bool $prettyPrint): self
8488
{
8589
$this->prettyPrint = $prettyPrint;
90+
91+
return $this;
92+
}
93+
94+
/**
95+
* @return bool
96+
*/
97+
public function hasSerializationEnabled(): bool
98+
{
99+
return $this->serialization;
100+
}
101+
102+
/**
103+
* @param bool $serialization
104+
*
105+
* @return $this
106+
*/
107+
public function setSerialization(bool $serialization): self
108+
{
109+
$this->serialization = $serialization;
110+
86111
return $this;
87112
}
88113

@@ -94,6 +119,7 @@ public function setPrettyPrint(bool $prettyPrint): self
94119
public function setOutputEnabled(bool $outputEnabled): self
95120
{
96121
$this->outputEnabled = $outputEnabled;
122+
97123
return $this;
98124
}
99125

@@ -121,6 +147,7 @@ public function collectErrors(): bool
121147
public function setCollectErrors(bool $collectErrors): self
122148
{
123149
$this->collectErrors = $collectErrors;
150+
124151
return $this;
125152
}
126153

@@ -140,6 +167,7 @@ public function getErrorRegistryClass(): string
140167
public function setErrorRegistryClass(string $errorRegistryClass): self
141168
{
142169
$this->errorRegistryClass = $errorRegistryClass;
170+
143171
return $this;
144172
}
145173

@@ -159,6 +187,7 @@ public function getExceptionClass(): string
159187
public function setExceptionClass(string $exceptionClass): self
160188
{
161189
$this->exceptionClass = $exceptionClass;
190+
162191
return $this;
163192
}
164193
}

src/Templates/Model.phptpl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,32 @@ class {{ class }}
167167
return $value;
168168
}
169169
{% endforeach %}
170+
171+
{% if generatorConfiguration.hasSerializationEnabled() %}
172+
/**
173+
* Get an array representation of the current state
174+
*
175+
* @return array
176+
*/
177+
public function toArray(): array
178+
{
179+
$modelData = get_object_vars($this);
180+
181+
unset($modelData['rawModelDataInput'], $modelData['errorRegistry'], $modelData['isInitialClass']);
182+
183+
return $modelData;
184+
}
185+
186+
/**
187+
* Get a JSON representation of the current state
188+
*
189+
* @param int $options [optional] Bitmask for json_encode
190+
*
191+
* @return string
192+
*/
193+
public function toJSON(int $options = 0): string
194+
{
195+
return json_encode($this->toArray(), $options);
196+
}
197+
{% endif %}
170198
}

tests/Basic/BasicSchemaGenerationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ public function testSetterChangeTheInternalState(): void
6767
$this->assertSame('NewValue', $object->getProperty());
6868
}
6969

70+
public function testSerializationFunctionsAreNotGeneratedByDefault(): void
71+
{
72+
$className = $this->generateClassFromFile('BasicSchema.json');
73+
74+
$object = new $className(['property' => 'Hello']);
75+
76+
$this->assertFalse(is_callable([$object, 'toArray']));
77+
$this->assertFalse(is_callable([$object, 'toJSON']));
78+
}
79+
80+
public function testSerializationFunctionsAreGeneratedWithEnabledSerialization(): void
81+
{
82+
$className = $this->generateClassFromFile(
83+
'BasicSchema.json',
84+
(new GeneratorConfiguration())->setSerialization(true)
85+
);
86+
87+
$object = new $className(['property' => 'Hello']);
88+
89+
$this->assertEquals(['property' => 'Hello'], $object->toArray());
90+
$this->assertEquals('{"property":"Hello"}', $object->toJSON());
91+
}
92+
7093
/**
7194
* @dataProvider invalidStringPropertyValueProvider
7295
*

0 commit comments

Comments
 (0)