Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Password validation class #899

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 50 additions & 1 deletion src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -847,4 +896,4 @@ protected function shouldCastUserExample()
{
return false;
}
}
}
Loading