โญ
howyi / json-schema-mapper
JSON Schema Object|Enum to PHP classes
json-schema-mapper
JSON Schema Object|Enum to PHP classes
document: https://howyi.gitbooks.io/json-schema-mapper/
composer require howyi/json-schema-mapper
./vendor/bin/jsm map [jsonDir] [phpDir] [namespace] [templatePath]
motivation
I am tired of making a PHP class that matches Json-schema anymore ๐ต
install
$ composer require howyi/json-schema-mapper
usage
Specify the target JSON directory and the map (PHP) directory and namespace and execute
$ ./vendor/bin/jsm map [jsonDir] [phpDir] [namespace] [templatePath]
description
A library that generates PHP classes that can be acquired from JSON Schema with \json_encode()
Convert JSON Schema type to PHP type as much as possible and do type hint
When "type is string and format is date-time", the type hint becomes \ DateTimeInterface
and when converting the array automatically performs ->format(\ DateTime::RFC3339)
The object specified by $ ref is converted to a type hint to the mapped interface
Enum is generated alone as an Enum class that inherits Eloquent\AbstractEnumration
example
directory
JSON dir
sample/json/
โ neta.json
โ osakanaType.json
โ shari.json
โ sushi.json
Converted PHP dir
sample/generated/
โ Neta/
โ โ Neta.php
โ โ NetaInterface.php
โ โ NetaTrait.php
โ Shari/
โ โ Shari.php
โ โ ShariInterface.php
โ โ ShariTrait.php
โ Sushi/
โ โ Sushi.php
โ โ SushiInterface.php
โ โ SushiTrait.php
โ OsakanaType.php
file
JSON (sushi.json)
{
"title": "ๅฏฟๅธๆ
ๅ ฑ",
"type": "object",
"properties": {
"sushiId": {
"title": "ๅฏฟๅธID",
"type": "integer"
},
"sushiName": {
"title": "ๅฏฟๅธใฎๅๅ",
"type": "string"
},
"eatable": {
"title": "้ฃในใใใใ",
"type": "boolean"
},
"length": {
"title": "ๅฏฟๅธใฎ้ทใ",
"type": "number"
},
"osakanaType": {
"title": "ใ้ญใฟใคใ",
"$ref": "osakanaType.json"
},
"neta": {
"title": "ใใฟ",
"$ref": "./neta.json"
},
"shari": {
"title": "ใทใฃใช",
"$ref": "./shari.json"
},
"expirationDate": {
"title": "ๆถ่ฒปๆ้",
"type": "string",
"format": "date-time"
}
},
"required": [
"sushiId",
"sushiName",
"eatable",
"osakanaType",
"neta",
"shari",
"expirationDate"
],
"additionalProperties": true
}
Converted PHP(Class)
<?php
namespace Json\Sushi;
use Json\OsakanaType;
use Json\Neta\NetaInterface;
use Json\Shari\ShariInterface;
class Sushi implements SushiInterface
{
use SushiTrait;
protected $sushiId;
protected $sushiName;
protected $eatable;
protected $length;
protected $osakanaType;
protected $neta;
protected $shari;
protected $expirationDate;
protected $additionalProperties;
public function __construct(
int $sushiId,
string $sushiName,
bool $eatable,
?float $length,
OsakanaType $osakanaType,
NetaInterface $neta,
ShariInterface $shari,
\DateTimeInterface $expirationDate,
array $additionalProperties = []
) {
$this->sushiId = $sushiId;
$this->sushiName = $sushiName;
$this->eatable = $eatable;
$this->length = $length;
$this->osakanaType = $osakanaType;
$this->neta = $neta;
$this->shari = $shari;
$this->expirationDate = $expirationDate;
$this->additionalProperties = $additionalProperties;
}
public function sushiId(): int
{
return $this->sushiId;
}
public function sushiName(): string
{
return $this->sushiName;
}
public function eatable(): bool
{
return $this->eatable;
}
public function length(): ?float
{
return $this->length;
}
public function osakanaType(): OsakanaType
{
return $this->osakanaType;
}
public function neta(): NetaInterface
{
return $this->neta;
}
public function shari(): ShariInterface
{
return $this->shari;
}
public function expirationDate(): \DateTimeInterface
{
return $this->expirationDate;
}
public function additionalProperties(): array
{
return $this->additionalProperties;
}
}
Converted PHP(Trait)
<?php
namespace Json\Sushi;
trait SushiTrait
{
use \JsonSchemaMapper\ObjectTrait;
public function jsonProperties(): array
{
return [
'sushiId',
'sushiName',
'eatable',
'length',
'osakanaType',
'neta',
'shari',
'expirationDate',
];
}
public function allowAdditionalProperties(): bool
{
return true;
}
}
Converted PHP(Interface)
<?php
namespace Json\Sushi;
use JsonSchemaMapper\JsonArrayAccess;
use Json\Neta\NetaInterface;
use Json\OsakanaType;
use Json\Shari\ShariInterface;
interface SushiInterface extends JsonArrayAccess, \JsonSerializable
{
public function toJsonArray(): array;
public function jsonProperties(): array;
public function allowAdditionalProperties(): bool;
public function sushiId(): int;
public function sushiName(): string;
public function eatable(): bool;
public function length(): ?float;
public function osakanaType(): OsakanaType;
public function neta(): NetaInterface;
public function shari(): ShariInterface;
public function expirationDate(): \DateTimeInterface;
public function additionalProperties(): array;
}
Top comments (0)