From 880152c98864dc19aebd1676cc1fd9cf148f4a5e Mon Sep 17 00:00:00 2001 From: Sylvain Feuerstoss Date: Thu, 3 Oct 2024 12:58:15 +0200 Subject: [PATCH] feat: add support for Password validation class --- src/Extracting/ParsesValidationRules.php | 51 +++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Extracting/ParsesValidationRules.php b/src/Extracting/ParsesValidationRules.php index 0dd9038c..834cb6ca 100644 --- a/src/Extracting/ParsesValidationRules.php +++ b/src/Extracting/ParsesValidationRules.php @@ -218,6 +218,55 @@ protected function parseRule($rule, array &$parameterData, bool $independentOnly return true; } + + if ($rule instanceof \Illuminate\Validation\Rules\Password) { + // no getters available on Password class, so we use Reflection to extract protected attributes + $passwordAttrs = []; + $reflection = new ReflectionClass($rule); + foreach ($reflection->getProperties() as $property) { + if ($property->isStatic()) { + continue; + } + $property->setAccessible(true); + $passwordAttrs[$property->getName()] = $property->getValue($rule); + } + + $min = $passwordAttrs['min'] ?? null; + $max = $passwordAttrs['max'] ?? null; + $letters = $passwordAttrs['letters'] ?? false; + $mixedCase = $passwordAttrs['mixedCase'] ?? false; + $numbers = $passwordAttrs['numbers'] ?? false; + $symbols = $passwordAttrs['symbols'] ?? false; + + if (!empty($min)) { + $parameterData['description'] .= ' ' . $this->getDescription('min', [':min' => $min]); + } + if (!empty($max)) { + $parameterData['description'] .= ' ' . $this->getDescription('max', [':max' => $max]); + } + if ($letters) { + $parameterData['description'] .= ' ' . $this->getDescription('password.letters'); + } + if ($mixedCase) { + $parameterData['description'] .= ' ' . $this->getDescription('password.mixed'); + } + if ($numbers) { + $parameterData['description'] .= ' ' . $this->getDescription('password.numbers'); + } + if ($symbols) { + $parameterData['description'] .= ' ' . $this->getDescription('password.symbols'); + } + + $parameterData['setter'] = fn () => Str::password( + length: rand($min ?? 10, $max ?? 14), + letters: $letters, + numbers: $numbers, + symbols: $symbols, + ); + + return true; + } + if ($rule instanceof Rule || $rule instanceof ValidationRule) { if (method_exists($rule, 'invokable')) { // Laravel wraps InvokableRule instances in an InvokableValidationRule class, @@ -847,4 +896,4 @@ protected function shouldCastUserExample() { return false; } -} +} \ No newline at end of file