diff --git a/CHANGELOG.md b/CHANGELOG.md index 56b09dbdf..87fc9c91f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.6.0-rc.5 ## mm/dd/2019 +1. [](#new) + * Added `FlexObjectInterface::getDefaultValue()` and `FormInterface::getDefaultValue()` 1. [](#improved) * Added `Content-Type: application/json` body support for PSR-7 `ServerRequest` * DebugBar: Resolve twig templates in deprecated backtraces in order to help locating Twig issues diff --git a/system/src/Grav/Common/User/FlexUser/User.php b/system/src/Grav/Common/User/FlexUser/User.php index f97adfdb9..49084eb94 100644 --- a/system/src/Grav/Common/User/FlexUser/User.php +++ b/system/src/Grav/Common/User/FlexUser/User.php @@ -161,9 +161,9 @@ class User extends FlexObject implements UserInterface, MediaManipulationInterfa * @param string|null $separator * @return mixed */ - public function value($name, $default = null, $separator = null) + public function getFormValue(string $name, $default = null, string $separator = null) { - $value = parent::value($name, null, $separator); + $value = parent::getFormValue($name, null, $separator); if ($name === 'avatar') { return $this->parseFileProperty($value); diff --git a/system/src/Grav/Framework/Flex/FlexForm.php b/system/src/Grav/Framework/Flex/FlexForm.php index 5cf079c19..2629448ef 100644 --- a/system/src/Grav/Framework/Flex/FlexForm.php +++ b/system/src/Grav/Framework/Flex/FlexForm.php @@ -101,6 +101,18 @@ class FlexForm implements FlexFormInterface return $value ?? $this->getObject()->value($name); } + public function getDefaultValue(string $name) + { + return $this->object->getDefaultValue($name); + } + + /** + * @return array + */ + public function getDefaultValues(): array + { + return $this->object->getDefaultValues(); + } /** * @return string */ diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index 8fe8602d6..ef8c4fd15 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -616,9 +616,50 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface /** * {@inheritdoc} - * @see FlexObjectInterface::value() + * @see FlexObjectInterface::getDefaultValue() */ - public function value($name, $default = null, $separator = null) + public function getDefaultValue(string $name, string $separator = null) + { + $separator = $separator ?: '.'; + $path = explode($separator, $name) ?: []; + $offset = array_shift($path) ?? ''; + + $current = $this->getDefaultValues(); + + if (!isset($current[$offset])) { + return null; + } + + $current = $current[$offset]; + + while ($path) { + $offset = array_shift($path); + + if ((\is_array($current) || $current instanceof \ArrayAccess) && isset($current[$offset])) { + $current = $current[$offset]; + } elseif (\is_object($current) && isset($current->{$offset})) { + $current = $current->{$offset}; + } else { + return null; + } + }; + + return $current; + } + + /** + * @return array + */ + public function getDefaultValues(): array + { + return $this->getBlueprint()->getDefaults(); + } + + /** + * {@inheritdoc} + * @see FlexObjectInterface::getFormValue() + */ + public function getFormValue(string $name, $default = null, string $separator = null) { if ($name === 'storage_key') { return $this->getStorageKey(); @@ -630,6 +671,19 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface return $this->getNestedProperty($name, $default, $separator); } + /** + * @param string $name + * @param mixed|null $default + * @param string|null $separator + * @return mixed + * + * @deprecated 1.6 Use ->getFormValue() method instead. + */ + public function value($name, $default = null, $separator = null) + { + return $this->getFormValue($name, $default, $separator); + } + /** * Returns a string representation of this object. * diff --git a/system/src/Grav/Framework/Flex/Interfaces/FlexObjectInterface.php b/system/src/Grav/Framework/Flex/Interfaces/FlexObjectInterface.php index d0f11f6e9..a10ee8447 100644 --- a/system/src/Grav/Framework/Flex/Interfaces/FlexObjectInterface.php +++ b/system/src/Grav/Framework/Flex/Interfaces/FlexObjectInterface.php @@ -129,7 +129,7 @@ interface FlexObjectInterface extends FlexCommonInterface, NestedObjectInterface * @throws \RuntimeException if object already exists. * @api */ - public function create($key = null); + public function create(string $key = null); /** * Save object into the storage. @@ -173,12 +173,36 @@ interface FlexObjectInterface extends FlexCommonInterface, NestedObjectInterface public function getForm(string $name = '', array $form = null); /** - * Form field compatibility. + * Returns default value suitable to be used in a form for the given property. * - * @param string $name Property name. - * @param mixed $default Default value. - * @param string $separator Optional nested property separator. - * @return mixed Returns value of the field. + * @see FlexObjectInterface::getForm() + * + * @param string $name Property name. + * @param string $separator Optional nested property separator. + * + * @return mixed|null Returns default value of the field, null if there is no default value. */ - public function value($name, $default = null, $separator = null); + public function getDefaultValue(string $name, string $separator = null); + + /** + * Returns default values suitable to be used in a form for the given property. + * + * @see FlexObjectInterface::getForm() + * + * @return array Returns default values. + */ + public function getDefaultValues(): array; + + /** + * Returns raw value suitable to be used in a form for the given property. + * + * @see FlexObjectInterface::getForm() + * + * @param string $name Property name. + * @param mixed $default Default value. + * @param string $separator Optional nested property separator. + * + * @return mixed Returns value of the field. + */ + public function getFormValue(string $name, $default = null, string $separator = null); } diff --git a/system/src/Grav/Framework/Form/Traits/FormTrait.php b/system/src/Grav/Framework/Form/Traits/FormTrait.php index 24d6a4b54..23f941093 100644 --- a/system/src/Grav/Framework/Form/Traits/FormTrait.php +++ b/system/src/Grav/Framework/Form/Traits/FormTrait.php @@ -113,6 +113,42 @@ trait FormTrait return $this->data[$name] ?? null; } + public function getDefaultValue(string $name) + { + $path = explode('.', $name) ?: []; + $offset = array_shift($path) ?? ''; + + $current = $this->getDefaultValues(); + + if (!isset($current[$offset])) { + return null; + } + + $current = $current[$offset]; + + while ($path) { + $offset = array_shift($path); + + if ((\is_array($current) || $current instanceof \ArrayAccess) && isset($current[$offset])) { + $current = $current[$offset]; + } elseif (\is_object($current) && isset($current->{$offset})) { + $current = $current->{$offset}; + } else { + return null; + } + }; + + return $current; + } + + /** + * @return array + */ + public function getDefaultValues(): array + { + return $this->getBlueprint()->getDefaults(); + } + /** * @param ServerRequestInterface $request * @return FormInterface|$this