diff --git a/formwork/src/Data/AbstractCollection.php b/formwork/src/Data/AbstractCollection.php index bbb6c5a3c..3f7dcec85 100644 --- a/formwork/src/Data/AbstractCollection.php +++ b/formwork/src/Data/AbstractCollection.php @@ -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 */ - 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 + */ + public function pluck(string $key, mixed $default = null): array + { + return array_values($this->extract($key, $default)); } /** @@ -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); @@ -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); } /** @@ -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]); } diff --git a/formwork/src/Fields/FieldCollection.php b/formwork/src/Fields/FieldCollection.php index a8074b116..7151279ef 100644 --- a/formwork/src/Fields/FieldCollection.php +++ b/formwork/src/Fields/FieldCollection.php @@ -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(); } diff --git a/formwork/src/Pages/Page.php b/formwork/src/Pages/Page.php index 2849a0ea4..ac05ba8ec 100644 --- a/formwork/src/Pages/Page.php +++ b/formwork/src/Pages/Page.php @@ -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) { diff --git a/formwork/src/Pages/PageCollection.php b/formwork/src/Pages/PageCollection.php index ae0aa02d4..77077a8ae 100644 --- a/formwork/src/Pages/PageCollection.php +++ b/formwork/src/Pages/PageCollection.php @@ -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(); } diff --git a/formwork/src/Panel/Controllers/FilesController.php b/formwork/src/Panel/Controllers/FilesController.php index 2314657bd..e41971518 100644 --- a/formwork/src/Panel/Controllers/FilesController.php +++ b/formwork/src/Panel/Controllers/FilesController.php @@ -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())) { diff --git a/formwork/src/Utils/Arr.php b/formwork/src/Utils/Arr.php index f321bf7fc..c67e670db 100644 --- a/formwork/src/Utils/Arr.php +++ b/formwork/src/Utils/Arr.php @@ -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> $array * @@ -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)); }