Reputation: 4108
I've been tasked with creating a class for objects defined by a JSON schema. Initially I was just going to build the class based on the schema, but now I've been told it should be automatically generated from the schema itself.
I want the class to have set/get methods for its members.
What's the best way to go about this?
Upvotes: 4
Views: 6886
Reputation: 9558
Please note that the package below is abandoned and no longer maintained. No replacement package was suggested.
I wrote a package for this use case: https://packagist.org/packages/dto/dto
The idea was to be able to define objects as JSON schema so that other services could consume the definitions, and then use those same JSON schemas to define the PHP objects internally.
<?php
class Example extends Dto\Dto
{
protected $schema = [
'type' => 'object',
'properties' => [
'a' => ['type' => 'string'],
'b' => ['type' => 'string']
],
'additionalProperties' => false
];
}
A couple things are tricky with this package (and being strict with types in PHP in general):
if ($dto->my_integer > 3) { // generates an error!
instead, you have to explicitly convert the value to a scalar value (as defined by your JSON Schema definition):
if ($dto->my_integer->toScalar() > 3) { // works
Hope this helps.
Upvotes: 0
Reputation: 4062
Using swaggest/php-code-builder you can generate PHP classes with accessor methods and validation during mapping.
Mapping and validation is powered by swaggest/json-schema, a fast and compatible JSON schema implementation.
Produced classes are IDE-friendly, so you'll have auto completion for all properties.
Upvotes: 4
Reputation: 4254
I think you have a couple of options here.
__call()
to intercept calls to getFoo()
or setBar()
and can perform the necessary validation and return values.In both cases, I think the tricky part will be if you meet reference types along the way. If your schema is fairly simple, though, it shouldn't be too bad.
Good luck!
Upvotes: -1