mirror of
https://github.com/getgrav/grav.git
synced 2026-07-04 13:48:37 +02:00
Added FlexObjectInterface::getDefaultValue() and FormInterface::getDefaultValue()
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user