respect-validation/library/Transformers/Prefix.php
Henrique Moody 562d98d805
Refactor the NotEmpty rule
Since we have the ability to use `not` as a prefix, having rules that
validate negative behaviour makes them a bit inflexible, verbose, and
harder to understand.

This commit will refactor the `NotEmpty`, and rename it to `Falsy`. It
will no longer trim strings, because Blank does a much better job at it;
it only simulates the behaviour of PHP’s native `empty()` function.

Because `Falsy`, `Blank`, and `Undef` have similar behaviour, I created
a page to demonstrate the difference and show when the user should use
one or the other.

Assisted-by: Cursor (claude-4.5-opus-high)
2025-12-29 12:48:35 +01:00

86 lines
2.6 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Transformers;
use function array_shift;
use function in_array;
use function str_starts_with;
use function substr;
final class Prefix implements Transformer
{
private const array RULES_TO_SKIP = [
'key',
'keyExists',
'keyOptional',
'keySet',
'length',
'max',
'maxAge',
'min',
'minAge',
'not',
'emoji',
'nullOr',
'property',
'propertyExists',
'propertyOptional',
'undefOr',
];
public function transform(RuleSpec $ruleSpec): RuleSpec
{
if ($ruleSpec->wrapper !== null || in_array($ruleSpec->name, self::RULES_TO_SKIP, true)) {
return $ruleSpec;
}
if (str_starts_with($ruleSpec->name, 'key')) {
$arguments = $ruleSpec->arguments;
array_shift($arguments);
$wrapperArguments = [$ruleSpec->arguments[0]];
return new RuleSpec(substr($ruleSpec->name, 3), $arguments, new RuleSpec('key', $wrapperArguments));
}
if (str_starts_with($ruleSpec->name, 'length')) {
return new RuleSpec(substr($ruleSpec->name, 6), $ruleSpec->arguments, new RuleSpec('length'));
}
if (str_starts_with($ruleSpec->name, 'max')) {
return new RuleSpec(substr($ruleSpec->name, 3), $ruleSpec->arguments, new RuleSpec('max'));
}
if (str_starts_with($ruleSpec->name, 'min')) {
return new RuleSpec(substr($ruleSpec->name, 3), $ruleSpec->arguments, new RuleSpec('min'));
}
if (str_starts_with($ruleSpec->name, 'not')) {
return new RuleSpec(substr($ruleSpec->name, 3), $ruleSpec->arguments, new RuleSpec('not'));
}
if (str_starts_with($ruleSpec->name, 'nullOr')) {
return new RuleSpec(substr($ruleSpec->name, 6), $ruleSpec->arguments, new RuleSpec('nullOr'));
}
if (str_starts_with($ruleSpec->name, 'property')) {
$arguments = $ruleSpec->arguments;
array_shift($arguments);
$wrapperArguments = [$ruleSpec->arguments[0]];
return new RuleSpec(substr($ruleSpec->name, 8), $arguments, new RuleSpec('property', $wrapperArguments));
}
if (str_starts_with($ruleSpec->name, 'undefOr')) {
return new RuleSpec(substr($ruleSpec->name, 7), $ruleSpec->arguments, new RuleSpec('undefOr'));
}
return $ruleSpec;
}
}