Upgrade to 3.9.2

This commit is contained in:
Bastian Allgeier
2023-03-08 12:24:06 +01:00
parent c58864a585
commit 3b0b4feb16
44 changed files with 672 additions and 201 deletions

View File

@@ -2,6 +2,7 @@
namespace Kirby\Option;
use Kirby\Cms\Field;
use Kirby\Cms\ModelWithContent;
use Kirby\Cms\Nest;
use Kirby\Data\Json;
@@ -108,45 +109,38 @@ class OptionsApi extends OptionsProvider
// load data from URL and convert from JSON to array
$data = $this->load($model);
// @codeCoverageIgnoreStart
if ($data === null) {
throw new NotFoundException('Options could not be loaded from API: ' . $model->toSafeString($this->url));
}
// @codeCoverageIgnoreEnd
// turn data into Nest so that it can be queried
// or field methods applied to the data
$data = Nest::create($data);
// optionally query a substructure inside the data array
if ($this->query !== null) {
// turn data into Nest so that it can be queried
$data = Nest::create($data);
// actually apply the query and turn the result back into an array
$data = Query::factory($this->query)->resolve($data)->toArray();
}
$data = Query::factory($this->query)->resolve($data);
$options = [];
// create options by resolving text and value query strings
// for each item from the data
$options = array_map(
function ($item, $key) use ($model, $safeMode) {
// convert simple `key: value` API data
if (is_string($item) === true) {
$item = [
'key' => $key,
'value' => $item
];
}
foreach ($data as $key => $item) {
// convert simple `key: value` API data
if (is_string($item) === true) {
$item = new Field(null, $key, $item);
}
$safeMethod = $safeMode === true ? 'toSafeString' : 'toString';
$safeMethod = $safeMode === true ? 'toSafeString' : 'toString';
return [
// value is always a raw string
'value' => $model->toString($this->value, ['item' => $item]),
// text is only a raw string when using {< >}
// or when the safe mode is explicitly disabled (select field)
'text' => $model->$safeMethod($this->text, ['item' => $item])
];
},
// separately pass values and keys to have the keys available in the callback
$data,
array_keys($data)
);
$options[] = [
// value is always a raw string
'value' => $model->toString($this->value, ['item' => $item]),
// text is only a raw string when using {< >}
// or when the safe mode is explicitly disabled (select field)
'text' => $model->$safeMethod($this->text, ['item' => $item])
];
}
// create Options object and render this subsequently
return $this->options = Options::factory($options);