From 0a0cd6046c71e6df1ed3f36424bac63c46fc67bb Mon Sep 17 00:00:00 2001 From: Awilum Date: Mon, 2 Aug 2021 11:55:04 +0300 Subject: [PATCH] feat(macros): Add ability to fetch specific fields from single entry and from entries collection #558 --- composer.json | 3 +- src/flextype/Support/Macros/ArraysMacros.php | 41 ---------------- src/flextype/macros.php | 49 ++++++++++++++++++++ 3 files changed, 51 insertions(+), 42 deletions(-) delete mode 100644 src/flextype/Support/Macros/ArraysMacros.php create mode 100644 src/flextype/macros.php diff --git a/composer.json b/composer.json index 6eabbdb2..8d843e1c 100755 --- a/composer.json +++ b/composer.json @@ -71,7 +71,8 @@ "src/flextype" ], "files": [ - "src/flextype/helpers.php" + "src/flextype/helpers.php", + "src/flextype/macros.php" ] }, "require-dev": { diff --git a/src/flextype/Support/Macros/ArraysMacros.php b/src/flextype/Support/Macros/ArraysMacros.php deleted file mode 100644 index 6612ee21..00000000 --- a/src/flextype/Support/Macros/ArraysMacros.php +++ /dev/null @@ -1,41 +0,0 @@ -toArray() as $key => $value) { - $result[$key] = arrays($value)->only($keys)->toArray(); - } - - return arrays($result); -}); - -Arrays::macro('exceptFromCollection', function(array $keys) { - $result = []; - - foreach ($this->toArray() as $key => $value) { - $result[$key] = arrays($value)->except($keys)->toArray(); - } - - return arrays($result); -}); \ No newline at end of file diff --git a/src/flextype/macros.php b/src/flextype/macros.php new file mode 100644 index 00000000..44c1f833 --- /dev/null +++ b/src/flextype/macros.php @@ -0,0 +1,49 @@ +toArray() as $key => $value) { + $result[$key] = arrays($value)->only($keys)->toArray(); + } + + return arrays($result); + }); +} + +if (! Arrays::hasMacro('onlyFromCollection')) { + /** + * Return slice of an array except given keys. + * + * @param array $keys List of keys to except. + * + * @return Arrays Returns instance of The Arrays class. + */ + Arrays::macro('exceptFromCollection', function(array $keys) { + $result = []; + + foreach ($this->toArray() as $key => $value) { + $result[$key] = arrays($value)->except($keys)->toArray(); + } + + return arrays($result); + }); +} \ No newline at end of file