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

Commit

Permalink
Merge pull request #294 from tighten/v9.6.0-changes
Browse files Browse the repository at this point in the history
v9.6.0 changes
  • Loading branch information
jamisonvalenta authored Apr 1, 2022
2 parents 0e73894 + 5efdcee commit 54cc5f0
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/Collect/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static function undot($array)
* Get all of the given array except for a specified array of keys.
*
* @param array $array
* @param array|string $keys
* @param array|string|int|float $keys
* @return array
*/
public static function except($array, $keys)
Expand All @@ -169,6 +169,10 @@ public static function exists($array, $key)
return $array->offsetExists($key);
}

if (is_float($key)) {
$key = (string) $key;
}

return array_key_exists($key, $array);
}

Expand Down Expand Up @@ -252,7 +256,7 @@ public static function flatten($array, $depth = INF)
* Remove one or many array items from a given array using "dot" notation.
*
* @param array $array
* @param array|string $keys
* @param array|string|int|float $keys
* @return void
*/
public static function forget(&$array, $keys)
Expand Down
10 changes: 7 additions & 3 deletions src/Collect/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,11 @@ public function groupBy($groupBy, $preserveKeys = false)
}

foreach ($groupKeys as $groupKey) {
$groupKey = is_bool($groupKey) ? (int) $groupKey : $groupKey;
$groupKey = match (true) {
is_bool($groupKey) => (int) $groupKey,
$groupKey instanceof \Stringable => (string) $groupKey,
default => $groupKey,
};

if (! array_key_exists($groupKey, $results)) {
$results[$groupKey] = new static;
Expand Down Expand Up @@ -842,8 +846,8 @@ public function nth($step, $offset = 0)

$position = 0;

foreach ($this->items as $item) {
if ($position % $step === $offset) {
foreach ($this->slice($offset)->items as $item) {
if ($position % $step === 0) {
$new[] = $item;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Collect/Support/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,10 @@ public function whereNotInStrict($key, $values);
/**
* Filter the items, removing any items that don't match the given type(s).
*
* @param class-string|array<array-key, class-string> $type
* @return static
* @template TWhereInstanceOf
*
* @param class-string<TWhereInstanceOf>|array<array-key, class-string<TWhereInstanceOf>> $type
* @return static<TKey, TWhereInstanceOf>
*/
public function whereInstanceOf($type);

Expand Down
4 changes: 2 additions & 2 deletions src/Collect/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,8 @@ public function nth($step, $offset = 0)
return new static(function () use ($step, $offset) {
$position = 0;

foreach ($this as $item) {
if ($position % $step === $offset) {
foreach ($this->slice($offset) as $item) {
if ($position % $step === 0) {
yield $item;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Collect/Support/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,10 @@ public function whereNotInStrict($key, $values)
/**
* Filter the items, removing any items that don't match the given type(s).
*
* @param class-string|array<array-key, class-string> $type
* @return static
* @template TWhereInstanceOf
*
* @param class-string<TWhereInstanceOf>|array<array-key, class-string<TWhereInstanceOf>> $type
* @return static<TKey, TWhereInstanceOf>
*/
public function whereInstanceOf($type)
{
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public function testExcept()
$this->assertEquals(['name' => 'taylor'], Arr::except($array, 'framework'));
$this->assertEquals(['name' => 'taylor', 'framework' => ['name' => 'Laravel']], Arr::except($array, 'framework.language'));
$this->assertEquals(['framework' => ['language' => 'PHP']], Arr::except($array, ['name', 'framework.name']));

$array = [1 => 'hAz', 2 => [5 => 'foo', 12 => 'baz']];
$this->assertEquals([1 => 'hAz'], Arr::except($array, 2));
$this->assertEquals([1 => 'hAz', 2 => [12 => 'baz']], Arr::except($array, 2.5));
}

public function testExists()
Expand Down Expand Up @@ -947,6 +951,14 @@ public function testForget()
$array = ['emails' => ['[email protected]' => ['name' => 'Joe'], 'jane@localhost' => ['name' => 'Jane']]];
Arr::forget($array, ['[email protected]', 'emails.jane@localhost']);
$this->assertEquals(['emails' => ['[email protected]' => ['name' => 'Joe']]], $array);

$array = ['name' => 'hAz', '1' => 'test', 2 => 'bAz'];
Arr::forget($array, 1);
$this->assertEquals(['name' => 'hAz', 2 => 'bAz'], $array);

$array = [2 => [1 =>'products', 3 => 'users']];
Arr::forget($array, 2.3);
$this->assertEquals([2 => [1 =>'products']], $array);
}

public function testWrap()
Expand Down
26 changes: 26 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,8 @@ public function testNth($collection)
$this->assertEquals(['b', 'f'], $data->nth(4, 1)->all());
$this->assertEquals(['c'], $data->nth(4, 2)->all());
$this->assertEquals(['d'], $data->nth(4, 3)->all());
$this->assertEquals(['c', 'e'], $data->nth(2, 2)->all());
$this->assertEquals(['c', 'd', 'e', 'f'], $data->nth(1, 2)->all());
}

/**
Expand Down Expand Up @@ -3023,6 +3025,30 @@ public function testGroupByAttribute($collection)
$this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
}

/**
* @dataProvider collectionClassProvider
*/
public function testGroupByAttributeWithStringableKey($collection)
{
$data = new $collection($payload = [
['name' => Str::of('Laravel'), 'url' => '1'],
['name' => new HtmlString('Laravel'), 'url' => '1'],
['name' => new class()
{
public function __toString()
{
return 'Framework';
}
}, 'url' => '2', ],
]);

$result = $data->groupBy('name');
$this->assertEquals(['Laravel' => [$payload[0], $payload[1]], 'Framework' => [$payload[2]]], $result->toArray());

$result = $data->groupBy('url');
$this->assertEquals(['1' => [$payload[0], $payload[1]], '2' => [$payload[2]]], $result->toArray());
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down
9 changes: 3 additions & 6 deletions tests/files/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,6 @@ public static function mask($string, $character, $index, $length = null, $encodi
return $string;
}

if (is_null($length) && PHP_MAJOR_VERSION < 8) {
$length = mb_strlen($string, $encoding);
}

$segment = mb_substr($string, $index, $length, $encoding);

if ($segment === '') {
Expand Down Expand Up @@ -1024,11 +1020,12 @@ public static function ucsplit($string)
* Get the number of words a string contains.
*
* @param string $string
* @param string|null $characters
* @return int
*/
public static function wordCount($string)
public static function wordCount($string, $characters = null)
{
return str_word_count($string);
return str_word_count($string, 0, $characters);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/files/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public function append(...$values)
return new static($this->value.implode('', $values));
}

/**
* Append a new line to the string.
*
* @param int $count
* @return $this
*/
public function newLine($count = 1)
{
return $this->append(str_repeat(PHP_EOL, $count));
}

/**
* Transliterate a UTF-8 value to ASCII.
*
Expand Down
2 changes: 1 addition & 1 deletion upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function getCurrentVersionFromGitHub()
echo Getting current version from $repository...

if [ -z "$requestedVersion" ]; then
collectionVersion=$(git ls-remote $repository --tags v9.5\* | 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\* | 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 54cc5f0

Please sign in to comment.