1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 21:56:33 +02:00

refactor(flextype): use collection instead of arrays for arrays collections

This commit is contained in:
Awilum
2022-04-05 16:59:50 +03:00
parent 6f73d54280
commit 1e77fa9a49
12 changed files with 104 additions and 31 deletions

View File

@@ -82,5 +82,11 @@
"phpstan/phpstan": "^1.4.4",
"symfony/var-dumper": "^5.4.3",
"victorjonsson/markdowndocs": "^1.3.8"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}

View File

@@ -105,13 +105,13 @@ class EntriesFetchCommand extends Command
if ($data = entries()->fetch($id, $options)) {
if (isset($options['collection']) && $options['collection'] == true) {
foreach ($data->toArray() as $item) {
foreach(arrays($item)->dot() as $key => $value) {
foreach(collection($item)->dot() as $key => $value) {
$output->writeln('<info>'.$key.':</info> ' . $value);
}
$output->writeln('');
}
} else {
foreach(arrays($data)->dot() as $key => $value) {
foreach(collection($data)->dot() as $key => $value) {
$output->writeln('<info>'.$key.':</info> ' . $value);
}
$output->writeln('');

View File

@@ -116,7 +116,7 @@ class Entries
}
}
$events = arrays($events)->unique()->toArray();
$events = collection($events)->unique()->toArray();
foreach ($events as $event) {
if (filesystem()->file($event)->exists()) {
@@ -159,7 +159,7 @@ class Entries
}
}
$fields = arrays($fields)->unique()->toArray();
$fields = collection($fields)->unique()->toArray();
foreach ($fields as $field) {
if (filesystem()->file($field)->exists()) {
@@ -258,7 +258,7 @@ class Entries
emitter()->emit('onEntriesFetchSingleCacheHasResult');
// Return result from the cache.
return arrays($this->registry()->get('methods.fetch.result'));
return collection($this->registry()->get('methods.fetch.result'));
}
// 2. Try to get requested entry from the filesystem
@@ -273,7 +273,7 @@ class Entries
if ($entryFileContent === false) {
// Run event
emitter()->emit('onEntriesFetchSingleNoResult');
return arrays($this->registry()->get('methods.fetch.params.result'));
return collection($this->registry()->get('methods.fetch.params.result'));
}
// Decode entry file content
@@ -295,14 +295,14 @@ class Entries
}
// Return entry fetch result
return arrays($this->registry()->get('methods.fetch.result'));
return collection($this->registry()->get('methods.fetch.result'));
}
// Run event
emitter()->emit('onEntriesFetchSingleNoResult');
// Return entry fetch result
return arrays($this->registry()->get('methods.fetch.result'));
return collection($this->registry()->get('methods.fetch.result'));
};
if ($this->registry()->has('methods.fetch.params.options.collection') &&
@@ -333,7 +333,7 @@ class Entries
emitter()->emit('onEntriesFetchCollectionNoResult');
// Return entries array
return arrays($this->registry()->get('methods.fetch.result'));
return collection($this->registry()->get('methods.fetch.result'));
}
// Find entries in the filesystem.
@@ -384,7 +384,7 @@ class Entries
if ($this->registry()->has('methods.fetch.params.options.filter.only')) {
$data = [];
foreach ($this->registry()->get('methods.fetch.result') as $key => $value) {
$data[$key] = arrays($value)->only($this->registry()->get('methods.fetch.params.options.filter.only'))->toArray();
$data[$key] = collection($value)->only($this->registry()->get('methods.fetch.params.options.filter.only'))->toArray();
}
$this->registry()->delete('methods.fetch.params.options.filter.only');
$this->registry()->set('methods.fetch.result', $data);
@@ -396,7 +396,7 @@ class Entries
if ($this->registry()->has('methods.fetch.params.options.filter.except')) {
$data = [];
foreach ($this->registry()->get('methods.fetch.result') as $key => $value) {
$data[$key] = arrays($value)->except($this->registry()->get('methods.fetch.params.options.filter.except'))->toArray();
$data[$key] = collection($value)->except($this->registry()->get('methods.fetch.params.options.filter.except'))->toArray();
}
$this->registry()->delete('methods.fetch.params.options.filter.except');
$this->registry()->set('methods.fetch.result', $data);
@@ -408,18 +408,18 @@ class Entries
$this->registry()->has('methods.fetch.params.options.filter') ?
$this->registry()->get('methods.fetch.params.options.filter') : []));
return arrays($this->registry()->get('methods.fetch.result'));
return collection($this->registry()->get('methods.fetch.result'));
} else {
// Run event
emitter()->emit('onEntriesFetchCollectionNoResult');
// Return entries array
return arrays($this->registry()->get('methods.fetch.result'));
return collection($this->registry()->get('methods.fetch.result'));
}
// Return entries array
return arrays($this->registry()->get('methods.fetch.result'));
return collection($this->registry()->get('methods.fetch.result'));
}
// Fetch single entry.
@@ -841,7 +841,7 @@ class Entries
*/
public function setRegistry(array $registry = []): void
{
$this->registry = arrays($registry);
$this->registry = collection($registry);
}
/**

View File

@@ -61,7 +61,7 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void
$data[$field] = ($data[$field] instanceof Arrays) ? $data[$field]->{$result}() : $data[$field];
}
$result = arrays($original['result'])->merge($data)->toArray();
$result = collection($original['result'])->merge($data)->toArray();
if (boolval(entries()->registry()->get('methods.fetch.collection.fields.entries.dump')) === false) {
unset($result['entries']);

View File

@@ -37,14 +37,14 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void
foreach (entries()->registry()->get('methods.fetch.result.parsers.'.$parserName.'.fields') as $field) {
if (! in_array($field, registry()->get('flextype.settings.entries.collections.default.fields'))) {
if ($parserName == 'markdown') {
if (arrays(entries()->registry()->get('methods.fetch.result'))->has($field)) {
if (collection(entries()->registry()->get('methods.fetch.result'))->has($field)) {
entries()->registry()->set('methods.fetch.result.'.$field,
parsers()->markdown()->parse(entries()->registry()->get('methods.fetch.result.'.$field), $cache));
}
}
if ($parserName == 'shortcodes') {
if (arrays(entries()->registry()->get('methods.fetch.result'))->has($field)) {
if (collection(entries()->registry()->get('methods.fetch.result'))->has($field)) {
entries()->registry()->set('methods.fetch.result.'.$field,
parsers()->shortcodes()->parse(entries()->registry()->get('methods.fetch.result.'.$field), $cache));
}

View File

@@ -29,14 +29,14 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void
// Modify fetch.
foreach (entries()->registry()->get('methods.fetch.result.registry.get') as $field => $body) {
$data = arrays($data)->merge(arrays($data)->set($field, registry()->get($body['key'],
$data = collection($data)->merge(collection($data)->set($field, registry()->get($body['key'],
isset($body['default']) ?
$body['default'] :
[]))->toArray())->toArray();
}
$result = arrays($original['result'])->merge($data)->toArray();
$result = collection($original['result'])->merge($data)->toArray();
if (boolval(entries()->registry()->get('methods.fetch.collection.fields.entries.dump')) === false) {
unset($result['registry']);

View File

@@ -29,5 +29,5 @@ parsers()->shortcodes()->addHandler('entries_fetch', static function (ShortcodeI
return '';
}
return arrays(entries()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default'));
return collection(entries()->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default'));
});

View File

@@ -82,7 +82,7 @@ class Plugins
// Get plugins list
$pluginsList = $this->getPluginsList();
// $pluginsList = arrays($pluginsList)->only(['twig', 'blueprints'])->toArray();
// $pluginsList = collection($pluginsList)->only(['twig', 'blueprints'])->toArray();
// Get plugins Cache ID
$pluginsCacheID = $this->getPluginsCacheID($pluginsList);
@@ -171,11 +171,11 @@ class Plugins
}
// Sort plugins list by priority.
$plugins = arrays($plugins)->sortBy('_priority', 'ASC')->toArray();
$plugins = collection($plugins)->sortBy('_priority', 'ASC')->toArray();
// ... and delete tmp _priority field for sorting
foreach ($plugins as $pluginName => $pluginData) {
$plugins = arrays($plugins)->delete($pluginName . '._priority')->toArray();
$plugins = collection($plugins)->delete($pluginName . '._priority')->toArray();
}
// Get Valid Plugins Dependencies

View File

@@ -55,7 +55,7 @@ class Frontmatter
if (isset($input['content'])) {
$content = $input['content'];
$input = arrays($input)->delete('content')->toArray();
$input = collection($input)->delete('content')->toArray();
$matter = serializers()->{$headerSerializer}()->encode($input);
} else {
$content = '';

View File

@@ -2,6 +2,73 @@
declare(strict_types=1);
use Glowy\Arrays\Arrays as Collection;
if (! function_exists('collection')) {
/**
* Create a new arrayable collection object from the given elements.
*
* Initializes a Collection object and assigns $items the supplied values.
*
* @param mixed $items Items
*
* @return Collection
*/
function collection($items = null): Collection
{
return Collection::create($items);
}
}
if (! function_exists('collectionFromJson')) {
/**
* Create a new arrayable collection object from the given JSON string.
*
* @param string $input A string containing JSON.
* @param bool $assoc Decode assoc. When TRUE, returned objects will be converted into associative array collection.
* @param int $depth Decode Depth. Set the maximum depth. Must be greater than zero.
* @param int $flags Bitmask consisting of decode options
*
* @return Collection
*/
function collectionFromJson(string $input, bool $assoc = true, int $depth = 512, int $flags = 0): Collection
{
return Collection::createFromJson($input, $assoc, $depth, $flags);
}
}
if (! function_exists('collectionFromString')) {
/**
* Create a new arrayable collection object from the given string.
*
* @param string $string Input string.
* @param string $separator Elements separator.
*
* @return Collection
*/
function collectionFromString(string $string, string $separator): Collection
{
return Collection::createFromString($string, $separator);
}
}
if (! function_exists('collectionWithRange')) {
/**
* Create a new arrayable object with a range of elements.
*
* @param float|int|string $low First value of the sequence.
* @param float|int|string $high The sequence is ended upon reaching the end value.
* @param int $step If a step value is given, it will be used as the increment between elements in the sequence.
* step should be given as a positive number. If not specified, step will default to 1.
*
* @return Collection
*/
function collectionWithRange($low, $high, int $step = 1): Arrays
{
return Collection::createWithRange($low, $high, $step);
}
}
if (! function_exists('filterCollection')) {
/**
* Filter collection.
@@ -13,7 +80,7 @@ if (! function_exists('filterCollection')) {
*/
function filterCollection($items = [], array $options = []): array
{
$collection = arrays($items);
$collection = collection($items);
! isset($options['return']) and $options['return'] = 'all';

View File

@@ -151,7 +151,7 @@ if (! function_exists('getBaseUrl')) {
$url .= $getAuth();
$serverData = arrays($_SERVER);
$serverData = collection($_SERVER);
$host = (string) $serverData->get('HTTP_HOST');
$port = (int) $serverData->get('SERVER_PORT');

View File

@@ -16,10 +16,10 @@ if (! Arrays::hasMacro('onlyFromCollection')) {
$result = [];
foreach ($this->toArray() as $key => $value) {
$result[$key] = arrays($value)->only($keys)->toArray();
$result[$key] = collection($value)->only($keys)->toArray();
}
return arrays($result);
return collection($result);
});
}
@@ -35,9 +35,9 @@ if (! Arrays::hasMacro('exceptFromCollection')) {
$result = [];
foreach ($this->toArray() as $key => $value) {
$result[$key] = arrays($value)->except($keys)->toArray();
$result[$key] = collection($value)->except($keys)->toArray();
}
return arrays($result);
return collection($result);
});
}