Skip to content
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
4 changes: 2 additions & 2 deletions ci/convergence-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ event_fire=0
config_getEnvironment=3
where_raw=0
eloquent_lists=0
macroable_trait=14
macroable_trait=12
softdeleting_trait=2
legacy_contracts=50
legacy_contracts=37
pagination_getters=0
route_filters=4
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"src/Illuminate/Queue/IlluminateQueueClosure.php"
],
"files": [
"src/Illuminate/Support/helpers.php"
"src/Illuminate/Support/helpers.php",
"src/Illuminate/Support/functions.php"
],
"psr-4": {
"L4\\": "src/L4/",
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Contracts/Support/CanBeEscapedWhenCastToString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Illuminate\Contracts\Support;

interface CanBeEscapedWhenCastToString
{
/**
* Indicate that the object's string representation should be escaped when __toString is invoked.
*
* @param bool $escape
* @return $this
*/
public function escapeWhenCastingToString($escape = true);
}
42 changes: 12 additions & 30 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ public function add($item)
* @return bool
*/
#[\Override]
public function contains($key)
public function contains($key, $operator = null, $value = null)
{
if (func_num_args() > 1)
{
return parent::contains($key, $operator, $value);
}

return ! is_null($this->find($key));
}

Expand All @@ -83,34 +88,6 @@ public function fetch($key)
return new static(array_fetch($this->toArray(), $key));
}

/**
* Get the max value of a given key.
*
* @param string $key
* @return mixed
*/
public function max($key)
{
return $this->reduce(function($result, $item) use ($key)
{
return (is_null($result) || $item->{$key} > $result) ? $item->{$key} : $result;
});
}

/**
* Get the min value of a given key.
*
* @param string $key
* @return mixed
*/
public function min($key)
{
return $this->reduce(function($result, $item) use ($key)
{
return (is_null($result) || $item->{$key} < $result) ? $item->{$key} : $result;
});
}

/**
* Get the array of primary keys
*
Expand Down Expand Up @@ -194,8 +171,13 @@ public function intersect($items)
* @return static
*/
#[\Override]
public function unique()
public function unique($key = null, $strict = false)
{
if ( ! is_null($key))
{
return parent::unique($key, $strict);
}

$dictionary = $this->getDictionary();

return new static(array_values($dictionary));
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function gatherKeysByType($type)
{
return head($models)->{$foreign};

})->unique();
})->unique()->values();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ protected function getRouteQueryString(array $parameters)
*/
protected function getStringParameters(array $parameters)
{
return array_where($parameters, function($k, $v) { return is_string($k); });
return array_where($parameters, function($v, $k) { return is_string($k); });
}

/**
Expand All @@ -372,7 +372,7 @@ protected function getStringParameters(array $parameters)
*/
protected function getNumericParameters(array $parameters)
{
return array_where($parameters, function($k, $v) { return is_numeric($k); });
return array_where($parameters, function($v, $k) { return is_numeric($k); });
}

/**
Expand Down
116 changes: 104 additions & 12 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,112 @@
use ArgumentCountError;
use ArrayAccess;
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Traits\MacroableTrait;
use InvalidArgumentException;
use JsonSerializable;
use Traversable;
use WeakMap;

class Arr {

use MacroableTrait;

/**
* Get an array from the given items.
*
* @param mixed $items
* @return array
*/
public static function from($items)
{
return match (true) {
is_array($items) => $items,
$items instanceof Enumerable => $items->all(),
$items instanceof Arrayable => $items->toArray(),
$items instanceof WeakMap => iterator_to_array($items, false),
$items instanceof Traversable => iterator_to_array($items),
$items instanceof Jsonable => json_decode($items->toJson(), true),
$items instanceof JsonSerializable => (array) $items->jsonSerialize(),
is_object($items) => (array) $items,
default => throw new InvalidArgumentException('Items cannot be represented by a scalar value.'),
};
}

/**
* Run an associative map over each of the items.
*
* The callback should return an associative array with a single key/value pair.
*
* @param array $array
* @param callable $callback
* @return array
*/
public static function mapWithKeys(array $array, callable $callback)
{
$result = [];

foreach ($array as $key => $value) {
$assoc = $callback($value, $key);

foreach ($assoc as $mapKey => $mapValue) {
$result[$mapKey] = $mapValue;
}
}

return $result;
}

/**
* Partition the array into two arrays using the given callback.
*
* @param array $array
* @param callable $callback
* @return array
*/
public static function partition($array, callable $callback)
{
$passed = [];
$failed = [];

foreach ($array as $key => $item) {
if ($callback($item, $key)) {
$passed[$key] = $item;
} else {
$failed[$key] = $item;
}
}

return [$passed, $failed];
}

/**
* Select an array of values from an array.
*
* @param array $array
* @param array|string $keys
* @return array
*/
public static function select($array, $keys)
{
$keys = static::wrap($keys);

return static::map($array, function ($item) use ($keys) {
$result = [];

foreach ($keys as $key) {
if (Arr::accessible($item) && Arr::exists($item, $key)) {
$result[$key] = $item[$key];
} elseif (is_object($item) && isset($item->{$key})) {
$result[$key] = $item->{$key};
}
}

return $result;
});
}

/**
* Determine whether the given value is array accessible.
*
Expand Down Expand Up @@ -842,22 +941,15 @@ public static function toCssClasses(array $array): string
}

/**
* Filter the array using the given Closure.
* Filter the array using the given callback.
*
* @param array $array
* @param \Closure $callback
* @param callable $callback
* @return array
*/
public static function where($array, Closure $callback): array
public static function where($array, callable $callback): array
{
$filtered = array();

foreach ($array as $key => $value)
{
if (call_user_func($callback, $key, $value)) $filtered[$key] = $value;
}

return $filtered;
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
}

/**
Expand All @@ -868,7 +960,7 @@ public static function where($array, Closure $callback): array
*/
public static function whereNotNull($array)
{
return static::where($array, function ($key, $value) {
return static::where($array, function ($value, $key) {
return ! is_null($value);
});
}
Expand Down
Loading
Loading