diff --git a/src/Form/Util/JsonConverter.php b/src/Form/Util/JsonConverter.php index 1206a9a..409b5da 100644 --- a/src/Form/Util/JsonConverter.php +++ b/src/Form/Util/JsonConverter.php @@ -46,7 +46,7 @@ public static function toStdClass(array $data): \stdClass { return new \stdClass(); } - $result = \json_decode(\json_encode($data, JSON_THROW_ON_ERROR)); + $result = \json_decode(\json_encode($data, JSON_THROW_ON_ERROR), FALSE, 512, JSON_THROW_ON_ERROR); Assertion::isInstanceOf($result, \stdClass::class); return $result; diff --git a/src/JsonForms/Definition/Control/ControlDefinition.php b/src/JsonForms/Definition/Control/ControlDefinition.php index c730641..44f9e8f 100644 --- a/src/JsonForms/Definition/Control/ControlDefinition.php +++ b/src/JsonForms/Definition/Control/ControlDefinition.php @@ -159,6 +159,13 @@ public function getEnum(): ?array { return $this->propertySchema->enum ?? NULL; } + /** + * {@inheritDoc} + */ + public function getKeywordValue(string $keyword, $default = NULL) { + return $this->uiSchema->{$keyword} ?? $default; + } + public function getLabel(): string { return $this->controlSchema->label ?? $this->propertySchema->title ?? ucfirst($this->getPropertyName()); } @@ -319,6 +326,9 @@ public function isRequired(): bool { return in_array($this->getPropertyName(), $this->objectSchema->required ?? [], TRUE); } + /** + * {@inheritDoc} + */ public function withScopePrefix(string $scopePrefix): DefinitionInterface { if (NULL !== $this->getScopePrefix()) { $scopePrefix = $this->getScopePrefix() . ltrim($scopePrefix, '#'); diff --git a/src/JsonForms/Definition/Custom/CustomDefinition.php b/src/JsonForms/Definition/Custom/CustomDefinition.php new file mode 100644 index 0000000..988a37d --- /dev/null +++ b/src/JsonForms/Definition/Custom/CustomDefinition.php @@ -0,0 +1,78 @@ +. + */ + +declare(strict_types=1); + +namespace Drupal\json_forms\JsonForms\Definition\Custom; + +use Drupal\json_forms\JsonForms\Definition\DefinitionInterface; + +/** + * Used for unknown UI schema types. + * + * @codeCoverageIgnore + */ +final class CustomDefinition implements DefinitionInterface { + + private \stdClass $uiSchema; + + private \stdClass $jsonSchema; + + private bool $parentReadonly; + + public function __construct(\stdClass $uiSchema, \stdClass $jsonSchema, bool $parentReadonly) { + $this->uiSchema = $uiSchema; + $this->jsonSchema = $jsonSchema; + $this->parentReadonly = $parentReadonly; + } + + /** + * {@inheritDoc} + */ + public function getKeywordValue(string $keyword, $default = NULL) { + return $this->uiSchema->{$keyword} ?? $default; + } + + public function getJsonSchema(): \stdClass { + return $this->jsonSchema; + } + + public function getUiSchema(): \stdClass { + return $this->uiSchema; + } + + public function getRule(): ?\stdClass { + return $this->uiSchema->rule ?? NULL; + } + + public function getType(): string { + return $this->uiSchema->type; + } + + public function isParentReadonly(): bool { + return $this->parentReadonly; + } + + /** + * {@inheritDoc} + */ + public function withScopePrefix(string $scopePrefix): DefinitionInterface { + return new static($this->uiSchema, $this->jsonSchema, $this->parentReadonly); + } + +} diff --git a/src/JsonForms/Definition/Markup/MarkupDefinition.php b/src/JsonForms/Definition/Custom/MarkupDefinition.php similarity index 96% rename from src/JsonForms/Definition/Markup/MarkupDefinition.php rename to src/JsonForms/Definition/Custom/MarkupDefinition.php index e5be14c..dfabca5 100644 --- a/src/JsonForms/Definition/Markup/MarkupDefinition.php +++ b/src/JsonForms/Definition/Custom/MarkupDefinition.php @@ -50,10 +50,7 @@ public function getContentMediaType(): string { } /** - * @param string $keyword - * @param mixed $default - * - * @return mixed + * {@inheritDoc} */ public function getKeywordValue(string $keyword, $default = NULL) { return $this->markupSchema->{$keyword} ?? $default; @@ -75,6 +72,9 @@ public function getType(): string { return $this->markupSchema->type; } + /** + * {@inheritDoc} + */ public function withScopePrefix(string $scopePrefix): DefinitionInterface { return new static($this->markupSchema); } diff --git a/src/JsonForms/Definition/DefinitionFactory.php b/src/JsonForms/Definition/DefinitionFactory.php index 345646b..7c66002 100644 --- a/src/JsonForms/Definition/DefinitionFactory.php +++ b/src/JsonForms/Definition/DefinitionFactory.php @@ -22,6 +22,7 @@ namespace Drupal\json_forms\JsonForms\Definition; use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition; +use Drupal\json_forms\JsonForms\Definition\Custom\CustomDefinition; use Drupal\json_forms\JsonForms\Definition\Layout\LayoutDefinition; use Drupal\json_forms\JsonForms\Definition\Markup\MarkupDefinition; @@ -38,7 +39,8 @@ public static function createDefinition( if ('Control' === $uiSchema->type) { return ControlDefinition::fromJsonSchema($uiSchema, $jsonSchema, $parentUiReadonly); } - elseif ('Markup' === $uiSchema->type) { + + if ('Markup' === $uiSchema->type) { return new MarkupDefinition($uiSchema); } @@ -46,7 +48,7 @@ public static function createDefinition( return new LayoutDefinition($uiSchema, $jsonSchema, $parentUiReadonly); } - throw new \InvalidArgumentException(sprintf('Invalid type "%s"', $uiSchema->type)); + return new CustomDefinition($uiSchema, $jsonSchema, $parentUiReadonly); } } diff --git a/src/JsonForms/Definition/DefinitionInterface.php b/src/JsonForms/Definition/DefinitionInterface.php index 3db7688..e898295 100644 --- a/src/JsonForms/Definition/DefinitionInterface.php +++ b/src/JsonForms/Definition/DefinitionInterface.php @@ -23,12 +23,22 @@ interface DefinitionInterface { + /** + * @param mixed $default + * + * @return mixed + * The value for the given keyword in the UI schema or $default if not set. + */ + public function getKeywordValue(string $keyword, $default = NULL); + + public function getRule(): ?\stdClass; + public function getType(): string; /** - * @param string $scopePrefix - * * @return static + * A new definition where the given scope prefix is prepended to the scopes + * of all Controls. */ public function withScopePrefix(string $scopePrefix): self; diff --git a/src/JsonForms/Definition/Layout/LayoutDefinition.php b/src/JsonForms/Definition/Layout/LayoutDefinition.php index 4c0549e..61d19e5 100644 --- a/src/JsonForms/Definition/Layout/LayoutDefinition.php +++ b/src/JsonForms/Definition/Layout/LayoutDefinition.php @@ -58,10 +58,7 @@ public function getLayoutSchema(): \stdClass { } /** - * @param string $keyword - * @param mixed $default - * - * @return mixed + * {@inheritDoc} */ public function getKeywordValue(string $keyword, $default = NULL) { return $this->layoutSchema->{$keyword} ?? $default; @@ -93,6 +90,9 @@ public function getOptionsValue(string $key, $default = NULL) { return $this->layoutSchema->options->{$key} ?? $default; } + /** + * {@inheritDoc} + */ public function withScopePrefix(string $scopePrefix): DefinitionInterface { $definition = (new \ReflectionClass($this))->newInstanceWithoutConstructor(); $definition->layoutSchema = $this->layoutSchema;