Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions src/Generator/Property/DynamicObjectProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php
namespace Helmich\Schema2Class\Generator\Property;

use Helmich\Schema2Class\Generator\SchemaToClass;

class DynamicObjectProperty extends AbstractPropertyInterface
{
use TypeConvert;

public static function canHandleSchema(array $schema)
{
return isset($schema["type"]) && $schema["type"] === "object" ||
(isset($schema["patternProperties"]) || isset($schema["additionalProperties"]));
}

public function isComplex()
{
return true;
}

public function convertJSONToType($inputVarName = 'input')
{
$key = $this->key;
$code = "\$$key = [];\n" .
"foreach (\$input['$key'] as \$k => \$v) {\n";

$i = 1;
$j = 0;

if (isset($this->schema["patternProperties"])) {
foreach ($this->schema["patternProperties"] as $k => $subDef) {
if ((isset($subDef["type"]) && $subDef["type"] === "object") || isset($subDef["properties"])) {

$type = $this->subTypeName() . "Pattern" . $i;
$pattern = var_export($k, true);

$code .= " " . ($j > 0 ? "else " : "") . "if (preg_match($pattern, \$k)) {\n" .
" \${$key}[\$k] = {$type}::buildFromInput(\$v);\n" .
" }\n";

$j++;
}

$i++;
}
}

if (isset($this->schema["additionalProperties"])) {
$subDef = $this->schema["additionalProperties"];
if ((isset($subDef["type"]) && $subDef["type"] === "object") || isset($subDef["properties"])) {
$type = $this->subTypeName() . "Additional";

if ($j > 0) {
$code .= " else {\n" .
" \${$key}[\$k] = {$type}::buildFromInput(\$v);\n" .
" }";
} else {
$code .= " \${$key}[\$k] = {$type}::buildFromInput(\$v);\n";
}
}
}

$code .= "}";

return $code;
}

public function convertTypeToJSON($outputVarName = 'output')
{
$key = $this->key;

return "\${$outputVarName}['$key'] = [];\n" .
"foreach (\$this->$key as \$k => \$v) {\n" .
" \${$outputVarName}['$key'][\$k] = \$v->toJson();\n" .
"}";
}

public function cloneProperty()
{
$key = $this->key;

return "\$this->$key = [];\n" .
"foreach (\$this->$key as \$k => \$v) {\n" .
" \$this->{$key}[\$k] = clone \$v;\n" .
"}";
}

/**
* @param SchemaToClass $generator
* @throws \Helmich\Schema2Class\Generator\GeneratorException
*/
public function generateSubTypes(SchemaToClass $generator)
{
$i = 1;

if (isset($this->schema["patternProperties"])) {
foreach ($this->schema["patternProperties"] as $k => $subDef) {
if ((isset($subDef["type"]) && $subDef["type"] === "object") || isset($subDef["properties"])) {
$req = $this->ctx->request
->withSchema($subDef)
->withClass($this->subTypeName() . "Pattern" . $i);

$generator->schemaToClass($req, $this->ctx->output, $this->ctx->writer);
}

$i ++;
}
}

if (isset($this->schema["additionalProperties"])) {
$subDef = $this->schema["additionalProperties"];
if ((isset($subDef["type"]) && $subDef["type"] === "object") || isset($subDef["properties"])) {
$req = $this->ctx->request
->withSchema($subDef)
->withClass($this->subTypeName($i) . "Additional");

$generator->schemaToClass($req, $this->ctx->output, $this->ctx->writer);
}
}
}

public function typeAnnotation()
{
return "array";
}

public function typeHint($phpVersion)
{
return "array";
}

private function subTypeName($idx = 0)
{
return $this->ctx->request->targetClass . $this->capitalizedName;
}

}
5 changes: 5 additions & 0 deletions src/Generator/Property/PropertyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function key();
*/
public function isComplex();

/**
* @return bool
*/
public function isCollection();

/**
* @param string $inputVarName
* @return string
Expand Down
2 changes: 2 additions & 0 deletions src/Generator/SchemaToClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Helmich\Schema2Class\Generator\Property\ArrayProperty;
use Helmich\Schema2Class\Generator\Property\DateProperty;
use Helmich\Schema2Class\Generator\Property\DynamicObjectProperty;
use Helmich\Schema2Class\Generator\Property\IntegerProperty;
use Helmich\Schema2Class\Generator\Property\IntersectProperty;
use Helmich\Schema2Class\Generator\Property\MixedProperty;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function schemaToClass(GeneratorRequest $in, OutputInterface $output, Wri
StringProperty::class,
ArrayProperty::class,
IntegerProperty::class,
DynamicObjectProperty::class,
NestedObjectProperty::class,
MixedProperty::class,
];
Expand Down