diff --git a/composer.json b/composer.json index 63ae8cb..59ebc4c 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ ], "require": { "php": ">=7.0", - "nesbot/carbon": "~1.20" + "nesbot/carbon": "~1.20", + "symfony/var-dumper": "~3.3" }, "require-dev": { "mockery/mockery": "~1.0", @@ -27,9 +28,6 @@ "Tightenco\\": "src/Tightenco" } }, - "suggest": { - "symfony/var-dumper": "Required to prettify dd() and dump() (~3.3)." - }, "scripts": { "test": [ "@composer install", diff --git a/composer.lock b/composer.lock index bfd25ac..023cee4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "679e3042bfdf42a1f874e734d08033b1", + "content-hash": "3b0035f594bd16fe9ad9d04825982dce", "packages": [ { "name": "nesbot/carbon", @@ -61,16 +61,16 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.6.0", + "version": "v1.7.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296" + "reference": "78be803ce01e55d3491c1397cf1c64beb9c1b63b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296", - "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/78be803ce01e55d3491c1397cf1c64beb9c1b63b", + "reference": "78be803ce01e55d3491c1397cf1c64beb9c1b63b", "shasum": "" }, "require": { @@ -82,7 +82,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.6-dev" + "dev-master": "1.7-dev" } }, "autoload": { @@ -116,20 +116,20 @@ "portable", "shim" ], - "time": "2017-10-11T12:05:26+00:00" + "time": "2018-01-30T19:27:44+00:00" }, { "name": "symfony/translation", - "version": "v3.4.2", + "version": "v3.4.4", "source": { "type": "git", "url": "https://github.com/symfony/translation.git", - "reference": "4c5d5582baf2829751a5207659329c1f52eedeb6" + "reference": "10b32cf0eae28b9b39fe26c456c42b19854c4b84" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/translation/zipball/4c5d5582baf2829751a5207659329c1f52eedeb6", - "reference": "4c5d5582baf2829751a5207659329c1f52eedeb6", + "url": "https://api.github.com/repos/symfony/translation/zipball/10b32cf0eae28b9b39fe26c456c42b19854c4b84", + "reference": "10b32cf0eae28b9b39fe26c456c42b19854c4b84", "shasum": "" }, "require": { @@ -184,7 +184,76 @@ ], "description": "Symfony Translation Component", "homepage": "https://symfony.com", - "time": "2017-12-12T08:27:14+00:00" + "time": "2018-01-18T22:16:57+00:00" + }, + { + "name": "symfony/var-dumper", + "version": "v3.4.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/var-dumper.git", + "reference": "472a9849930cf21f73abdb02240f17cf5b5bd1a7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/472a9849930cf21f73abdb02240f17cf5b5bd1a7", + "reference": "472a9849930cf21f73abdb02240f17cf5b5bd1a7", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0" + }, + "require-dev": { + "ext-iconv": "*", + "twig/twig": "~1.34|~2.4" + }, + "suggest": { + "ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).", + "ext-intl": "To show region name in time zone dump", + "ext-symfony_debug": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.4-dev" + } + }, + "autoload": { + "files": [ + "Resources/functions/dump.php" + ], + "psr-4": { + "Symfony\\Component\\VarDumper\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony mechanism for exploring and dumping PHP variables", + "homepage": "https://symfony.com", + "keywords": [ + "debug", + "dump" + ], + "time": "2018-01-29T09:03:43+00:00" } ], "packages-dev": [ @@ -558,16 +627,16 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "4.2.0", + "version": "4.3.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "66465776cfc249844bde6d117abff1d22e06c2da" + "reference": "94fd0001232e47129dd3504189fa1c7225010d08" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/66465776cfc249844bde6d117abff1d22e06c2da", - "reference": "66465776cfc249844bde6d117abff1d22e06c2da", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/94fd0001232e47129dd3504189fa1c7225010d08", + "reference": "94fd0001232e47129dd3504189fa1c7225010d08", "shasum": "" }, "require": { @@ -605,7 +674,7 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2017-11-27T17:38:31+00:00" + "time": "2017-11-30T07:14:17+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -1052,16 +1121,16 @@ }, { "name": "phpunit/phpunit-mock-objects", - "version": "5.0.5", + "version": "5.0.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "283b9f4f670e3a6fd6c4ff95c51a952eb5c75933" + "reference": "33fd41a76e746b8fa96d00b49a23dadfa8334cdf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/283b9f4f670e3a6fd6c4ff95c51a952eb5c75933", - "reference": "283b9f4f670e3a6fd6c4ff95c51a952eb5c75933", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/33fd41a76e746b8fa96d00b49a23dadfa8334cdf", + "reference": "33fd41a76e746b8fa96d00b49a23dadfa8334cdf", "shasum": "" }, "require": { @@ -1107,7 +1176,7 @@ "mock", "xunit" ], - "time": "2017-12-10T08:01:53+00:00" + "time": "2018-01-06T05:45:45+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", @@ -1156,16 +1225,16 @@ }, { "name": "sebastian/comparator", - "version": "2.1.0", + "version": "2.1.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "1174d9018191e93cb9d719edec01257fc05f8158" + "reference": "11c07feade1d65453e06df3b3b90171d6d982087" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1174d9018191e93cb9d719edec01257fc05f8158", - "reference": "1174d9018191e93cb9d719edec01257fc05f8158", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/11c07feade1d65453e06df3b3b90171d6d982087", + "reference": "11c07feade1d65453e06df3b3b90171d6d982087", "shasum": "" }, "require": { @@ -1216,7 +1285,7 @@ "compare", "equality" ], - "time": "2017-11-03T07:16:52+00:00" + "time": "2018-01-12T06:34:42+00:00" }, { "name": "sebastian/diff", @@ -1668,75 +1737,6 @@ "homepage": "https://github.com/sebastianbergmann/version", "time": "2016-10-03T07:35:21+00:00" }, - { - "name": "symfony/var-dumper", - "version": "v3.4.2", - "source": { - "type": "git", - "url": "https://github.com/symfony/var-dumper.git", - "reference": "757074cf71b952ce9e75b557538948811c2bf006" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/var-dumper/zipball/757074cf71b952ce9e75b557538948811c2bf006", - "reference": "757074cf71b952ce9e75b557538948811c2bf006", - "shasum": "" - }, - "require": { - "php": "^5.5.9|>=7.0.8", - "symfony/polyfill-mbstring": "~1.0" - }, - "conflict": { - "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0" - }, - "require-dev": { - "ext-iconv": "*", - "twig/twig": "~1.34|~2.4" - }, - "suggest": { - "ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).", - "ext-intl": "To show region name in time zone dump", - "ext-symfony_debug": "" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.4-dev" - } - }, - "autoload": { - "files": [ - "Resources/functions/dump.php" - ], - "psr-4": { - "Symfony\\Component\\VarDumper\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony mechanism for exploring and dumping PHP variables", - "homepage": "https://symfony.com", - "keywords": [ - "debug", - "dump" - ], - "time": "2017-12-11T22:06:16+00:00" - }, { "name": "theseer/tokenizer", "version": "1.1.0", @@ -1779,16 +1779,16 @@ }, { "name": "webmozart/assert", - "version": "1.2.0", + "version": "1.3.0", "source": { "type": "git", "url": "https://github.com/webmozart/assert.git", - "reference": "2db61e59ff05fe5126d152bd0655c9ea113e550f" + "reference": "0df1908962e7a3071564e857d86874dad1ef204a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/2db61e59ff05fe5126d152bd0655c9ea113e550f", - "reference": "2db61e59ff05fe5126d152bd0655c9ea113e550f", + "url": "https://api.github.com/repos/webmozart/assert/zipball/0df1908962e7a3071564e857d86874dad1ef204a", + "reference": "0df1908962e7a3071564e857d86874dad1ef204a", "shasum": "" }, "require": { @@ -1825,7 +1825,7 @@ "check", "validate" ], - "time": "2016-11-23T20:04:58+00:00" + "time": "2018-01-29T19:49:41+00:00" } ], "aliases": [], diff --git a/src/Tightenco/Support/Collection.php b/src/Tightenco/Support/Collection.php index 6625875..22fd5f7 100644 --- a/src/Tightenco/Support/Collection.php +++ b/src/Tightenco/Support/Collection.php @@ -271,6 +271,8 @@ public function crossJoin(...$lists) */ public function dd(...$args) { + http_response_code(500); + call_user_func_array([$this, 'dump'], $args); die(1); @@ -391,7 +393,7 @@ public function every($key, $operator = null, $value = null) public function except($keys) { if ($keys instanceof self) { - $keys = $keys->keys()->all(); + $keys = $keys->all(); } elseif (! is_array($keys)) { $keys = func_get_args(); } @@ -658,6 +660,12 @@ public function get($key, $default = null) */ public function groupBy($groupBy, $preserveKeys = false) { + if (is_array($groupBy)) { + $nextGroups = $groupBy; + + $groupBy = array_shift($nextGroups); + } + $groupBy = $this->valueRetriever($groupBy); $results = []; @@ -680,7 +688,13 @@ public function groupBy($groupBy, $preserveKeys = false) } } - return new static($results); + $result = new static($results); + + if (! empty($nextGroups)) { + return $result->map->groupBy($nextGroups, $preserveKeys); + } + + return $result; } /** @@ -1052,6 +1066,10 @@ public function only($keys) return new static($this->items); } + if ($keys instanceof self) { + $keys = $keys->all(); + } + $keys = is_array($keys) ? $keys : func_get_args(); return new static(Arr::only($this->items, $keys)); diff --git a/src/Tightenco/Support/alias.php b/src/Tightenco/Support/alias.php index 696c9f5..7bdb211 100644 --- a/src/Tightenco/Support/alias.php +++ b/src/Tightenco/Support/alias.php @@ -1,20 +1,35 @@ Tightenco\Support\Collection::class, - Illuminate\Support\Arr::class => Tightenco\Support\Arr::class, - Illuminate\Support\Carbon::class => Tightenco\Support\Carbon::class, - Illuminate\Support\HigherOrderCollectionProxy::class => Tightenco\Support\HigherOrderCollectionProxy::class, - Illuminate\Support\HigherOrderTapProxy::class => Tightenco\Support\HigherOrderTapProxy::class, - Illuminate\Support\HtmlString::class => Tightenco\Support\HtmlString::class, - Illuminate\Support\Optional::class => Tightenco\Support\Optional::class, - Illuminate\Support\Str::class => Tightenco\Support\Str::class, - Illuminate\Support\Debug\Dumper::class => Tightenco\Support\Debug\Dumper::class, - Illuminate\Support\Debug\HtmlDumper::class => Tightenco\Support\Debug\HtmlDumper::class, + /** + * Contracts + */ + Tightenco\Contracts\Support\Arrayable::class => Illuminate\Contracts\Support\Arrayable::class, + Tightenco\Contracts\Support\Htmlable::class => Illuminate\Contracts\Support\Htmlable::class, + Tightenco\Contracts\Support\Jsonable::class => Illuminate\Contracts\Support\Jsonable::class, + + /** + * Support + */ + Tightenco\Support\Arr::class => Illuminate\Support\Arr::class, + Tightenco\Support\Collection::class => Illuminate\Support\Collection::class, + Tightenco\Support\HigherOrderCollectionProxy::class => Illuminate\Support\HigherOrderCollectionProxy::class, + Tightenco\Support\HtmlString::class => Illuminate\Support\HtmlString::class, + + /** + * Support\Debug + */ + Tightenco\Support\Debug\Dumper::class => Illuminate\Support\Debug\Dumper::class, + Tightenco\Support\Debug\HtmlDumper::class => Illuminate\Support\Debug\HtmlDumper::class, + + /** + * Support\Traits + */ + Tightenco\Support\Traits\Macroable::class => Illuminate\Support\Traits\Macroable::class, ]; -foreach ($aliases as $illuminate => $tighten) { - if (class_exists($illuminate) && ! interface_exists($illuminate) && ! trait_exists($illuminate)) { - class_alias($illuminate, $tighten); +foreach ($aliases as $tighten => $illuminate) { + if (! class_exists($illuminate) && ! interface_exists($illuminate) && ! trait_exists($illuminate)) { + class_alias($tighten, $illuminate); } } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 2f6c949..6d741d2 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -973,11 +973,9 @@ public function testExcept() $this->assertEquals(['first' => 'Taylor'], $data->except(['last', 'email', 'missing'])->all()); $this->assertEquals(['first' => 'Taylor'], $data->except('last', 'email', 'missing')->all()); - $this->assertEquals(['first' => 'Taylor'], $data->except(collect(['last' => 'Otwell', 'email' => 'taylorotwell@gmail.com', 'missing']))->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(['last'])->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except('last')->all()); - $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(collect(['last' => 'Otwell']))->all()); } public function testPluckWithArrayAndObjectValues() @@ -1601,6 +1599,48 @@ public function testGroupByClosureWhereItemsHaveMultipleGroupsPreservingKeys() $this->assertEquals($expected_result, $result->toArray()); } + public function testGroupByMultiLevelAndClosurePreservingKeys() + { + $data = new Collection([ + 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']], + 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']], + 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']], + 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']], + ]); + + $result = $data->groupBy([ + 'skilllevel', + function ($item) { + return $item['roles']; + }, + ], true); + + $expected_result = [ + 1 => [ + 'Role_1' => [ + 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']], + 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']], + ], + 'Role_3' => [ + 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']], + ], + 'Role_2' => [ + 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']], + ], + ], + 2 => [ + 'Role_1' => [ + 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']], + ], + 'Role_2' => [ + 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']], + ], + ], + ]; + + $this->assertEquals($expected_result, $result->toArray()); + } + public function testKeyByAttribute() { $data = new Collection([['rating' => 1, 'name' => '1'], ['rating' => 2, 'name' => '2'], ['rating' => 3, 'name' => '3']]); @@ -1928,9 +1968,11 @@ public function testOnly() $this->assertEquals($data->all(), $data->only(null)->all()); $this->assertEquals(['first' => 'Taylor'], $data->only(['first', 'missing'])->all()); $this->assertEquals(['first' => 'Taylor'], $data->only('first', 'missing')->all()); + $this->assertEquals(['first' => 'Taylor'], $data->only(collect(['first', 'missing']))->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(['first', 'email'])->all()); $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only('first', 'email')->all()); + $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(collect(['first', 'email']))->all()); } public function testGettingAvgItemsFromCollection() @@ -2422,6 +2464,12 @@ public function testUnlessDefault() $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); } + + public function testGetWithNullReturnsNull() + { + $collection = new Collection([1, 2, 3]); + $this->assertNull($collection->get(null)); + } } class TestSupportCollectionHigherOrderItem