Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated dependency #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
vendor
node_modules
/composer.lock
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ $schema = $retriever->retrieve('file://' . realpath('schema.json'));

// Generate
$formGenerator = new JsonSchemaForm\Generator($schema);
// Optional supply your twig environment
$formGenerator->setTwig($yourTwigEnvironment);
echo $formGenerator->render();
?>
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"justinrainbow/json-schema": "dev-fix-required-property-validation",
"twig/twig": "1.*"
"twig/twig": "3.3.*"
},
"autoload": {
"psr-0": { "JsonSchemaForm": "src/" }
Expand Down
143 changes: 0 additions & 143 deletions composer.lock

This file was deleted.

2 changes: 1 addition & 1 deletion examples/schema2.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"warehouseLocation": {
"description": "Coordinates of the warehouse with the product",
"$ref": "http://json-schema.org/geo"
"$ref": "http://json-schema.org/learn/examples/geographical-location.schema.json"
}
},
"required": ["id", "name", "price"]
Expand Down
67 changes: 28 additions & 39 deletions src/JsonSchemaForm/ChunkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ abstract class ChunkGenerator {
protected $path;
protected $schema;


public function __construct($generator, $path) {
$this->generator = $generator;
$this->path = $path;
Expand All @@ -21,7 +20,7 @@ public function __construct($generator, $path) {
}
}

$this->schema = \JsonSchemaForm\JsonPath::getSchemaNode(implode('.', $nibbles), $generator->schema);
$this->schema = JsonPath::getSchemaNode(implode('.', $nibbles), $generator->schema);

//last nibble numeric? The items of the Array schema is targeted!
if ($nibbleIsNumeric) {
Expand All @@ -31,10 +30,8 @@ public function __construct($generator, $path) {

abstract public function render($options = array());

/**
* @return string
*/
public function getDomClass() {
public function getDomClass(): string
{
$result = array();

if (!empty($this->path)) {
Expand All @@ -51,12 +48,8 @@ public function getDomClass() {
return implode(' ', $result);
}

/**
* @param string $template
* @param array $options
* @return string
*/
protected function _render($template, array $renderVars) {
protected function _render(string $template, array $renderVars): string
{
foreach (array('id', 'label', 'name', 'value') as $expectedPropertyName) {
if (!isset($renderVars[$expectedPropertyName])) {
$renderVars[$expectedPropertyName] = $this->{'get' . $expectedPropertyName}();
Expand All @@ -66,39 +59,35 @@ protected function _render($template, array $renderVars) {
return $this->generator->twig->render($template, $renderVars);
}

/**
* @return string
*/
private function getLabel() {
if (isset($this->schema->title)) {
return $this->schema->title;
}
if (isset($options['name'])) {
return $options['name'];
}
return '';
}
protected function getDomCompatible($string) {
return preg_replace('![^_a-z0-9-]!i', '-', $string);
}

protected function getValue() {
//traverse path in $this->generator->data;
return JsonPath::getValue(implode('.', $this->path), $this->generator->data);
}

/**
* @return string
*/
private function getId() {
if (!empty($this->path)) {
return $this->getDomCompatible(implode('-', $this->path));
}
return '';
}

private function getName() {
private function getId() {
if (!empty($this->path)) {
return $this->getDomCompatible(implode('-', $this->path));
}
return '';
}

private function getLabel(): string
{
if (isset($this->schema->title)) {
return $this->schema->title;
}
if (isset($options['name'])) {
return $options['name'];
}
return '';
}

private function getName(): string
{
return 'root[' . implode('][', $this->path) . ']';
}

protected function getDomCompatible($string) {
return preg_replace('![^_a-z0-9-]!i', '-', $string);
}
}
3 changes: 2 additions & 1 deletion src/JsonSchemaForm/ChunkGenerator/ArrayField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace JsonSchemaForm\ChunkGenerator;

class ArrayField extends \JsonSchemaForm\ChunkGenerator {
public function render($options = array()) {
public function render($options = array()): string
{
$chunkGeneratorClass = 'JsonSchemaForm\\ChunkGenerator\\' . ucfirst($this->schema->items->type) . 'Field';
$value = $this->getValue();
if (empty($value)) {
Expand Down
3 changes: 2 additions & 1 deletion src/JsonSchemaForm/ChunkGenerator/BooleanField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace JsonSchemaForm\ChunkGenerator;

class BooleanField extends \JsonSchemaForm\ChunkGenerator {
public function render($options = array()) {
public function render($options = array()): string
{
return $this->_render('chunk/boolean.twig', $options);
}
}
11 changes: 6 additions & 5 deletions src/JsonSchemaForm/ChunkGenerator/NumberField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
namespace JsonSchemaForm\ChunkGenerator;

class NumberField extends \JsonSchemaForm\ChunkGenerator {
public function render($options = array()) {
public function render($options = array()): string
{
if (empty($this->schema->enum)) {
$inputType = (isset($this->schema->inputType) ? $this->schema->inputType : 'number');
$inputType = ($this->schema->inputType ?? 'number');
return $this->_render('chunk/' . $inputType . '.twig', $options);
}

$options['options'] = array();
foreach ($this->schema->enum as $enumValue) {
$enumTitles = (isset($this->schema->enumTitles) ? (object) $this->schema->enumTitles : new StdClass());
$enumTitles = (isset($this->schema->enumTitles) ? (object) $this->schema->enumTitles : new \StdClass());
$enumTitleKey = (string) $enumValue;

$options['options'][] = array(
'id' => $this->getDomCompatible(implode(array_merge($this->path, array($enumValue)), '-')),
'label' => (isset($enumTitles->$enumTitleKey) ? $enumTitles->$enumTitleKey: $enumValue),
'label' => ($enumTitles->$enumTitleKey ?? $enumValue),
'value' => $enumValue
);
}

$inputType = (isset($this->schema->inputType) ? $this->schema->inputType : 'select');
$inputType = ($this->schema->inputType ?? 'select');
return $this->_render('chunk/' . $inputType . '.twig', $options);
}
}
5 changes: 3 additions & 2 deletions src/JsonSchemaForm/ChunkGenerator/ObjectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace JsonSchemaForm\ChunkGenerator;

class ObjectField extends \JsonSchemaForm\ChunkGenerator {
public function render($options = array()) {
public function render($options = array()): string
{
foreach ($this->schema->properties as $propertyName => $propertySchema) {
if (!isset($propertySchema->type)) {
continue;
Expand All @@ -21,7 +22,7 @@ public function render($options = array()) {
$fieldGenerator = new $fieldGeneratorClassName($this->generator, $newPath);
$fieldHtmlChunks[$fieldGenerator->getDomClass()] = $fieldGenerator->render();
}
$options['fieldHtmlChunks'] = $fieldHtmlChunks;
$options['fieldHtmlChunks'] = $fieldHtmlChunks ?? null;
return $this->_render('chunk/object.twig', $options);
}
}
11 changes: 6 additions & 5 deletions src/JsonSchemaForm/ChunkGenerator/StringField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace JsonSchemaForm\ChunkGenerator;

class StringField extends \JsonSchemaForm\ChunkGenerator {
public function render($options = array()) {
public function render($options = array()): string
{
if (empty($this->schema->enum)) {
$inputType = (isset($this->schema->inputType) ? $this->schema->inputType : 'input');
$options['type'] = (isset($this->schema->format) ? $this->schema->format : 'text');
$inputType = ($this->schema->inputType ?? 'input');
$options['type'] = ($this->schema->format ?? 'text');
return $this->_render('chunk/' . $inputType . '.twig', $options);
}

Expand All @@ -15,12 +16,12 @@ public function render($options = array()) {
foreach($this->schema->enum as $enumValue) {
$options['options'][] = array(
'id' => $this->getDomCompatible(implode(array_merge($this->path, array($enumValue)), '-')),
'label' => (isset($enumTitles->$enumValue) ? $enumTitles->$enumValue: $enumValue),
'label' => ($enumTitles->$enumValue ?? $enumValue),
'value' => $enumValue
);
}

$inputType = (isset($this->schema->inputType) ? $this->schema->inputType : 'select');
$inputType = ($this->schema->inputType ?? 'select');
return $this->_render('chunk/' . $inputType . '.twig', $options);
}
}