diff --git a/src/Collect/Support/Arr.php b/src/Collect/Support/Arr.php index 47ef399..55059be 100644 --- a/src/Collect/Support/Arr.php +++ b/src/Collect/Support/Arr.php @@ -2,6 +2,7 @@ namespace Tightenco\Collect\Support; +use ArgumentCountError; use ArrayAccess; use Tightenco\Collect\Support\Traits\Macroable; use InvalidArgumentException; @@ -458,7 +459,7 @@ public static function join($array, $glue, $finalGlue = '') * Key an associative array by a field or using a callback. * * @param array $array - * @param callable|array|string + * @param callable|array|string $keyBy * @return array */ public static function keyBy($array, $keyBy) @@ -555,7 +556,11 @@ public static function map(array $array, callable $callback) { $keys = array_keys($array); - $items = array_map($callback, $array, $keys); + try { + $items = array_map($callback, $array, $keys); + } catch (ArgumentCountError) { + $items = array_map($callback, $array); + } return array_combine($keys, $items); } diff --git a/src/Collect/Support/Collection.php b/src/Collect/Support/Collection.php index f4bc2de..40b612a 100644 --- a/src/Collect/Support/Collection.php +++ b/src/Collect/Support/Collection.php @@ -1321,7 +1321,7 @@ public function sortDesc($options = SORT_REGULAR) /** * Sort the collection using the given callback. * - * @param array|(callable(TValue, TKey): mixed)|string $callback + * @param array|(callable(TValue, TKey): mixed)|string $callback * @param int $options * @param bool $descending * @return static @@ -1359,7 +1359,7 @@ public function sortBy($callback, $options = SORT_REGULAR, $descending = false) /** * Sort the collection using multiple comparisons. * - * @param array $comparisons + * @param array $comparisons * @return static */ protected function sortByMany(array $comparisons = []) @@ -1401,7 +1401,7 @@ protected function sortByMany(array $comparisons = []) /** * Sort the collection in descending order using the given callback. * - * @param array|(callable(TValue, TKey): mixed)|string $callback + * @param array|(callable(TValue, TKey): mixed)|string $callback * @param int $options * @return static */ diff --git a/src/Collect/Support/Traits/EnumeratesValues.php b/src/Collect/Support/Traits/EnumeratesValues.php index e6d38b4..3104500 100644 --- a/src/Collect/Support/Traits/EnumeratesValues.php +++ b/src/Collect/Support/Traits/EnumeratesValues.php @@ -15,6 +15,7 @@ use Symfony\Component\VarDumper\VarDumper; use Traversable; use UnexpectedValueException; +use UnitEnum; /** * @template TKey of array-key @@ -1005,6 +1006,8 @@ protected function getArrayableItems($items) return (array) $items->jsonSerialize(); } elseif ($items instanceof Traversable) { return iterator_to_array($items); + } elseif ($items instanceof UnitEnum) { + return [$items]; } return (array) $items; diff --git a/tests/Support/Enums.php b/tests/Support/Enums.php new file mode 100644 index 0000000..601b3ac --- /dev/null +++ b/tests/Support/Enums.php @@ -0,0 +1,13 @@ +assertEquals(['first' => 'taylor', 'last' => 'otwell'], $data); } + public function testMapByReference() + { + $data = ['first' => 'taylor', 'last' => 'otwell']; + $mapped = Arr::map($data, 'strrev'); + + $this->assertEquals(['first' => 'rolyat', 'last' => 'llewto'], $mapped); + $this->assertEquals(['first' => 'taylor', 'last' => 'otwell'], $data); + } + public function testPrepend() { $array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero'); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 9eb16a2..ddcc24e 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -24,6 +24,10 @@ use Symfony\Component\VarDumper\VarDumper; use UnexpectedValueException; +if (PHP_VERSION_ID >= 80100) { + include_once 'Enums.php'; +} + class SupportCollectionTest extends TestCase { /** @@ -4333,6 +4337,26 @@ public function testCollectionFromTraversableWithKeys($collection) $this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $data->toArray()); } + /** + * @dataProvider collectionClassProvider + * @requires PHP >= 8.1 + */ + public function testCollectionFromEnum($collection) + { + $data = new $collection(TestEnum::A); + $this->assertEquals([TestEnum::A], $data->toArray()); + } + + /** + * @dataProvider collectionClassProvider + * @requires PHP >= 8.1 + */ + public function testCollectionFromBackedEnum($collection) + { + $data = new $collection(TestBackedEnum::A); + $this->assertEquals([TestBackedEnum::A], $data->toArray()); + } + /** * @dataProvider collectionClassProvider */ diff --git a/upgrade.sh b/upgrade.sh index 7e30cf5..5a66179 100755 --- a/upgrade.sh +++ b/upgrade.sh @@ -147,6 +147,7 @@ carriageReturn=" ) tests=( + 'Support/Enums.php' 'Support/SupportCollectionTest.php' 'Support/SupportArrTest.php' 'Support/SupportMacroableTest.php' @@ -380,7 +381,7 @@ function getCurrentVersionFromGitHub() echo Getting current version from $repository... if [ -z "$requestedVersion" ]; then - collectionVersion=$(git ls-remote $repository --tags v9.17\* | 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.18\* | 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