diff --git a/src/Generator/Property/DynamicObjectProperty.php b/src/Generator/Property/DynamicObjectProperty.php new file mode 100644 index 00000000..d6dceceb --- /dev/null +++ b/src/Generator/Property/DynamicObjectProperty.php @@ -0,0 +1,137 @@ +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; + } + +} \ No newline at end of file diff --git a/src/Generator/Property/PropertyInterface.php b/src/Generator/Property/PropertyInterface.php index 784852bc..652e4da3 100644 --- a/src/Generator/Property/PropertyInterface.php +++ b/src/Generator/Property/PropertyInterface.php @@ -27,6 +27,11 @@ public function key(); */ public function isComplex(); + /** + * @return bool + */ + public function isCollection(); + /** * @param string $inputVarName * @return string diff --git a/src/Generator/SchemaToClass.php b/src/Generator/SchemaToClass.php index 490c9b08..03323b9a 100644 --- a/src/Generator/SchemaToClass.php +++ b/src/Generator/SchemaToClass.php @@ -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; @@ -53,6 +54,7 @@ public function schemaToClass(GeneratorRequest $in, OutputInterface $output, Wri StringProperty::class, ArrayProperty::class, IntegerProperty::class, + DynamicObjectProperty::class, NestedObjectProperty::class, MixedProperty::class, ];