From 1e77fa9a4913667785a741d0f57bc19f9d9e0595 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 5 Apr 2022 16:59:50 +0300 Subject: [PATCH] refactor(flextype): use collection instead of arrays for arrays collections --- composer.json | 6 ++ .../Commands/Entries/EntrieFetchCommand.php | 4 +- src/flextype/core/Entries/Entries.php | 26 +++---- .../Entries/Fields/Default/EntriesField.php | 2 +- .../Entries/Fields/Default/ParsersField.php | 4 +- .../Entries/Fields/Default/RegistryField.php | 4 +- .../Parsers/Shortcodes/EntriesShortcode.php | 2 +- src/flextype/core/Plugins.php | 6 +- src/flextype/core/Serializers/Frontmatter.php | 2 +- .../helpers/{arrays.php => collection.php} | 69 ++++++++++++++++++- src/flextype/helpers/urls.php | 2 +- src/flextype/macros/arrays.php | 8 +-- 12 files changed, 104 insertions(+), 31 deletions(-) rename src/flextype/helpers/{arrays.php => collection.php} (51%) diff --git a/composer.json b/composer.json index 104445f9..0ffb3777 100755 --- a/composer.json +++ b/composer.json @@ -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 + } } } diff --git a/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php b/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php index b7ac2e6c..dcf5697b 100644 --- a/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php +++ b/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php @@ -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(''.$key.': ' . $value); } $output->writeln(''); } } else { - foreach(arrays($data)->dot() as $key => $value) { + foreach(collection($data)->dot() as $key => $value) { $output->writeln(''.$key.': ' . $value); } $output->writeln(''); diff --git a/src/flextype/core/Entries/Entries.php b/src/flextype/core/Entries/Entries.php index 15cf299f..2c8e6b34 100755 --- a/src/flextype/core/Entries/Entries.php +++ b/src/flextype/core/Entries/Entries.php @@ -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); } /** diff --git a/src/flextype/core/Entries/Fields/Default/EntriesField.php b/src/flextype/core/Entries/Fields/Default/EntriesField.php index 9978e9e3..3efaeee0 100644 --- a/src/flextype/core/Entries/Fields/Default/EntriesField.php +++ b/src/flextype/core/Entries/Fields/Default/EntriesField.php @@ -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']); diff --git a/src/flextype/core/Entries/Fields/Default/ParsersField.php b/src/flextype/core/Entries/Fields/Default/ParsersField.php index 53a4d27a..3ef20eec 100644 --- a/src/flextype/core/Entries/Fields/Default/ParsersField.php +++ b/src/flextype/core/Entries/Fields/Default/ParsersField.php @@ -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)); } diff --git a/src/flextype/core/Entries/Fields/Default/RegistryField.php b/src/flextype/core/Entries/Fields/Default/RegistryField.php index 238e712e..5b052856 100644 --- a/src/flextype/core/Entries/Fields/Default/RegistryField.php +++ b/src/flextype/core/Entries/Fields/Default/RegistryField.php @@ -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']); diff --git a/src/flextype/core/Parsers/Shortcodes/EntriesShortcode.php b/src/flextype/core/Parsers/Shortcodes/EntriesShortcode.php index 6867c003..2a270647 100644 --- a/src/flextype/core/Parsers/Shortcodes/EntriesShortcode.php +++ b/src/flextype/core/Parsers/Shortcodes/EntriesShortcode.php @@ -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')); }); diff --git a/src/flextype/core/Plugins.php b/src/flextype/core/Plugins.php index b4a39fea..4284d43e 100755 --- a/src/flextype/core/Plugins.php +++ b/src/flextype/core/Plugins.php @@ -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 diff --git a/src/flextype/core/Serializers/Frontmatter.php b/src/flextype/core/Serializers/Frontmatter.php index c237bbc7..08365901 100644 --- a/src/flextype/core/Serializers/Frontmatter.php +++ b/src/flextype/core/Serializers/Frontmatter.php @@ -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 = ''; diff --git a/src/flextype/helpers/arrays.php b/src/flextype/helpers/collection.php similarity index 51% rename from src/flextype/helpers/arrays.php rename to src/flextype/helpers/collection.php index 3b8df304..778daed8 100644 --- a/src/flextype/helpers/arrays.php +++ b/src/flextype/helpers/collection.php @@ -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'; diff --git a/src/flextype/helpers/urls.php b/src/flextype/helpers/urls.php index 60bdcef4..ec41823f 100644 --- a/src/flextype/helpers/urls.php +++ b/src/flextype/helpers/urls.php @@ -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'); diff --git a/src/flextype/macros/arrays.php b/src/flextype/macros/arrays.php index abb6c33d..2412a016 100644 --- a/src/flextype/macros/arrays.php +++ b/src/flextype/macros/arrays.php @@ -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); }); }