From 7594f44a5cb1e528d59cf64e2a6e4d9335e23a7f Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Wed, 15 Jan 2025 14:27:44 -0500 Subject: [PATCH] [11.x] Replace duplicate `ValidatedInput` functions with `InteractsWithData` trait (#54208) * Replace duplicate functions with InteractsWithData trait * Support keys parameter * Add all keys parameter test * Add enums test * Remove unused imports --- src/Illuminate/Support/ValidatedInput.php | 357 ++-------------------- tests/Support/ValidatedInputTest.php | 15 + 2 files changed, 32 insertions(+), 340 deletions(-) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 669ebdeadd0d..f3ac30c79e2a 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -4,13 +4,14 @@ use ArrayIterator; use Illuminate\Contracts\Support\ValidatedData; -use Illuminate\Support\Facades\Date; -use stdClass; +use Illuminate\Support\Traits\InteractsWithData; use Symfony\Component\VarDumper\VarDumper; use Traversable; class ValidatedInput implements ValidatedData { + use InteractsWithData; + /** * The underlying input. * @@ -29,149 +30,6 @@ public function __construct(array $input) $this->input = $input; } - /** - * Determine if the validated input has one or more keys. - * - * @param string|array $key - * @return bool - */ - public function exists($key) - { - return $this->has($key); - } - - /** - * Determine if the validated input has one or more keys. - * - * @param mixed $keys - * @return bool - */ - public function has($keys) - { - $keys = is_array($keys) ? $keys : func_get_args(); - - foreach ($keys as $key) { - if (! Arr::has($this->all(), $key)) { - return false; - } - } - - return true; - } - - /** - * Determine if the validated input contains any of the given keys. - * - * @param string|array $keys - * @return bool - */ - public function hasAny($keys) - { - $keys = is_array($keys) ? $keys : func_get_args(); - - $input = $this->all(); - - return Arr::hasAny($input, $keys); - } - - /** - * Determine if the validated input is missing one or more keys. - * - * @param mixed $keys - * @return bool - */ - public function missing($keys) - { - return ! $this->has($keys); - } - - /** - * Apply the callback if the validated input is missing the given input item key. - * - * @param string $key - * @param callable $callback - * @param callable|null $default - * @return $this|mixed - */ - public function whenMissing($key, callable $callback, ?callable $default = null) - { - if ($this->missing($key)) { - return $callback(data_get($this->all(), $key)) ?: $this; - } - - if ($default) { - return $default(); - } - - return $this; - } - - /** - * Retrieve input from the validated input as a Stringable instance. - * - * @param string $key - * @param mixed $default - * @return \Illuminate\Support\Stringable - */ - public function str($key, $default = null) - { - return $this->string($key, $default); - } - - /** - * Retrieve input from the validated input as a Stringable instance. - * - * @param string $key - * @param mixed $default - * @return \Illuminate\Support\Stringable - */ - public function string($key, $default = null) - { - return Str::of($this->input($key, $default)); - } - - /** - * Get a subset containing the provided keys with values from the input data. - * - * @param mixed $keys - * @return array - */ - public function only($keys) - { - $results = []; - - $input = $this->all(); - - $placeholder = new stdClass; - - foreach (is_array($keys) ? $keys : func_get_args() as $key) { - $value = data_get($input, $key, $placeholder); - - if ($value !== $placeholder) { - Arr::set($results, $key, $value); - } - } - - return $results; - } - - /** - * Get all of the input except for a specified array of items. - * - * @param mixed $keys - * @return array - */ - public function except($keys) - { - $keys = is_array($keys) ? $keys : func_get_args(); - - $results = $this->all(); - - Arr::forget($results, $keys); - - return $results; - } - /** * Merge the validated input with the given array of additional data. * @@ -183,137 +41,37 @@ public function merge(array $items) return new static(array_merge($this->all(), $items)); } - /** - * Get the input as a collection. - * - * @param array|string|null $key - * @return \Illuminate\Support\Collection - */ - public function collect($key = null) - { - return new Collection(is_array($key) ? $this->only($key) : $this->input($key)); - } - /** * Get the raw, underlying input array. * + * @param array|mixed|null $keys * @return array */ - public function all() + public function all($keys = null) { - return $this->input; - } - - /** - * Apply the callback if the validated inputs contains the given input item key. - * - * @param string $key - * @param callable $callback - * @param callable|null $default - * @return $this|mixed - */ - public function whenHas($key, callable $callback, ?callable $default = null) - { - if ($this->has($key)) { - return $callback(data_get($this->all(), $key)) ?: $this; - } - - if ($default) { - return $default(); - } - - return $this; - } - - /** - * Determine if the validated inputs contains a non-empty value for an input item. - * - * @param string|array $key - * @return bool - */ - public function filled($key) - { - $keys = is_array($key) ? $key : func_get_args(); - - foreach ($keys as $value) { - if ($this->isEmptyString($value)) { - return false; - } - } - - return true; - } - - /** - * Determine if the validated inputs contains an empty value for an input item. - * - * @param string|array $key - * @return bool - */ - public function isNotFilled($key) - { - $keys = is_array($key) ? $key : func_get_args(); - - foreach ($keys as $value) { - if (! $this->isEmptyString($value)) { - return false; - } + if (! $keys) { + return $this->input; } - return true; - } - - /** - * Determine if the validated inputs contains a non-empty value for any of the given inputs. - * - * @param string|array $keys - * @return bool - */ - public function anyFilled($keys) - { - $keys = is_array($keys) ? $keys : func_get_args(); - - foreach ($keys as $key) { - if ($this->filled($key)) { - return true; - } - } + $input = []; - return false; - } - - /** - * Apply the callback if the validated inputs contains a non-empty value for the given input item key. - * - * @param string $key - * @param callable $callback - * @param callable|null $default - * @return $this|mixed - */ - public function whenFilled($key, callable $callback, ?callable $default = null) - { - if ($this->filled($key)) { - return $callback(data_get($this->all(), $key)) ?: $this; - } - - if ($default) { - return $default(); + foreach (is_array($keys) ? $keys : func_get_args() as $key) { + Arr::set($input, $key, Arr::get($this->input, $key)); } - return $this; + return $input; } /** - * Determine if the given input key is an empty string for "filled". + * Retrieve data from the instance. * - * @param string $key - * @return bool + * @param string|null $key + * @param mixed $default + * @return mixed */ - protected function isEmptyString($key) + protected function data($key = null, $default = null) { - $value = $this->input($key); - - return ! is_bool($value) && ! is_array($value) && trim((string) $value) === ''; + return $this->input($key, $default); } /** @@ -340,87 +98,6 @@ public function input($key = null, $default = null) ); } - /** - * Retrieve input as a boolean value. - * - * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false. - * - * @param string|null $key - * @param bool $default - * @return bool - */ - public function boolean($key = null, $default = false) - { - return filter_var($this->input($key, $default), FILTER_VALIDATE_BOOLEAN); - } - - /** - * Retrieve input as an integer value. - * - * @param string $key - * @param int $default - * @return int - */ - public function integer($key, $default = 0) - { - return intval($this->input($key, $default)); - } - - /** - * Retrieve input as a float value. - * - * @param string $key - * @param float $default - * @return float - */ - public function float($key, $default = 0.0) - { - return floatval($this->input($key, $default)); - } - - /** - * Retrieve input from the validated inputs as a Carbon instance. - * - * @param string $key - * @param string|null $format - * @param string|null $tz - * @return \Illuminate\Support\Carbon|null - * - * @throws \Carbon\Exceptions\InvalidFormatException - */ - public function date($key, $format = null, $tz = null) - { - if ($this->isNotFilled($key)) { - return null; - } - - if (is_null($format)) { - return Date::parse($this->input($key), $tz); - } - - return Date::createFromFormat($format, $this->input($key), $tz); - } - - /** - * Retrieve input from the validated inputs as an enum. - * - * @template TEnum - * - * @param string $key - * @param class-string $enumClass - * @return TEnum|null - */ - public function enum($key, $enumClass) - { - if ($this->isNotFilled($key) || - ! enum_exists($enumClass) || - ! method_exists($enumClass, 'tryFrom')) { - return null; - } - - return $enumClass::tryFrom($this->input($key)); - } - /** * Dump the validated inputs items and end the script. * diff --git a/tests/Support/ValidatedInputTest.php b/tests/Support/ValidatedInputTest.php index 7b85c1ae30c9..fe821604ba1b 100644 --- a/tests/Support/ValidatedInputTest.php +++ b/tests/Support/ValidatedInputTest.php @@ -17,6 +17,7 @@ public function test_can_access_input() $this->assertSame('Taylor', $input->name); $this->assertSame('Taylor', $input['name']); + $this->assertEquals(['name' => 'Taylor'], $input->all(['name'])); $this->assertEquals(['name' => 'Taylor'], $input->only(['name'])); $this->assertEquals(['name' => 'Taylor'], $input->except(['votes'])); $this->assertEquals(['name' => 'Taylor', 'votes' => 100], $input->all()); @@ -478,6 +479,20 @@ public function test_enum_method() $this->assertNull($input->enum('invalid_enum_value', StringBackedEnum::class)); } + public function test_enums_method() + { + $input = new ValidatedInput([ + 'valid_enum_value' => 'Hello world', + 'invalid_enum_value' => 'invalid', + ]); + + $this->assertEmpty($input->enums('doesnt_exists', StringBackedEnum::class)); + + $this->assertEquals([StringBackedEnum::HELLO_WORLD], $input->enums('valid_enum_value', StringBackedEnum::class)); + + $this->assertEmpty($input->enums('invalid_enum_value', StringBackedEnum::class)); + } + public function test_collect_method() { $input = new ValidatedInput(['users' => [1, 2, 3]]);