diff --git a/src/JsonForms/Definition/Control/ArrayControlDefinition.php b/src/JsonForms/Definition/Control/ArrayControlDefinition.php index 414b8f2..06494fc 100644 --- a/src/JsonForms/Definition/Control/ArrayControlDefinition.php +++ b/src/JsonForms/Definition/Control/ArrayControlDefinition.php @@ -21,6 +21,8 @@ namespace Drupal\json_forms\JsonForms\Definition\Control; +use Drupal\json_forms\Util\ConvertUtil; + final class ArrayControlDefinition extends ControlDefinition { public function getItems(): ?\stdClass { @@ -35,15 +37,15 @@ public function getPrefixItems(): ?array { } public function getMaxItems(): ?int { - return $this->propertySchema->maxItems ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->maxItems ?? NULL); } public function getMinItems(): ?int { - return $this->propertySchema->minItems ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->minItems ?? NULL); } public function getUniqueItems(): ?bool { - return $this->propertySchema->uniqueItems ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->uniqueItems ?? NULL); } public function getContains(): ?\stdClass { @@ -51,11 +53,11 @@ public function getContains(): ?\stdClass { } public function getMaxContains(): ?int { - return $this->propertySchema->maxContains ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->maxContains ?? NULL); } public function getMinContains(): ?int { - return $this->propertySchema->minContains ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->minContains ?? NULL); } } diff --git a/src/JsonForms/Definition/Control/NumberControlDefinition.php b/src/JsonForms/Definition/Control/NumberControlDefinition.php index 2bfe181..e3d7bd9 100644 --- a/src/JsonForms/Definition/Control/NumberControlDefinition.php +++ b/src/JsonForms/Definition/Control/NumberControlDefinition.php @@ -21,33 +21,35 @@ namespace Drupal\json_forms\JsonForms\Definition\Control; +use Drupal\json_forms\Util\ConvertUtil; + class NumberControlDefinition extends ControlDefinition { public function getMultipleOf(): ?int { - return $this->propertySchema->multipleOf ?? NULL; + return ConvertUtil::stdClassToNull($this->getpropertySchema->multipleOf ?? NULL); } public function getMaximum(): ?int { - return $this->propertySchema->maximum ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->maximum ?? NULL); } public function getExclusiveMaximum(): ?int { - return $this->propertySchema->exclusiveMaximum ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->exclusiveMaximum ?? NULL); } public function getMinimum(): ?int { - return $this->propertySchema->minimum ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->minimum ?? NULL); } public function getExclusiveMinimum(): ?int { - return $this->propertySchema->exclusiveMinimum ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->exclusiveMinimum ?? NULL); } /** * @return int|null Not a standardized property. */ public function getPrecision(): ?int { - return $this->propertySchema->precision ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->precision ?? NULL); } } diff --git a/src/JsonForms/Definition/Control/ObjectControlDefinition.php b/src/JsonForms/Definition/Control/ObjectControlDefinition.php index 35d7a66..e624e04 100644 --- a/src/JsonForms/Definition/Control/ObjectControlDefinition.php +++ b/src/JsonForms/Definition/Control/ObjectControlDefinition.php @@ -21,14 +21,16 @@ namespace Drupal\json_forms\JsonForms\Definition\Control; +use Drupal\json_forms\Util\ConvertUtil; + final class ObjectControlDefinition extends ControlDefinition { public function getMaxProperties(): ?int { - return $this->propertySchema->maxProperties ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->maxProperties ?? NULL); } public function getMinProperties(): ?int { - return $this->propertySchema->minProperties ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->minProperties ?? NULL); } /** @@ -42,7 +44,7 @@ public function getProperties(): \stdClass { * @return array|null */ public function getRequired(): ?array { - return $this->propertySchema->required ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->required ?? NULL); } public function getDependentRequired(): ?\stdClass { diff --git a/src/JsonForms/Definition/Control/StringControlDefinition.php b/src/JsonForms/Definition/Control/StringControlDefinition.php index 46f27a6..b1d9d0b 100644 --- a/src/JsonForms/Definition/Control/StringControlDefinition.php +++ b/src/JsonForms/Definition/Control/StringControlDefinition.php @@ -21,18 +21,20 @@ namespace Drupal\json_forms\JsonForms\Definition\Control; +use Drupal\json_forms\Util\ConvertUtil; + class StringControlDefinition extends ControlDefinition { public function getMaxLength(): ?int { - return $this->propertySchema->maxLength ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->maxLength ?? NULL); } public function getMinLength(): ?int { - return $this->propertySchema->minLength ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->minLength ?? NULL); } public function getPattern(): ?string { - return $this->propertySchema->pattern ?? NULL; + return ConvertUtil::stdClassToNull($this->propertySchema->pattern ?? NULL); } } diff --git a/src/Util/ConvertUtil.php b/src/Util/ConvertUtil.php new file mode 100644 index 0000000..ba46116 --- /dev/null +++ b/src/Util/ConvertUtil.php @@ -0,0 +1,39 @@ +. + */ + +declare(strict_types=1); + +namespace Drupal\json_forms\Util; + +final class ConvertUtil { + + /** + * Returns NULL if the value is an \stdClass, otherwise the value itself. + * + * This method might be used for constraints in form render arrays such that + * no data pointer (defined in \stdClass object) will be used there. + * + * @param mixed $value + * + * @return mixed|null + */ + public static function stdClassToNull($value) { + return $value instanceof \stdClass ? NULL : $value; + } + +}