Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #309 from tighten/v9.18.0-changes
Browse files Browse the repository at this point in the history
v9.18.0 changes
  • Loading branch information
jamisonvalenta authored Aug 19, 2022
2 parents 8e71f43 + 3447a51 commit d605f6e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/Collect/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tightenco\Collect\Support;

use ArgumentCountError;
use ArrayAccess;
use Tightenco\Collect\Support\Traits\Macroable;
use InvalidArgumentException;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Collect/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ public function sortDesc($options = SORT_REGULAR)
/**
* Sort the collection using the given callback.
*
* @param array<array-key, (callable(TValue, TKey): mixed)|array<array-key, string>|(callable(TValue, TKey): mixed)|string $callback
* @param array<array-key, (callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
* @param int $options
* @param bool $descending
* @return static
Expand Down Expand Up @@ -1359,7 +1359,7 @@ public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
/**
* Sort the collection using multiple comparisons.
*
* @param array<array-key, (callable(TValue, TKey): mixed)|array<array-key, string> $comparisons
* @param array<array-key, (callable(TValue, TKey): mixed)|string|array{string, string}> $comparisons
* @return static
*/
protected function sortByMany(array $comparisons = [])
Expand Down Expand Up @@ -1401,7 +1401,7 @@ protected function sortByMany(array $comparisons = [])
/**
* Sort the collection in descending order using the given callback.
*
* @param array<array-key, (callable(TValue, TKey): mixed)|array<array-key, string>|(callable(TValue, TKey): mixed)|string $callback
* @param array<array-key, (callable(TValue, TKey): mixed)|string|array{string, string}>|(callable(TValue, TKey): mixed)|string $callback
* @param int $options
* @return static
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Collect/Support/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\VarDumper\VarDumper;
use Traversable;
use UnexpectedValueException;
use UnitEnum;

/**
* @template TKey of array-key
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/Enums.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tightenco\Collect\Tests\Support;

enum TestEnum
{
case A;
}

enum TestBackedEnum: int
{
case A = 1;
}
9 changes: 9 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,15 @@ public function testMap()
$this->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');
Expand Down
24 changes: 24 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
use Symfony\Component\VarDumper\VarDumper;
use UnexpectedValueException;

if (PHP_VERSION_ID >= 80100) {
include_once 'Enums.php';
}

class SupportCollectionTest extends TestCase
{
/**
Expand Down Expand Up @@ -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
*/
Expand Down
3 changes: 2 additions & 1 deletion upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ carriageReturn="
)

tests=(
'Support/Enums.php'
'Support/SupportCollectionTest.php'
'Support/SupportArrTest.php'
'Support/SupportMacroableTest.php'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d605f6e

Please sign in to comment.