diff --git a/src/Collect/Support/Arr.php b/src/Collect/Support/Arr.php index c40bb6a..e47cfba 100644 --- a/src/Collect/Support/Arr.php +++ b/src/Collect/Support/Arr.php @@ -731,6 +731,18 @@ public static function sort($array, $callback = null) return Collection::make($array)->sortBy($callback)->all(); } + /** + * Sort the array in descending order using the given callback or "dot" notation. + * + * @param array $array + * @param callable|array|string|null $callback + * @return array + */ + public static function sortDesc($array, $callback = null) + { + return Collection::make($array)->sortByDesc($callback)->all(); + } + /** * Recursively sort an array by keys and values. * diff --git a/src/Collect/Support/Collection.php b/src/Collect/Support/Collection.php index d50ebc2..c9045f7 100644 --- a/src/Collect/Support/Collection.php +++ b/src/Collect/Support/Collection.php @@ -627,6 +627,41 @@ public function intersect($items) return new static(array_intersect($this->items, $this->getArrayableItems($items))); } + /** + * Intersect the collection with the given items, using the callback. + * + * @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $items + * @param callable(TValue, TValue): int $callback + * @return static + */ + public function intersectUsing($items, callable $callback) + { + return new static(array_uintersect($this->items, $this->getArrayableItems($items), $callback)); + } + + /** + * Intersect the collection with the given items with additional index check. + * + * @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $items + * @return static + */ + public function intersectAssoc($items) + { + return new static(array_intersect_assoc($this->items, $this->getArrayableItems($items))); + } + + /** + * Intersect the collection with the given items with additional index check, using the callback. + * + * @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $items + * @param callable(TValue, TValue): int $callback + * @return static + */ + public function intersectAssocUsing($items, callable $callback) + { + return new static(array_intersect_uassoc($this->items, $this->getArrayableItems($items), $callback)); + } + /** * Intersect the collection with the given items by key. * @@ -872,7 +907,7 @@ public function nth($step, $offset = 0) /** * Get the items with the specified keys. * - * @param \Tightenco\Collect\Support\Enumerable|array|string $keys + * @param \Tightenco\Collect\Support\Enumerable|array|string|null $keys * @return static */ public function only($keys) diff --git a/src/Collect/Support/LazyCollection.php b/src/Collect/Support/LazyCollection.php index eb94e0d..f9caadc 100644 --- a/src/Collect/Support/LazyCollection.php +++ b/src/Collect/Support/LazyCollection.php @@ -632,6 +632,41 @@ public function intersect($items) return $this->passthru('intersect', func_get_args()); } + /** + * Intersect the collection with the given items, using the callback. + * + * @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $items + * @param callable(TValue, TValue): int $callback + * @return static + */ + public function intersectUsing() + { + return $this->passthru('intersectUsing', func_get_args()); + } + + /** + * Intersect the collection with the given items with additional index check. + * + * @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $items + * @return static + */ + public function intersectAssoc($items) + { + return $this->passthru('intersectAssoc', func_get_args()); + } + + /** + * Intersect the collection with the given items with additional index check, using the callback. + * + * @param \Tightenco\Collect\Contracts\Support\Arrayable|iterable $items + * @param callable(TValue, TValue): int $callback + * @return static + */ + public function intersectAssocUsing($items, callable $callback) + { + return $this->passthru('intersectAssocUsing', func_get_args()); + } + /** * Intersect the collection with the given items by key. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index e9d8c0f..2a1939a 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -843,6 +843,32 @@ public function testSort() $this->assertEquals($expected, $sortedWithDotNotation); } + public function testSortDesc() + { + $unsorted = [ + ['name' => 'Chair'], + ['name' => 'Desk'], + ]; + + $expected = [ + ['name' => 'Desk'], + ['name' => 'Chair'], + ]; + + $sorted = array_values(Arr::sortDesc($unsorted)); + $this->assertEquals($expected, $sorted); + + // sort with closure + $sortedWithClosure = array_values(Arr::sortDesc($unsorted, function ($value) { + return $value['name']; + })); + $this->assertEquals($expected, $sortedWithClosure); + + // sort with dot notation + $sortedWithDotNotation = array_values(Arr::sortDesc($unsorted, 'name')); + $this->assertEquals($expected, $sortedWithDotNotation); + } + public function testSortRecursive() { $array = [ diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 01536f4..6b7be12 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1700,6 +1700,68 @@ public function testIntersectCollection($collection) $this->assertEquals(['first_word' => 'Hello'], $c->intersect(new $collection(['first_world' => 'Hello', 'last_word' => 'World']))->all()); } + /** + * @dataProvider collectionClassProvider + */ + public function testIntersectUsingWithNull($collection) + { + $collect = new $collection(['green', 'brown', 'blue']); + + $this->assertEquals([], $collect->intersectUsing(null, 'strcasecmp')->all()); + } + + /** + * @dataProvider collectionClassProvider + */ + public function testIntersectUsingCollection($collection) + { + $collect = new $collection(['green', 'brown', 'blue']); + + $this->assertEquals(['green', 'brown'], $collect->intersectUsing(new $collection(['GREEN', 'brown', 'yellow']), 'strcasecmp')->all()); + } + + /** + * @dataProvider collectionClassProvider + */ + public function testIntersectAssocWithNull($collection) + { + $array1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']); + + $this->assertEquals([], $array1->intersectAssoc(null)->all()); + } + + /** + * @dataProvider collectionClassProvider + */ + public function testIntersectAssocCollection($collection) + { + $array1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']); + $array2 = new $collection(['a' => 'green', 'b' => 'yellow', 'blue', 'red']); + + $this->assertEquals(['a' => 'green'], $array1->intersectAssoc($array2)->all()); + } + + /** + * @dataProvider collectionClassProvider + */ + public function testIntersectAssocUsingWithNull($collection) + { + $array1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']); + + $this->assertEquals([], $array1->intersectAssocUsing(null, 'strcasecmp')->all()); + } + + /** + * @dataProvider collectionClassProvider + */ + public function testIntersectAssocUsingCollection($collection) + { + $array1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']); + $array2 = new $collection(['a' => 'GREEN', 'B' => 'brown', 'yellow', 'red']); + + $this->assertEquals(['b' => 'brown'], $array1->intersectAssocUsing($array2, 'strcasecmp')->all()); + } + /** * @dataProvider collectionClassProvider */ diff --git a/tests/Support/SupportLazyCollectionIsLazyTest.php b/tests/Support/SupportLazyCollectionIsLazyTest.php index 6d64315..d48c30b 100644 --- a/tests/Support/SupportLazyCollectionIsLazyTest.php +++ b/tests/Support/SupportLazyCollectionIsLazyTest.php @@ -508,6 +508,39 @@ public function testIntersectIsLazy() }); } + public function testIntersectUsingIsLazy() + { + $this->assertDoesNotEnumerate(function ($collection) { + $collection->intersectUsing([1, 2], 'strcasecmp'); + }); + + $this->assertEnumeratesOnce(function ($collection) { + $collection->intersectUsing([1, 2], 'strcasecmp')->all(); + }); + } + + public function testIntersectAssocIsLazy() + { + $this->assertDoesNotEnumerate(function ($collection) { + $collection->intersectAssoc([1, 2]); + }); + + $this->assertEnumeratesOnce(function ($collection) { + $collection->intersectAssoc([1, 2])->all(); + }); + } + + public function testIntersectAssocUsingIsLazy() + { + $this->assertDoesNotEnumerate(function ($collection) { + $collection->intersectAssocUsing([1, 2], 'strcasecmp'); + }); + + $this->assertEnumeratesOnce(function ($collection) { + $collection->intersectAssocUsing([1, 2], 'strcasecmp')->all(); + }); + } + public function testIntersectByKeysIsLazy() { $this->assertDoesNotEnumerate(function ($collection) { diff --git a/tests/files/Support/Str.php b/tests/files/Support/Str.php index 7d4f72e..73f8688 100644 --- a/tests/files/Support/Str.php +++ b/tests/files/Support/Str.php @@ -1073,7 +1073,7 @@ public static function snake($value, $delimiter = '_') */ public static function squish($value) { - return preg_replace('~(\s|\x{3164})+~u', ' ', preg_replace('~^[\s]+|[\s]+$~u', '', $value)); + return preg_replace('~(\s|\x{3164})+~u', ' ', preg_replace('~^[\s\x{FEFF}]+|[\s\x{FEFF}]+$~u', '', $value)); } /** diff --git a/tests/files/Support/Stringable.php b/tests/files/Support/Stringable.php index c211520..109888c 100644 --- a/tests/files/Support/Stringable.php +++ b/tests/files/Support/Stringable.php @@ -57,7 +57,7 @@ public function afterLast($search) /** * Append the given values to the string. * - * @param array $values + * @param array ...$values * @return static */ public function append(...$values) @@ -545,7 +545,7 @@ public function pluralStudly($count = 2) /** * Prepend the given values to the string. * - * @param array $values + * @param array ...$values * @return static */ public function prepend(...$values) diff --git a/upgrade.sh b/upgrade.sh index 8a8c586..aa1ee13 100755 --- a/upgrade.sh +++ b/upgrade.sh @@ -381,7 +381,7 @@ function getCurrentVersionFromGitHub() echo Getting current version from $repository... if [ -z "$requestedVersion" ]; then - collectionVersion=$(git ls-remote $repository --tags v9.48\* | grep tags/ | grep -v {} | cut -d \/ -f 3 | cut -d v -f 2 | grep -v RC | grep -vi beta | sort -t. -k 1,1n -k 2,2n -k 3,3n| tail -1) + collectionVersion=$(git ls-remote $repository --tags v9.49\* | grep tags/ | grep -v {} | cut -d \/ -f 3 | cut -d v -f 2 | grep -v RC | grep -vi beta | sort -t. -k 1,1n -k 2,2n -k 3,3n| tail -1) else collectionVersion=$requestedVersion fi