8000 v9.49.0 changes by jamisonvalenta · Pull Request #340 · tighten/collect · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

v9.49.0 changes #340

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Collect/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
37 changes: 36 additions & 1 deletion src/Collect/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, TValue>|iterable<array-key, TValue> $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<TKey, TValue>|iterable<TKey, TValue> $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<array-key, TValue>|iterable<array-key, TValue> $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.
*
Expand Down Expand Up @@ -872,7 +907,7 @@ public function nth($step, $offset = 0)
/**
* Get the items with the specified keys.
*
* @param \Tightenco\Collect\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
* @param \Tightenco\Collect\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string|null $keys
* @return static
*/
public function only($keys)
Expand Down
35 changes: 35 additions & 0 deletions src/Collect/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, TValue>|iterable<array-key, TValue> $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<TKey, TValue>|iterable<TKey, TValue> $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<array-key, TValue>|iterable<array-key, TValue> $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.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
62 changes: 62 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/files/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/files/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
0