function jsonPath($obj, $expr, $args = null) { $jsonpath = new JsonPath(); $jsonpath->resultType = $args ? $args['resultType'] : "VALUE"; $x = $jsonpath->normalize($expr); $jsonpath->obj = $obj; if ($expr && $obj && ($jsonpath->resultType == "VALUE" || $jsonpath->resultType == "PATH")) { $jsonpath->trace(preg_replace("/^\\\$;/", "", $x), $obj, "\$"); if (count($jsonpath->result)) { return $jsonpath->result; } else { return false; } } }
private function normalizedFirst($expr) { if ($expr == "") { return false; } else { if (preg_match("/^\$(\\[([0-9*]+|'[-a-zA-Z0-9_ ]+')\\])*\$/", $expr)) { print "normalized: " . $expr; return $expr; } else { $res = $this->jsonPath->jsonPath($this->data, $expr, array("resultType" => "PATH")); return $res; } } }
public static function getValue($key, $container) { if ($key === '') { return $container; } $keyNibbles = explode('.', $key); $keyNibble = array_shift($keyNibbles); if ($container) { // Traverse arrays if (is_numeric($keyNibble) && isset($container[$keyNibble])) { return JsonPath::getValue(implode('.', $keyNibbles), $container[$keyNibble]); } if (isset($container->{$keyNibble})) { return JsonPath::getValue(implode('.', $keyNibbles), $container->{$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; }
protected function getValue() { //traverse path in $this->generator->data; return JsonPath::getValue(implode('.', $this->path), $this->generator->data); }