mirror of
https://github.com/getgrav/grav.git
synced 2025-11-02 19:36:06 +01:00
Added third $name parameter to Blueprint::flattenData() method, useful for flattening repeating data
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
* Made `Grav::redirect()` to accept `Route` class
|
* Made `Grav::redirect()` to accept `Route` class
|
||||||
2. [](#improved)
|
2. [](#improved)
|
||||||
* Upgraded vendor libs for PHP 8.1 compatibility
|
* Upgraded vendor libs for PHP 8.1 compatibility
|
||||||
|
* Added third `$name` parameter to `Blueprint::flattenData()` method, useful for flattening repeating data
|
||||||
|
|
||||||
# v1.7.25
|
# v1.7.25
|
||||||
## 11/16/2021
|
## 11/16/2021
|
||||||
|
|||||||
@@ -293,15 +293,16 @@ class Blueprint extends BlueprintForm
|
|||||||
/**
|
/**
|
||||||
* Flatten data by using blueprints.
|
* Flatten data by using blueprints.
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data Data to be flattened.
|
||||||
* @param bool $includeAll
|
* @param bool $includeAll True if undefined properties should also be included.
|
||||||
|
* @param string $name Property which will be flattened, useful for flattening repeating data.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function flattenData(array $data, bool $includeAll = false)
|
public function flattenData(array $data, bool $includeAll = false, string $name = '')
|
||||||
{
|
{
|
||||||
$this->initInternals();
|
$this->initInternals();
|
||||||
|
|
||||||
return $this->blueprintSchema->flattenData($data, $includeAll);
|
return $this->blueprintSchema->flattenData($data, $includeAll, $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -116,22 +116,28 @@ class BlueprintSchema extends BlueprintSchemaBase implements ExportInterface
|
|||||||
* Flatten data by using blueprints.
|
* Flatten data by using blueprints.
|
||||||
*
|
*
|
||||||
* @param array $data Data to be flattened.
|
* @param array $data Data to be flattened.
|
||||||
* @param bool $includeAll
|
* @param bool $includeAll True if undefined properties should also be included.
|
||||||
|
* @param string $name Property which will be flattened, useful for flattening repeating data.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function flattenData(array $data, bool $includeAll = false)
|
public function flattenData(array $data, bool $includeAll = false, string $name = '')
|
||||||
{
|
{
|
||||||
|
$prefix = $name !== '' ? $name . '.' : '';
|
||||||
|
|
||||||
$list = [];
|
$list = [];
|
||||||
if ($includeAll) {
|
if ($includeAll) {
|
||||||
foreach ($this->items as $key => $rules) {
|
$items = $name !== '' ? $this->getProperty($name)['fields'] ?? [] : $this->items;
|
||||||
|
foreach ($items as $key => $rules) {
|
||||||
$type = $rules['type'] ?? '';
|
$type = $rules['type'] ?? '';
|
||||||
if (!str_starts_with($type, '_') && !str_contains($key, '*')) {
|
if (!str_starts_with($type, '_') && !str_contains($key, '*')) {
|
||||||
$list[$key] = null;
|
$list[$prefix . $key] = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_replace($list, $this->flattenArray($data, $this->nested, ''));
|
$nested = $this->getNestedRules($name);
|
||||||
|
|
||||||
|
return array_replace($list, $this->flattenArray($data, $nested, $prefix));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user