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
26 changes: 19 additions & 7 deletions formwork/src/Data/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,28 @@ public function group(callable $callback): array
}

/**
* Get the value corresponding to the specified key from each item in the collection
* Extract an associative array of values corresponding to the specified key from the collection items
*
* Typed collection should implement their own version of this method, optimised for their data type
* Typed collections should implement their own version of this method, optimised for their data type
*
* @return array<mixed>
*/
public function pluck(string $key, mixed $default = null): array
public function extract(string $key, mixed $default = null): array
{
// @phpstan-ignore argument.templateType
return Arr::pluck($this->data, $key, $default);
return Arr::extract($this->data, $key, $default);
}

/**
* Extract a list of values corresponding to the specified key from the collection items
*
* This method is similar to `extract()` but does not preserve keys
*
* @return array<mixed>
*/
public function pluck(string $key, mixed $default = null): array
{
return array_values($this->extract($key, $default));
}

/**
Expand All @@ -408,7 +420,7 @@ public function flatten(int $depth = PHP_INT_MAX): static
*/
public function filterBy(string $key, mixed $value = true, mixed $default = null, ?bool $strict = null): static
{
$values = $this->pluck($key, $default);
$values = $this->extract($key, $default);

if (is_callable($value)) {
$values = Arr::map($values, $value);
Expand All @@ -429,7 +441,7 @@ public function sortBy(
bool $caseSensitive = false,
bool $preserveKeys = true,
): static {
return $this->sort($direction, $type, $this->pluck($key), $caseSensitive, $preserveKeys);
return $this->sort($direction, $type, $this->extract($key), $caseSensitive, $preserveKeys);
}

/**
Expand All @@ -439,7 +451,7 @@ public function sortBy(
*/
public function groupBy(string $key, mixed $default = null): array
{
$values = $this->pluck($key, $default);
$values = $this->extract($key, $default);
return $this->group(fn($v, $k) => $values[$k]);
}

Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Fields/FieldCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function model(): ?Model
return $this->model;
}

public function pluck(string $key, mixed $default = null): array
public function extract(string $key, mixed $default = null): array
{
return $this->everyItem()->get($key, $default)->toArray();
}
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Pages/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function defaults(): array
];

// Merge with scheme default field values
$defaults = [...$defaults, ...Arr::reject($this->fields()->pluck('default'), fn($value) => $value === null)];
$defaults = [...$defaults, ...Arr::reject($this->fields()->extract('default'), fn($value) => $value === null)];

// If the page doesn't have a route, by default it won't be routable nor cacheable
if ($this->route() === null) {
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Pages/PageCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function paginate(int $length, int $currentPage): self
return $pageCollection;
}

public function pluck(string $key, mixed $default = null): array
public function extract(string $key, mixed $default = null): array
{
return $this->everyItem()->get($key, $default)->toArray();
}
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Panel/Controllers/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private function updateFileMetadata(File $file, FieldCollection $fieldCollection

$scheme = $file->scheme();

$defaults = $scheme->fields()->pluck('default');
$defaults = $scheme->fields()->extract('default');

foreach ($fieldCollection as $field) {
if ($field->isEmpty() || (Arr::has($defaults, $field->name()) && Arr::get($defaults, $field->name()) === $field->value())) {
Expand Down
6 changes: 4 additions & 2 deletions formwork/src/Utils/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,9 @@ public static function find(array $array, callable $callback): mixed
}

/**
* Get the value corresponding to the specified key from each element of an array
* Extract an array of values corresponding to the specified key from the given array
*
* The original array keys are preserved
*
* @param array<TKey, array<K, V>> $array
*
Expand All @@ -545,7 +547,7 @@ public static function find(array $array, callable $callback): mixed
* @template K of array-key
* @template V
*/
public static function pluck(array $array, string $key, mixed $default = null): array
public static function extract(array $array, string $key, mixed $default = null): array
{
return self::map($array, fn($value) => self::get(self::from($value), $key, $default));
}
Expand Down