public static function getSchemaNode($key, $parentSchemaNode) { if (!$key) { //use $container! return $parentSchemaNode; } if ($parentSchemaNode->type === 'array' && $parentSchemaNode->items) { return JsonPath::getSchemaNode($key, $parentSchemaNode->items); } $keyNibbles = explode('.', $key); $keyNibble = array_shift($keyNibbles); if ($parentSchemaNode && $parentSchemaNode->properties && isset($parentSchemaNode->properties->{$keyNibble})) { return JsonPath::getSchemaNode(implode('.', $keyNibbles), $parentSchemaNode->properties->{$keyNibble}); } return null; }
/** * @param array - Any additional options per element for rendering * e.g. array('my.path' => array('inputType' => 'textarea')) * @return string HTML form */ public function render($formRenderOptions = array()) { if (!$this->twig instanceof \Twig_Environment) { $this->setTwig($this->getDefaultTwigEnvironment()); } //update valid with schema $formRenderOptions config if (!empty($formRenderOptions)) { foreach ($formRenderOptions as $path => $config) { $schemaNode = JsonPath::getSchemaNode($path, $this->schema); if ($schemaNode) { foreach ($config as $key => $value) { $schemaNode->{$key} = $value; } } } } $fieldGeneratorClassName = 'JsonSchemaForm\\ChunkGenerator\\' . ucfirst($this->schema->type) . 'Field'; $fieldGenerator = new $fieldGeneratorClassName($this, empty($formRenderOptions['path']) ? array() : $formRenderOptions['path']); return $this->twig->render('form.twig', array('html' => $fieldGenerator->render())); }
public function parse($formData, $schema) { $result = new \StdClass(); foreach ($formData as $key => $value) { $schemaNode = JsonPath::getSchemaNode($key, $schema); if ($schemaNode && !empty($schemaNode->type)) { switch ($schemaNode->type) { case 'object': $parsedValue = $this->parse($value, $schemaNode); break; case 'string': $parsedValue = (string) $value; break; case 'boolean': $parsedValue = (bool) $value; break; case 'number': $parsedValue = (int) $value; break; case 'array': $parsedValue = array(); foreach ($value as $item) { $parsedValue[] = DataParser::parse($item, $schemaNode->items); } break; default: echo 'Datatype not supported by DataParser: ' . $schemaNode->type . PHP_EOL; print_r($value); print_r($schemaNode); exit; } $result->{$key} = $parsedValue; } } return $result; }