Improve flex classes

This commit is contained in:
Matias Griese 2021-12-07 13:03:09 +02:00
parent 627a1510dc
commit 3ad68d6d5a
9 changed files with 31 additions and 20 deletions

View File

@ -407,7 +407,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
$keys = $this->getKeys();
shuffle($keys);
return $this->createFrom(array_replace(array_flip($keys), $this->entries) ?? []);
return $this->createFrom(array_replace(array_flip($keys), $this->entries));
}
/**

View File

@ -62,7 +62,7 @@ class Flex implements FlexInterface
*/
public function addDirectoryType(string $type, string $blueprint, array $config = [])
{
$config = array_replace_recursive(['enabled' => true], $this->config ?? [], $config);
$config = array_replace_recursive(['enabled' => true], $this->config, $config);
$this->types[$type] = new FlexDirectory($type, $blueprint, $config);
@ -143,7 +143,7 @@ class Flex implements FlexInterface
public function getMixedCollection(array $keys, array $options = []): FlexCollectionInterface
{
$collectionClass = $options['collection_class'] ?? ObjectCollection::class;
if (!class_exists($collectionClass)) {
if (!is_a($collectionClass, FlexCollectionInterface::class, true)) {
throw new RuntimeException(sprintf('Cannot create collection: Class %s does not exist', $collectionClass));
}
@ -233,7 +233,7 @@ class Flex implements FlexInterface
// Use the original key ordering.
if (!$guessed) {
$list = array_replace(array_fill_keys($keys, null), $list) ?? [];
$list = array_replace(array_fill_keys($keys, null), $list);
} else {
// We have mixed keys, we need to map flex keys back to storage keys.
$results = [];

View File

@ -47,7 +47,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
private $_flexDirectory;
/** @var string */
private $_keyField;
private $_keyField = 'storage_key';
/**
* Get list of cached methods.
@ -487,7 +487,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*/
public function getKeyField(): string
{
return $this->_keyField ?? 'storage_key';
return $this->_keyField;
}
/**

View File

@ -495,6 +495,9 @@ class FlexDirectory implements FlexDirectoryInterface
{
/** @phpstan-var class-string $className */
$className = $this->objectClassName ?: $this->getObjectClass();
if (!is_a($className, FlexObjectInterface::class, true)) {
throw new \RuntimeException('Bad object class: ' . $className);
}
return new $className($data, $key, $this, $validate);
}
@ -509,6 +512,9 @@ class FlexDirectory implements FlexDirectoryInterface
{
/** phpstan-var class-string $className */
$className = $this->collectionClassName ?: $this->getCollectionClass();
if (!is_a($className, FlexCollectionInterface::class, true)) {
throw new \RuntimeException('Bad collection class: ' . $className);
}
return $className::createFromArray($entries, $this, $keyField);
}
@ -523,6 +529,9 @@ class FlexDirectory implements FlexDirectoryInterface
{
/** @phpstan-var class-string $className */
$className = $this->indexClassName ?: $this->getIndexClass();
if (!is_a($className, FlexIndexInterface::class, true)) {
throw new \RuntimeException('Bad index class: ' . $className);
}
return $className::createFromArray($entries, $this, $keyField);
}

View File

@ -40,14 +40,14 @@ use function in_array;
* @implements FlexIndexInterface<T>
* @mixin C
*/
class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexIndexInterface
class FlexIndex extends ObjectIndex implements FlexIndexInterface
{
const VERSION = 1;
/** @var FlexDirectory|null */
private $_flexDirectory;
/** @var string */
private $_keyField;
private $_keyField = 'storage_key';
/** @var array */
private $_indexKeys;
@ -353,7 +353,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
*/
public function getKeyField(): string
{
return $this->_keyField ?? 'storage_key';
return $this->_keyField;
}
/**
@ -416,7 +416,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
$previous = $search;
}
return $this->createFrom(array_replace($previous ?? [], $this->getEntries()) ?? []);
return $this->createFrom(array_replace($previous ?? [], $this->getEntries()));
}
/**

View File

@ -888,8 +888,8 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
public function getDefaultValue(string $name, string $separator = null)
{
$separator = $separator ?: '.';
$path = explode($separator, $name) ?: [];
$offset = array_shift($path) ?? '';
$path = explode($separator, $name);
$offset = array_shift($path);
$current = $this->getDefaultValues();

View File

@ -288,7 +288,7 @@ trait PageContentTrait
'process',
$var,
function ($value) {
$value = array_replace(Grav::instance()['config']->get('system.pages.process', []), is_array($value) ? $value : []) ?? [];
$value = array_replace(Grav::instance()['config']->get('system.pages.process', []), is_array($value) ? $value : []);
foreach ($value as $process => $status) {
$value[$process] = (bool)$status;
}
@ -664,6 +664,7 @@ trait PageContentTrait
*/
protected function processContent($content): string
{
$content = is_string($content) ? $content : '';
$grav = Grav::instance();
/** @var Config $config */
@ -676,7 +677,6 @@ trait PageContentTrait
$twig_first = $this->getNestedProperty('header.twig_first') ?? $config->get('system.pages.twig_first', false);
$never_cache_twig = $this->getNestedProperty('header.never_cache_twig') ?? $config->get('system.pages.never_cache_twig', false);
$cached = null;
if ($cache_enable) {
$cache = $this->getCache('render');
$key = md5($this->getCacheKey() . '-content');
@ -688,12 +688,10 @@ trait PageContentTrait
if ($process_twig && $never_cache_twig) {
$this->_content = $this->processTwig($this->_content);
}
} else {
$cached = null;
}
}
if (!$cached) {
if (null === $this->_content) {
$markdown_options = [];
if ($process_markdown) {
// Build markdown options.

View File

@ -127,6 +127,10 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface
$formatterClassName = $formatter['class'] ?? JsonFormatter::class;
$formatterOptions = $formatter['options'] ?? [];
if (!is_a($formatterClassName, FileFormatterInterface::class, true)) {
throw new \InvalidArgumentException('Bad Data Formatter');
}
$this->dataFormatter = new $formatterClassName($formatterOptions);
}

View File

@ -44,7 +44,7 @@ trait FlexMediaTrait
}
/** @var array */
protected $_uploads;
protected $_uploads = [];
/**
* @return string|null
@ -119,7 +119,7 @@ trait FlexMediaTrait
// Load settings for the field.
$schema = $this->getBlueprint()->schema();
$settings = $field && is_object($schema) ? (array)$schema->getProperty($field) : null;
$settings = (array)$schema->getProperty($field);
if (!is_array($settings)) {
return null;
}
@ -405,7 +405,7 @@ trait FlexMediaTrait
*/
protected function getUpdatedMedia(): array
{
return $this->_uploads ?? [];
return $this->_uploads;
}
/**