├── .php-cs-fixer.dist.php ├── LICENSE ├── composer.json ├── config └── collection-macros.php └── src ├── CollectionMacrosServiceProvider.php └── Macros ├── Decrement.php ├── Increment.php ├── Krsort.php ├── Ksort.php └── Recursive.php /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | in(__DIR__) 14 | ->exclude($excludeDirs) 15 | ->ignoreDotFiles(true)->ignoreVCS(true) 16 | ->filter(function (\SplFileInfo $file) use ($excludeFiles) { 17 | return ! in_array($file->getRelativePathName(), $excludeFiles); 18 | }) 19 | ; 20 | 21 | return (new Werxe\PhpCsFixer\Config()) 22 | ->setFinder($finder) 23 | ->setUsingCache(false) 24 | ->setRiskyAllowed(true) 25 | ->withPHPUnitRules() 26 | ; 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2011-2019 Werxe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "werxe/laravel-collection-macros", 3 | "description": "Custom Laravel Collection macros.", 4 | "keywords": [ 5 | "php", 6 | "werxe", 7 | "laravel", 8 | "collection", 9 | "macros" 10 | ], 11 | "homepage": "https://werxe.com", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Werxe", 16 | "email": "hello@werxe.com", 17 | "homepage": "https://werxe.com" 18 | } 19 | ], 20 | "require": { 21 | "php": "^8.0", 22 | "illuminate/support": "^9.0" 23 | }, 24 | "require-dev": { 25 | "bamarni/composer-bin-plugin": "^1.4", 26 | "orchestra/testbench": "^7.0", 27 | "phpunit/phpunit": "^9.0", 28 | "werxe/php-cs-fixer-config": "^2.0" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "Werxe\\Laravel\\CollectionMacros\\": "src/" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "Werxe\\Laravel\\CollectionMacros\\Tests\\": "tests/" 38 | } 39 | }, 40 | "config": { 41 | "preferred-install": "dist", 42 | "sort-packages": true, 43 | "optimize-autoloader": true, 44 | "allow-plugins": { 45 | "bamarni/composer-bin-plugin": true 46 | } 47 | }, 48 | "extra": { 49 | "branch-alias": { 50 | "dev-master": "7.0.x-dev" 51 | }, 52 | "laravel": { 53 | "providers": [ 54 | "Werxe\\Laravel\\CollectionMacros\\CollectionMacrosServiceProvider" 55 | ] 56 | } 57 | }, 58 | "scripts": { 59 | "cs": "./vendor/bin/php-cs-fixer fix --ansi --show-progress=dots", 60 | "cs:ci": "./vendor/bin/php-cs-fixer fix --ansi --show-progress=dots --diff --dry-run", 61 | "test": "./vendor/bin/phpunit --verbose --colors=always" 62 | }, 63 | "minimum-stability": "stable" 64 | } 65 | -------------------------------------------------------------------------------- /config/collection-macros.php: -------------------------------------------------------------------------------- 1 | Increment::class, 13 | 'decrement' => Decrement::class, 14 | 'krsort' => Krsort::class, 15 | 'ksort' => Ksort::class, 16 | 'recursive' => Recursive::class, 17 | ]; 18 | -------------------------------------------------------------------------------- /src/CollectionMacrosServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 20 | // Publish config 21 | $this->publishes([ 22 | realpath(__DIR__.'/../config/collection-macros.php') => config_path('werxe/collection-macros/config.php'), 23 | ], 'werxe:collection-macros.config'); 24 | } 25 | } 26 | 27 | /** 28 | * Register any package services. 29 | * 30 | * @return void 31 | */ 32 | public function register() 33 | { 34 | $this->mergeConfigFrom( 35 | realpath(__DIR__.'/../config/collection-macros.php'), 'werxe.collection-macros.config' 36 | ); 37 | 38 | $macros = $this->app['config']->get('werxe.collection-macros.config'); 39 | 40 | foreach ($macros as $macro => $class) { 41 | if (! Collection::hasMacro($macro)) { 42 | Collection::macro($macro, app($class)()); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Macros/Decrement.php: -------------------------------------------------------------------------------- 1 | items, $key)) { 15 | $amount = Arr::get($this->items, $key) - $amount; 16 | } 17 | 18 | Arr::set($this->items, $key, $amount); 19 | 20 | return $this; 21 | }; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Macros/Increment.php: -------------------------------------------------------------------------------- 1 | items, $key)) { 15 | $amount += Arr::get($this->items, $key); 16 | } 17 | 18 | Arr::set($this->items, $key, $amount); 19 | 20 | return $this; 21 | }; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Macros/Krsort.php: -------------------------------------------------------------------------------- 1 | items); 15 | 16 | return new Collection($this->items); 17 | }; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Macros/Ksort.php: -------------------------------------------------------------------------------- 1 | items); 15 | 16 | return new Collection($this->items); 17 | }; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Macros/Recursive.php: -------------------------------------------------------------------------------- 1 | map(static function ($value) { 15 | if (is_array($value) || is_object($value)) { 16 | return (new Collection($value))->recursive(); 17 | } 18 | 19 | return $value; 20 | }); 21 | }; 22 | } 23 | } 24 | --------------------------------------------------------------------------------