From b390d592ab173c6da3878bf6022f30895b6b43d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:35:07 +0300 Subject: [PATCH 01/28] feat: Add `exists` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 87e47b55226c..3af044ecd184 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -242,4 +242,16 @@ public function getIterator(): Traversable { return new ArrayIterator($this->input); } + + /** + * Determine if the validated inputs contains a given input item key. + * + * @param string|array $key + * @return bool + */ + public function exists($key) + { + return $this->has($key); + } + } From a9df6e65df0e9a9fbf48ee02427fb3c267b955bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:35:57 +0300 Subject: [PATCH 02/28] feat: Add `hasAny` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 3af044ecd184..4e151298c86a 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -254,4 +254,19 @@ public function exists($key) return $this->has($key); } + /** + * Determine if the validated inputs contains any of the given inputs. + * + * @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); + } + } From 0226b12ab6416b76108058fcc9a7ecc31e329103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:37:17 +0300 Subject: [PATCH 03/28] feat: Add `whenHas` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 4e151298c86a..e03d286ad1b5 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -269,4 +269,25 @@ public function hasAny($keys) return Arr::hasAny($input, $keys); } + /** + * 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; + } + } From d7bfa17032d8c558f1d8b6eb26857046fdf8eea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:37:48 +0300 Subject: [PATCH 04/28] feat: Add `filled` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index e03d286ad1b5..f83a6030e467 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -290,4 +290,23 @@ public function whenHas($key, callable $callback, callable $default = null) 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; + } + } From 4ece0bb1134a7c7c0e55c3ba41fa70e92de854b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:38:07 +0300 Subject: [PATCH 05/28] feat: Add `isNotFilled` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index f83a6030e467..cfcbb36ac16c 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -309,4 +309,23 @@ public function filled($key) 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; + } + } + + return true; + } + } From 06e7cf8116ddcd125574c9585cd36aa3f9aaca28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:38:32 +0300 Subject: [PATCH 06/28] feat: Add `anyFilled` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index cfcbb36ac16c..a78ea20018d4 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -328,4 +328,23 @@ public function isNotFilled($key) 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; + } + } + + return false; + } + } From dacbe6732ae523ce7040e3f13bd34bb9e3b5930e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:38:57 +0300 Subject: [PATCH 07/28] feat: Add `whenFilled` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index a78ea20018d4..2af84d4ff53f 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -347,4 +347,25 @@ public function anyFilled($keys) 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(); + } + + return $this; + } + } From 4a81e44c930645197d7edcee3ae4e7f9363e9f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:39:32 +0300 Subject: [PATCH 08/28] feat: Add `whenMissing` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 2af84d4ff53f..e4f97b55ec60 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -368,4 +368,25 @@ public function whenFilled($key, callable $callback, callable $default = null) return $this; } + /** + * Apply the callback if the validated inputs 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; + } + } From de2d4cb05619887b68dd352e4a1bb8d926ca8fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:39:49 +0300 Subject: [PATCH 09/28] feat: Add `isEmptyString` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index e4f97b55ec60..e67019f44449 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -389,4 +389,17 @@ public function whenMissing($key, callable $callback, callable $default = null) return $this; } + /** + * Determine if the given input key is an empty string for "filled". + * + * @param string $key + * @return bool + */ + protected function isEmptyString($key) + { + $value = $this->input($key); + + return ! is_bool($value) && ! is_array($value) && trim((string) $value) === ''; + } + } From 85f66b0e0a3ec4c4b2849ec5ea6194044b09fdbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:40:08 +0300 Subject: [PATCH 10/28] feat: Add `keys` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index e67019f44449..d62ff2f3dc43 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -402,4 +402,14 @@ protected function isEmptyString($key) return ! is_bool($value) && ! is_array($value) && trim((string) $value) === ''; } + /** + * Get the keys for all of the input. + * + * @return array + */ + public function keys() + { + return array_keys($this->input()); + } + } From b3820c4be955950cbb34e26e296ab6d07b3670b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:40:35 +0300 Subject: [PATCH 11/28] feat: Add `input` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index d62ff2f3dc43..fa6d0b167b4a 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -412,4 +412,18 @@ public function keys() return array_keys($this->input()); } + /** + * Retrieve an input item from the validated inputs. + * + * @param string|null $key + * @param mixed $default + * @return mixed + */ + public function input($key = null, $default = null) + { + return data_get( + $this->all(), $key, $default + ); + } + } From 1a8ee000df3564c570ba21314fb9fcdc4b1f6908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:40:58 +0300 Subject: [PATCH 12/28] feat: Add `string` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index fa6d0b167b4a..ed7ff41566d7 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -426,4 +426,16 @@ public function input($key = null, $default = null) ); } + /** + * Retrieve input from the validated inputs as a Stringable instance. + * + * @param string $key + * @param mixed $default + * @return \Illuminate\Support\Stringable + */ + public function string($key, $default = null) + { + return str($this->input($key, $default)); + } + } From 457d3d183d8ebf3f2bdb6212f0dae23bde893096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:41:14 +0300 Subject: [PATCH 13/28] feat: Add `str` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index ed7ff41566d7..31febdcf4ebf 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -426,6 +426,18 @@ public function input($key = null, $default = null) ); } + /** + * Retrieve input from the validated inputs 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 inputs as a Stringable instance. * From 32463c9ac1e1223dc3950fe984b045867b3eba2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:41:36 +0300 Subject: [PATCH 14/28] feat: Add `boolean` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 31febdcf4ebf..b2567fb9137c 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -450,4 +450,18 @@ public function string($key, $default = null) return str($this->input($key, $default)); } + /** + * 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); + } + } From 42303ba4eb73f5c6d2dd69b7a50022cca990e2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:41:59 +0300 Subject: [PATCH 15/28] feat: Add `integer` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index b2567fb9137c..fb4e727931d1 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -464,4 +464,16 @@ 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)); + } + } From 4dab39409f34a7b440f33554cf822306fccb70d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:42:14 +0300 Subject: [PATCH 16/28] feat: Add `float` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index fb4e727931d1..e8bd09257ef3 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -476,4 +476,16 @@ 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)); + } + } From 88ad06e04a665af937338bd796006663573fecc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:42:41 +0300 Subject: [PATCH 17/28] feat: Add `date` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index e8bd09257ef3..43662f0b240c 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -4,6 +4,7 @@ use ArrayIterator; use Illuminate\Contracts\Support\ValidatedData; +use Illuminate\Support\Facades\Date; use stdClass; use Traversable; @@ -488,4 +489,27 @@ 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); + } + } From 6e6a75544dc01e7849a69b24dedfb3d947552125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:43:01 +0300 Subject: [PATCH 18/28] feat: Add `enum` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 43662f0b240c..fc9c0239694e 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -512,4 +512,24 @@ public function date($key, $format = null, $tz = null) 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)); + } + } From 1729646e949251e6c3d78d304ecb343511f9850c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:43:25 +0300 Subject: [PATCH 19/28] feat: Add `dump` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index fc9c0239694e..21c46da73c9c 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Support\ValidatedData; use Illuminate\Support\Facades\Date; use stdClass; +use Symfony\Component\VarDumper\VarDumper; use Traversable; class ValidatedInput implements ValidatedData @@ -532,4 +533,18 @@ public function enum($key, $enumClass) return $enumClass::tryFrom($this->input($key)); } + /** + * Dump the items. + * + * @param mixed $keys + * @return $this + */ + public function dump($keys = []) + { + $keys = is_array($keys) ? $keys : func_get_args(); + + VarDumper::dump(count($keys) > 0 ? $this->only($keys) : $this->all()); + + return $this; + } } From afd557fd7003262b4ebdee3f9d9b1d0fc6e7176f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:44:27 +0300 Subject: [PATCH 20/28] feat: Add `dd` method to `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 21c46da73c9c..0c98075d10f8 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -533,6 +533,19 @@ public function enum($key, $enumClass) return $enumClass::tryFrom($this->input($key)); } + /** + * Dump the validated inputs items and end the script. + * + * @param mixed ...$keys + * @return never + */ + public function dd(...$keys) + { + $this->dump(...$keys); + + exit(1); + } + /** * Dump the items. * From b5ec8a51a4b1b1b2700577053e2e8509338071b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:45:24 +0300 Subject: [PATCH 21/28] refactor: Add `$key` prop to `collect` method in `ValidatedInput` --- src/Illuminate/Support/ValidatedInput.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 0c98075d10f8..308563a78504 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -115,11 +115,12 @@ public function merge(array $items) /** * Get the input as a collection. * + * @param array|string|null $key * @return \Illuminate\Support\Collection */ - public function collect() + public function collect($key = null) { - return new Collection($this->input); + return collect(is_array($key) ? $this->only($key) : $this->input($key)); } /** From a1dfeb7a9ef8e8a9567528675163d1a1c97a8cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:46:37 +0300 Subject: [PATCH 22/28] refactor: Change usages `$this->input` to `$this->all()` --- src/Illuminate/Support/ValidatedInput.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 308563a78504..5300183b452c 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -40,7 +40,7 @@ public function has($keys) $keys = is_array($keys) ? $keys : func_get_args(); foreach ($keys as $key) { - if (! Arr::has($this->input, $key)) { + if (! Arr::has($this->all(), $key)) { return false; } } @@ -69,7 +69,7 @@ public function only($keys) { $results = []; - $input = $this->input; + $input = $this->all(); $placeholder = new stdClass; @@ -94,7 +94,7 @@ public function except($keys) { $keys = is_array($keys) ? $keys : func_get_args(); - $results = $this->input; + $results = $this->all(); Arr::forget($results, $keys); @@ -109,7 +109,7 @@ public function except($keys) */ public function merge(array $items) { - return new static(array_merge($this->input, $items)); + return new static(array_merge($this->all(), $items)); } /** From 98467799d88015c04a6e0f2f0f7ab54c3fd207e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:47:07 +0300 Subject: [PATCH 23/28] refactor: Change usages `$this->input[$key]` to `$this->input($key)` --- src/Illuminate/Support/ValidatedInput.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 5300183b452c..4205a09a1d80 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -151,7 +151,7 @@ public function toArray() */ public function __get($name) { - return $this->input[$name]; + return $this->input($name); } /** @@ -206,7 +206,7 @@ public function offsetExists($key): bool */ public function offsetGet($key): mixed { - return $this->input[$key]; + return $this->input($key); } /** From 65b718d1297099fe6f7d70637f753b54d550138b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:48:29 +0300 Subject: [PATCH 24/28] refactor: Change usages `isset($this->input[$name])` to `$this->exists($name)` --- src/Illuminate/Support/ValidatedInput.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 4205a09a1d80..70bc6c3d2e8c 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -173,7 +173,7 @@ public function __set($name, $value) */ public function __isset($name) { - return isset($this->input[$name]); + return $this->exists($name); } /** @@ -195,7 +195,7 @@ public function __unset($name) */ public function offsetExists($key): bool { - return isset($this->input[$key]); + return $this->exists($key); } /** From 2c3e069b49be94072b324ec7d2f5f3cbd3610ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20Ayd=C4=B1n?= Date: Mon, 6 May 2024 20:48:42 +0300 Subject: [PATCH 25/28] test: Added tests --- tests/Support/ValidatedInputTest.php | 481 +++++++++++++++++++++++++++ 1 file changed, 481 insertions(+) diff --git a/tests/Support/ValidatedInputTest.php b/tests/Support/ValidatedInputTest.php index a8bdbe894322..6798bd69c914 100644 --- a/tests/Support/ValidatedInputTest.php +++ b/tests/Support/ValidatedInputTest.php @@ -2,7 +2,11 @@ namespace Illuminate\Tests\Support; +use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; +use Illuminate\Support\Stringable; use Illuminate\Support\ValidatedInput; +use Illuminate\Tests\Support\Fixtures\StringBackedEnum; use PHPUnit\Framework\TestCase; class ValidatedInputTest extends TestCase @@ -44,4 +48,481 @@ public function test_input_existence() $this->assertEquals(true, $inputB->has(['name', 'votes'])); } + + public function test_exists_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertTrue($input->exists('name')); + $this->assertTrue($input->exists('surname')); + $this->assertTrue($input->exists(['name', 'surname'])); + $this->assertTrue($input->exists('foo.bar')); + $this->assertTrue($input->exists(['name', 'foo.baz'])); + $this->assertTrue($input->exists(['name', 'foo'])); + $this->assertTrue($input->exists('foo')); + + $this->assertFalse($input->exists('votes')); + $this->assertFalse($input->exists(['name', 'votes'])); + $this->assertFalse($input->exists(['votes', 'foo.bar'])); + } + + public function test_has_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertTrue($input->has('name')); + $this->assertTrue($input->has('surname')); + $this->assertTrue($input->has(['name', 'surname'])); + $this->assertTrue($input->has('foo.bar')); + $this->assertTrue($input->has(['name', 'foo.baz'])); + $this->assertTrue($input->has(['name', 'foo'])); + $this->assertTrue($input->has('foo')); + + $this->assertFalse($input->has('votes')); + $this->assertFalse($input->has(['name', 'votes'])); + $this->assertFalse($input->has(['votes', 'foo.bar'])); + } + + public function test_has_any_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertTrue($input->hasAny('name')); + $this->assertTrue($input->hasAny('surname')); + $this->assertTrue($input->hasAny('foo.bar')); + $this->assertTrue($input->hasAny(['name', 'surname'])); + $this->assertTrue($input->hasAny(['name', 'foo.bat'])); + $this->assertTrue($input->hasAny(['votes', 'foo'])); + + $this->assertFalse($input->hasAny('votes')); + $this->assertFalse($input->hasAny(['votes', 'foo.bat'])); + } + + public function test_when_has_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'age' => '', 'foo' => ['bar' => null]]); + + $name = $age = $city = $foo = $bar = $baz = false; + + $input->whenHas('name', function ($value) use (&$name) { + $name = $value; + }); + + $input->whenHas('age', function ($value) use (&$age) { + $age = $value; + }); + + $input->whenHas('city', function ($value) use (&$city) { + $city = $value; + }); + + $input->whenHas('foo', function ($value) use (&$foo) { + $foo = $value; + }); + + $input->whenHas('foo.bar', function ($value) use (&$bar) { + $bar = $value; + }); + + $input->whenHas('foo.baz', function () use (&$baz) { + $baz = 'test'; + }, function () use (&$baz) { + $baz = true; + }); + + $this->assertSame('Fatih', $name); + $this->assertSame('', $age); + $this->assertFalse($city); + $this->assertEquals(['bar' => null], $foo); + $this->assertTrue($baz); + $this->assertNull($bar); + } + + public function test_filled_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertTrue($input->filled('name')); + $this->assertTrue($input->filled('surname')); + $this->assertTrue($input->filled(['name', 'surname'])); + $this->assertTrue($input->filled(['name', 'foo'])); + $this->assertTrue($input->filled('foo')); + + $this->assertFalse($input->filled('foo.bar')); + $this->assertFalse($input->filled(['name', 'foo.baz'])); + $this->assertFalse($input->filled('votes')); + $this->assertFalse($input->filled(['name', 'votes'])); + $this->assertFalse($input->filled(['votes', 'foo.bar'])); + } + + public function test_is_not_filled_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertFalse($input->isNotFilled('name')); + $this->assertFalse($input->isNotFilled('surname')); + $this->assertFalse($input->isNotFilled(['name', 'surname'])); + $this->assertFalse($input->isNotFilled(['name', 'foo'])); + $this->assertFalse($input->isNotFilled('foo')); + $this->assertFalse($input->isNotFilled(['name', 'foo.baz'])); + $this->assertFalse($input->isNotFilled(['name', 'votes'])); + + $this->assertTrue($input->isNotFilled('foo.bar')); + $this->assertTrue($input->isNotFilled('votes')); + $this->assertTrue($input->isNotFilled(['votes', 'foo.bar'])); + } + + public function test_any_filled_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertTrue($input->anyFilled('name')); + $this->assertTrue($input->anyFilled('surname')); + $this->assertTrue($input->anyFilled(['name', 'surname'])); + $this->assertTrue($input->anyFilled(['name', 'foo'])); + $this->assertTrue($input->anyFilled('foo')); + $this->assertTrue($input->anyFilled(['name', 'foo.baz'])); + $this->assertTrue($input->anyFilled(['name', 'votes'])); + + $this->assertFalse($input->anyFilled('foo.bar')); + $this->assertFalse($input->anyFilled('votes')); + $this->assertFalse($input->anyFilled(['votes', 'foo.bar'])); + } + + public function test_when_filled_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'age' => '', 'foo' => ['bar' => null]]); + + $name = $age = $city = $foo = $bar = $baz = false; + + $input->whenFilled('name', function ($value) use (&$name) { + $name = $value; + }); + + $input->whenFilled('age', function ($value) use (&$age) { + $age = $value; + }); + + $input->whenFilled('city', function ($value) use (&$city) { + $city = $value; + }); + + $input->whenFilled('foo', function ($value) use (&$foo) { + $foo = $value; + }); + + $input->whenFilled('foo.bar', function ($value) use (&$bar) { + $bar = $value; + }); + + $input->whenFilled('foo.baz', function () use (&$baz) { + $baz = 'test'; + }, function () use (&$baz) { + $baz = true; + }); + + $this->assertSame('Fatih', $name); + $this->assertEquals(['bar' => null], $foo); + $this->assertTrue($baz); + $this->assertFalse($age); + $this->assertFalse($city); + $this->assertFalse($bar); + } + + public function test_missing_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertFalse($input->missing('name')); + $this->assertFalse($input->missing('surname')); + $this->assertFalse($input->missing(['name', 'surname'])); + $this->assertFalse($input->missing('foo.bar')); + $this->assertFalse($input->missing(['name', 'foo.baz'])); + $this->assertFalse($input->missing(['name', 'foo'])); + $this->assertFalse($input->missing('foo')); + + $this->assertTrue($input->missing('votes')); + $this->assertTrue($input->missing(['name', 'votes'])); + $this->assertTrue($input->missing(['votes', 'foo.bar'])); + } + + public function test_when_missing_method() + { + $input = new ValidatedInput(['foo' => ['bar' => null]]); + + $name = $age = $city = $foo = $bar = $baz = false; + + $input->whenMissing('name', function () use (&$name) { + $name = 'Fatih'; + }); + + $input->whenMissing('age', function () use (&$age) { + $age = ''; + }); + + $input->whenMissing('city', function () use (&$city) { + $city = null; + }); + + $input->whenMissing('foo', function ($value) use (&$foo) { + $foo = $value; + }); + + $input->whenMissing('foo.baz', function () use (&$baz) { + $baz = true; + }); + + $input->whenMissing('foo.bar', function () use (&$bar) { + $bar = 'test'; + }, function () use (&$bar) { + $bar = true; + }); + + $this->assertSame('Fatih', $name); + $this->assertSame('', $age); + $this->assertNull($city); + $this->assertFalse($foo); + $this->assertTrue($baz); + $this->assertTrue($bar); + } + + public function test_keys_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertEquals(['name', 'surname', 'foo'], $input->keys()); + } + + public function test_all_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertEquals(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']], $input->all()); + } + + public function test_input_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertSame('Fatih', $input->input('name')); + $this->assertSame(null, $input->input('foo.bar')); + $this->assertSame('test', $input->input('foo.bat', 'test')); + } + + public function test_str_method() + { + $input = new ValidatedInput([ + 'int' => 123, + 'int_str' => '456', + 'float' => 123.456, + 'float_str' => '123.456', + 'float_zero' => 0.000, + 'float_str_zero' => '0.000', + 'str' => 'abc', + 'empty_str' => '', + 'null' => null, + ]); + + $this->assertTrue($input->str('int') instanceof Stringable); + $this->assertTrue($input->str('int') instanceof Stringable); + $this->assertTrue($input->str('unknown_key') instanceof Stringable); + $this->assertSame('123', $input->str('int')->value()); + $this->assertSame('456', $input->str('int_str')->value()); + $this->assertSame('123.456', $input->str('float')->value()); + $this->assertSame('123.456', $input->str('float_str')->value()); + $this->assertSame('0', $input->str('float_zero')->value()); + $this->assertSame('0.000', $input->str('float_str_zero')->value()); + $this->assertSame('', $input->str('empty_str')->value()); + $this->assertSame('', $input->str('null')->value()); + $this->assertSame('', $input->str('unknown_key')->value()); + } + + public function test_string_method() + { + $input = new ValidatedInput([ + 'int' => 123, + 'int_str' => '456', + 'float' => 123.456, + 'float_str' => '123.456', + 'float_zero' => 0.000, + 'float_str_zero' => '0.000', + 'str' => 'abc', + 'empty_str' => '', + 'null' => null, + ]); + + $this->assertTrue($input->string('int') instanceof Stringable); + $this->assertTrue($input->string('int') instanceof Stringable); + $this->assertTrue($input->string('unknown_key') instanceof Stringable); + $this->assertSame('123', $input->string('int')->value()); + $this->assertSame('456', $input->string('int_str')->value()); + $this->assertSame('123.456', $input->string('float')->value()); + $this->assertSame('123.456', $input->string('float_str')->value()); + $this->assertSame('0', $input->string('float_zero')->value()); + $this->assertSame('0.000', $input->string('float_str_zero')->value()); + $this->assertSame('', $input->string('empty_str')->value()); + $this->assertSame('', $input->string('null')->value()); + $this->assertSame('', $input->string('unknown_key')->value()); + } + + public function test_boolean_method() + { + $input = new ValidatedInput([ + 'with_trashed' => 'false', + 'download' => true, + 'checked' => 1, + 'unchecked' => '0', + 'with_on' => 'on', + 'with_yes' => 'yes', + ]); + + $this->assertTrue($input->boolean('checked')); + $this->assertTrue($input->boolean('download')); + $this->assertFalse($input->boolean('unchecked')); + $this->assertFalse($input->boolean('with_trashed')); + $this->assertFalse($input->boolean('some_undefined_key')); + $this->assertTrue($input->boolean('with_on')); + $this->assertTrue($input->boolean('with_yes')); + } + + public function test_integer_method() + { + $input = new ValidatedInput([ + 'int' => '123', + 'raw_int' => 456, + 'zero_padded' => '078', + 'space_padded' => ' 901', + 'nan' => 'nan', + 'mixed' => '1ab', + 'underscore_notation' => '2_000', + 'null' => null, + ]); + + $this->assertSame(123, $input->integer('int')); + $this->assertSame(456, $input->integer('raw_int')); + $this->assertSame(78, $input->integer('zero_padded')); + $this->assertSame(901, $input->integer('space_padded')); + $this->assertSame(0, $input->integer('nan')); + $this->assertSame(1, $input->integer('mixed')); + $this->assertSame(2, $input->integer('underscore_notation')); + $this->assertSame(123456, $input->integer('unknown_key', 123456)); + $this->assertSame(0, $input->integer('null')); + $this->assertSame(0, $input->integer('null', 123456)); + } + + public function test_float_method() + { + $input = new ValidatedInput([ + 'float' => '1.23', + 'raw_float' => 45.6, + 'decimal_only' => '.6', + 'zero_padded' => '0.78', + 'space_padded' => ' 90.1', + 'nan' => 'nan', + 'mixed' => '1.ab', + 'scientific_notation' => '1e3', + 'null' => null, + ]); + + $this->assertSame(1.23, $input->float('float')); + $this->assertSame(45.6, $input->float('raw_float')); + $this->assertSame(.6, $input->float('decimal_only')); + $this->assertSame(0.78, $input->float('zero_padded')); + $this->assertSame(90.1, $input->float('space_padded')); + $this->assertSame(0.0, $input->float('nan')); + $this->assertSame(1.0, $input->float('mixed')); + $this->assertSame(1e3, $input->float('scientific_notation')); + $this->assertSame(123.456, $input->float('unknown_key', 123.456)); + $this->assertSame(0.0, $input->float('null')); + $this->assertSame(0.0, $input->float('null', 123.456)); + } + + public function test_date_method() + { + $input = new ValidatedInput([ + 'as_null' => null, + 'as_invalid' => 'invalid', + + 'as_datetime' => '24-01-01 16:30:25', + 'as_format' => '1704126625', + 'as_timezone' => '24-01-01 13:30:25', + + 'as_date' => '2024-01-01', + 'as_time' => '16:30:25', + ]); + + $current = Carbon::create(2024, 1, 1, 16, 30, 25); + + $this->assertNull($input->date('as_null')); + $this->assertNull($input->date('doesnt_exists')); + + $this->assertEquals($current, $input->date('as_datetime')); + $this->assertEquals($current->format('Y-m-d H:i:s P'), $input->date('as_format', 'U')->format('Y-m-d H:i:s P')); + $this->assertEquals($current, $input->date('as_timezone', null, 'America/Santiago')); + + $this->assertTrue($input->date('as_date')->isSameDay($current)); + $this->assertTrue($input->date('as_time')->isSameSecond('16:30:25')); + } + + public function test_enum_method() + { + $input = new ValidatedInput([ + 'valid_enum_value' => 'Hello world', + 'invalid_enum_value' => 'invalid', + ]); + + $this->assertNull($input->enum('doesnt_exists', StringBackedEnum::class)); + + $this->assertEquals(StringBackedEnum::HELLO_WORLD, $input->enum('valid_enum_value', StringBackedEnum::class)); + + $this->assertNull($input->enum('invalid_enum_value', StringBackedEnum::class)); + } + + public function test_collect_method() + { + $input = new ValidatedInput(['users' => [1, 2, 3]]); + + $this->assertInstanceOf(Collection::class, $input->collect('users')); + $this->assertTrue($input->collect('developers')->isEmpty()); + $this->assertEquals([1, 2, 3], $input->collect('users')->all()); + $this->assertEquals(['users' => [1, 2, 3]], $input->collect()->all()); + + $input = new ValidatedInput(['text-payload']); + $this->assertEquals(['text-payload'], $input->collect()->all()); + + $input = new ValidatedInput(['email' => 'test@example.com']); + $this->assertEquals(['test@example.com'], $input->collect('email')->all()); + + $input = new ValidatedInput([]); + $this->assertInstanceOf(Collection::class, $input->collect()); + $this->assertTrue($input->collect()->isEmpty()); + + $input = new ValidatedInput(['users' => [1, 2, 3], 'roles' => [4, 5, 6], 'foo' => ['bar', 'baz'], 'email' => 'test@example.com']); + $this->assertInstanceOf(Collection::class, $input->collect(['users'])); + $this->assertTrue($input->collect(['developers'])->isEmpty()); + $this->assertTrue($input->collect(['roles'])->isNotEmpty()); + $this->assertEquals(['roles' => [4, 5, 6]], $input->collect(['roles'])->all()); + $this->assertEquals(['users' => [1, 2, 3], 'email' => 'test@example.com'], $input->collect(['users', 'email'])->all()); + $this->assertEquals(collect(['roles' => [4, 5, 6], 'foo' => ['bar', 'baz']]), $input->collect(['roles', 'foo'])); + $this->assertEquals(['users' => [1, 2, 3], 'roles' => [4, 5, 6], 'foo' => ['bar', 'baz'], 'email' => 'test@example.com'], $input->collect()->all()); + } + + public function test_only_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertEquals(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null]], $input->only('name', 'surname', 'foo.bar')); + $this->assertEquals(['name' => 'Fatih', 'foo' => ['bar' => null, 'baz' => '']], $input->only('name', 'foo')); + $this->assertEquals(['foo' => ['baz' => '']], $input->only('foo.baz')); + $this->assertEquals(['name' => 'Fatih'], $input->only('name')); + } + + public function test_except_method() + { + $input = new ValidatedInput(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null, 'baz' => '']]); + + $this->assertEquals(['name' => 'Fatih', 'surname' => 'AYDIN', 'foo' => ['bar' => null]], $input->except('foo.baz')); + $this->assertEquals(['surname' => 'AYDIN'], $input->except('name', 'foo')); + $this->assertEquals([], $input->except('name', 'surname', 'foo')); + } } From 1864c55958562accfae6becd7f6023577d6893ac Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 6 May 2024 14:20:14 -0400 Subject: [PATCH 26/28] formatting --- src/Illuminate/Support/ValidatedInput.php | 442 +++++++++++----------- 1 file changed, 221 insertions(+), 221 deletions(-) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 70bc6c3d2e8c..53912a25b5e3 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -32,233 +32,35 @@ public function __construct(array $input) /** * Determine if the validated input has one or more keys. * - * @param mixed $keys + * @param string|array $key * @return bool */ - public function has($keys) + public function exists($key) { - $keys = is_array($keys) ? $keys : func_get_args(); - - foreach ($keys as $key) { - if (! Arr::has($this->all(), $key)) { - return false; - } - } - - return true; + return $this->has($key); } /** - * Determine if the validated input is missing one or more keys. + * Determine if the validated input has one or more keys. * * @param mixed $keys * @return bool */ - public function missing($keys) - { - return ! $this->has($keys); - } - - /** - * 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) + public function has($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. - * - * @param array $items - * @return static - */ - 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 collect(is_array($key) ? $this->only($key) : $this->input($key)); - } - - /** - * Get the raw, underlying input array. - * - * @return array - */ - public function all() - { - return $this->input; - } - - /** - * Get the instance as an array. - * - * @return array - */ - public function toArray() - { - return $this->all(); - } - - /** - * Dynamically access input data. - * - * @param string $name - * @return mixed - */ - public function __get($name) - { - return $this->input($name); - } - - /** - * Dynamically set input data. - * - * @param string $name - * @param mixed $value - * @return mixed - */ - public function __set($name, $value) - { - $this->input[$name] = $value; - } - - /** - * Determine if an input key is set. - * - * @return bool - */ - public function __isset($name) - { - return $this->exists($name); - } - - /** - * Remove an input key. - * - * @param string $name - * @return void - */ - public function __unset($name) - { - unset($this->input[$name]); - } - - /** - * Determine if an item exists at an offset. - * - * @param mixed $key - * @return bool - */ - public function offsetExists($key): bool - { - return $this->exists($key); - } - - /** - * Get an item at a given offset. - * - * @param mixed $key - * @return mixed - */ - public function offsetGet($key): mixed - { - return $this->input($key); - } - - /** - * Set the item at a given offset. - * - * @param mixed $key - * @param mixed $value - * @return void - */ - public function offsetSet($key, $value): void - { - if (is_null($key)) { - $this->input[] = $value; - } else { - $this->input[$key] = $value; + foreach ($keys as $key) { + if (! Arr::has($this->all(), $key)) { + return false; + } } - } - - /** - * Unset the item at a given offset. - * - * @param string $key - * @return void - */ - public function offsetUnset($key): void - { - unset($this->input[$key]); - } - - /** - * Get an iterator for the input. - * - * @return \ArrayIterator - */ - public function getIterator(): Traversable - { - return new ArrayIterator($this->input); - } - /** - * Determine if the validated inputs contains a given input item key. - * - * @param string|array $key - * @return bool - */ - public function exists($key) - { - return $this->has($key); + return true; } /** - * Determine if the validated inputs contains any of the given inputs. + * Determine if the validated input contains any of the given keys. * * @param string|array $keys * @return bool @@ -273,7 +75,7 @@ public function hasAny($keys) } /** - * Apply the callback if the validated inputs contains the given input item key. + * Apply the callback if the validated input contains the given input item key. * * @param string $key * @param callable $callback @@ -294,7 +96,7 @@ public function whenHas($key, callable $callback, callable $default = null) } /** - * Determine if the validated inputs contains a non-empty value for an input item. + * Determine if the validated input contains a non-empty value for an input key. * * @param string|array $key * @return bool @@ -313,7 +115,7 @@ public function filled($key) } /** - * Determine if the validated inputs contains an empty value for an input item. + * Determine if the validated input contains an empty value for an input key. * * @param string|array $key * @return bool @@ -332,7 +134,7 @@ public function isNotFilled($key) } /** - * Determine if the validated inputs contains a non-empty value for any of the given inputs. + * Determine if the validated input contains a non-empty value for any of the given input keys. * * @param string|array $keys * @return bool @@ -351,7 +153,7 @@ public function anyFilled($keys) } /** - * Apply the callback if the validated inputs contains a non-empty value for the given input item key. + * Apply the callback if the validated input contains a non-empty value for the given input item key. * * @param string $key * @param callable $callback @@ -372,7 +174,18 @@ public function whenFilled($key, callable $callback, callable $default = null) } /** - * Apply the callback if the validated inputs is missing the given input item key. + * 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 @@ -416,7 +229,7 @@ public function keys() } /** - * Retrieve an input item from the validated inputs. + * Retrieve an input item from the validated input. * * @param string|null $key * @param mixed $default @@ -430,7 +243,7 @@ public function input($key = null, $default = null) } /** - * Retrieve input from the validated inputs as a Stringable instance. + * Retrieve input from the validated input as a Stringable instance. * * @param string $key * @param mixed $default @@ -442,7 +255,7 @@ public function str($key, $default = null) } /** - * Retrieve input from the validated inputs as a Stringable instance. + * Retrieve input from the validated input as a Stringable instance. * * @param string $key * @param mixed $default @@ -492,7 +305,7 @@ public function float($key, $default = 0.0) } /** - * Retrieve input from the validated inputs as a Carbon instance. + * Retrieve input from the validated input as a Carbon instance. * * @param string $key * @param string|null $format @@ -515,7 +328,7 @@ public function date($key, $format = null, $tz = null) } /** - * Retrieve input from the validated inputs as an enum. + * Retrieve input from the validated input as an enum. * * @template TEnum * @@ -535,7 +348,81 @@ public function enum($key, $enumClass) } /** - * Dump the validated inputs items and end the script. + * 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. + * + * @param array $items + * @return static + */ + 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 collect(is_array($key) ? $this->only($key) : $this->input($key)); + } + + /** + * Get the raw, underlying input array. + * + * @return array + */ + public function all() + { + return $this->input; + } + + /** + * Dump the validated input items and end the script. * * @param mixed ...$keys * @return never @@ -561,4 +448,117 @@ public function dump($keys = []) return $this; } + + /** + * Get the instance as an array. + * + * @return array + */ + public function toArray() + { + return $this->all(); + } + + /** + * Dynamically access input data. + * + * @param string $name + * @return mixed + */ + public function __get($name) + { + return $this->input($name); + } + + /** + * Dynamically set input data. + * + * @param string $name + * @param mixed $value + * @return mixed + */ + public function __set($name, $value) + { + $this->input[$name] = $value; + } + + /** + * Determine if an input key is set. + * + * @return bool + */ + public function __isset($name) + { + return $this->exists($name); + } + + /** + * Remove an input key. + * + * @param string $name + * @return void + */ + public function __unset($name) + { + unset($this->input[$name]); + } + + /** + * Determine if an item exists at an offset. + * + * @param mixed $key + * @return bool + */ + public function offsetExists($key): bool + { + return $this->exists($key); + } + + /** + * Get an item at a given offset. + * + * @param mixed $key + * @return mixed + */ + public function offsetGet($key): mixed + { + return $this->input($key); + } + + /** + * Set the item at a given offset. + * + * @param mixed $key + * @param mixed $value + * @return void + */ + public function offsetSet($key, $value): void + { + if (is_null($key)) { + $this->input[] = $value; + } else { + $this->input[$key] = $value; + } + } + + /** + * Unset the item at a given offset. + * + * @param string $key + * @return void + */ + public function offsetUnset($key): void + { + unset($this->input[$key]); + } + + /** + * Get an iterator for the input. + * + * @return \ArrayIterator + */ + public function getIterator(): Traversable + { + return new ArrayIterator($this->input); + } } From 2091b9470734c1ade0d0ef1cb9d171330c3186ab Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 6 May 2024 14:22:18 -0400 Subject: [PATCH 27/28] formatting --- src/Illuminate/Support/ValidatedInput.php | 26 +++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 53912a25b5e3..6136182d09d4 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -173,6 +173,19 @@ public function whenFilled($key, callable $callback, callable $default = null) return $this; } + /** + * Determine if the given input key is an empty string. + * + * @param string $key + * @return bool + */ + protected function isEmptyString($key) + { + $value = $this->input($key); + + return ! is_bool($value) && ! is_array($value) && trim((string) $value) === ''; + } + /** * Determine if the validated input is missing one or more keys. * @@ -205,19 +218,6 @@ public function whenMissing($key, callable $callback, callable $default = null) return $this; } - /** - * Determine if the given input key is an empty string for "filled". - * - * @param string $key - * @return bool - */ - protected function isEmptyString($key) - { - $value = $this->input($key); - - return ! is_bool($value) && ! is_array($value) && trim((string) $value) === ''; - } - /** * Get the keys for all of the input. * From f52f4bc0c9500c7e081e5a7188d821ec8a7a5708 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 6 May 2024 14:28:56 -0400 Subject: [PATCH 28/28] formatting --- src/Illuminate/Support/ValidatedInput.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Support/ValidatedInput.php b/src/Illuminate/Support/ValidatedInput.php index 6136182d09d4..85c5108cb17a 100644 --- a/src/Illuminate/Support/ValidatedInput.php +++ b/src/Illuminate/Support/ValidatedInput.php @@ -96,7 +96,7 @@ public function whenHas($key, callable $callback, callable $default = null) } /** - * Determine if the validated input contains a non-empty value for an input key. + * Determine if the validated input contains a non-empty value for an input item. * * @param string|array $key * @return bool @@ -115,7 +115,7 @@ public function filled($key) } /** - * Determine if the validated input contains an empty value for an input key. + * Determine if the validated input contains an empty value for an input item. * * @param string|array $key * @return bool @@ -134,7 +134,7 @@ public function isNotFilled($key) } /** - * Determine if the validated input contains a non-empty value for any of the given input keys. + * Determine if the validated input contains a non-empty value for any of the given input items. * * @param string|array $keys * @return bool @@ -174,7 +174,7 @@ public function whenFilled($key, callable $callback, callable $default = null) } /** - * Determine if the given input key is an empty string. + * Determine if the given input item is an empty string. * * @param string $key * @return bool @@ -483,7 +483,7 @@ public function __set($name, $value) } /** - * Determine if an input key is set. + * Determine if an input item is set. * * @return bool */ @@ -493,7 +493,7 @@ public function __isset($name) } /** - * Remove an input key. + * Remove an input item. * * @param string $name * @return void