Skip to content

Commit

Permalink
Rename "Validatable" to "Rule"
Browse files Browse the repository at this point in the history
Besides the interface's name, everything already calls this type "Rule",
not "Validatable." This commit puts a stone on it and renames the
interface for better naming.
  • Loading branch information
henriquemoody committed Dec 5, 2024
1 parent d356696 commit e6af762
Show file tree
Hide file tree
Showing 75 changed files with 393 additions and 538 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ create a validator that validates if a string is equal to "Hello World".

### Creating the rule

The rule itself needs to implement the `Validatable` interface but, it is
The rule itself needs to implement the `Rule` interface but, it is
convenient to just extend the `Simple` or `Standard` class.
Doing that, you'll only need to declare one method: `validate($input)`.
This method must return `true` or `false`.
Expand Down
10 changes: 5 additions & 5 deletions bin/create-mixin
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use Respect\Validation\Mixins\StaticUndefOr;
use Respect\Validation\Rules\NotUndef;
use Respect\Validation\Rules\NullOr;
use Respect\Validation\Rules\UndefOr;
use Respect\Validation\Validatable;
use Respect\Validation\Rule;

function addMethodToInterface(
string $originalName,
Expand Down Expand Up @@ -209,15 +209,15 @@ function overwriteFile(string $content, string $basename): void

foreach ($mixins as [$name, $prefix, $allowList, $denyList]) {
$chainedNamespace = new PhpNamespace('Respect\\Validation\\Mixins');
$chainedNamespace->addUse(Validatable::class);
$chainedNamespace->addUse(Rule::class);
$chainedInterface = $chainedNamespace->addInterface('Chained' . $name);

$staticNamespace = new PhpNamespace('Respect\\Validation\\Mixins');
$staticNamespace->addUse(Validatable::class);
$staticNamespace->addUse(Rule::class);
$staticInterface = $staticNamespace->addInterface('Static' . $name);

if ($name === 'Validator') {
$chainedInterface->addExtend(Validatable::class);
$chainedInterface->addExtend(Rule::class);
$chainedInterface->addExtend(ChainedKey::class);
$chainedInterface->addExtend(ChainedLength::class);
$chainedInterface->addExtend(ChainedMax::class);
Expand All @@ -240,7 +240,7 @@ function overwriteFile(string $content, string $basename): void
$setTemplates->addComment('@param array<string, mixed> $templates');

$getRules = $chainedInterface->addMethod('getRules')->setPublic()->setReturnType('array');
$getRules->addComment('@return array<Validatable>');
$getRules->addComment('@return array<Rule>');

$staticInterface->addExtend(StaticKey::class);
$staticInterface->addExtend(StaticLength::class);
Expand Down
2 changes: 1 addition & 1 deletion docs/07-custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
You can also create and use your own rules. To do this, you will need to create
a rule and an exception to go with the rule.

To create a rule, you need to create a class that implements the `Validatable` interface
To create a rule, you need to create a class that implements the `Rule` interface
and is within the Rules `namespace`. It is convenient to just extend the `Simple` or
`Standard` class. When the rule is called the logic inside the validate method will be
executed. Here's how the class should look:
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/AllOf.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AllOf

- `AllOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`
- `AllOf(Rule $rule1, Rule $rule2, Rule ...$rule)`

Will validate if all inner validators validates.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/AnyOf.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AnyOf

- `AnyOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`
- `AnyOf(Rule $rule1, Rule $rule2, Rule ...$rule)`

This is a group validator that acts as an OR operator.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Consecutive.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Consecutive

- `Consecutive(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`
- `Consecutive(Rule $rule1, Rule $rule2, Rule ...$rule)`

Validates the input against a series of rules until one fails.

Expand Down
4 changes: 2 additions & 2 deletions docs/rules/DateTimeDiff.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# DateTimeDiff

- `DateTimeDiff(string $type, Validatable $rule)`
- `DateTimeDiff(string $type, Validatable $rule, string $format)`
- `DateTimeDiff(string $type, Rule $rule)`
- `DateTimeDiff(string $type, Rule $rule, string $format)`

Validates the difference of date/time against a specific rule.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Each.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Each

- `Each(Validatable $rule)`
- `Each(Rule $rule)`

Validates whether each value in the input is valid according to another rule.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Key.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Key

- `Key(int|string $key, Validatable $rule)`
- `Key(int|string $key, Rule $rule)`

Validates the value of an array against a given rule.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/KeyOptional.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# KeyOptional

- `KeyOptional(int|string $key, Validatable $rule)`
- `KeyOptional(int|string $key, Rule $rule)`

Validates the value of an array against a given rule when the key exists.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Lazy.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Lazy

- `Lazy(callable(mixed $input): Validatable $ruleCreator)`
- `Lazy(callable(mixed $input): Rule $ruleCreator)`

Validates the input using a rule that is created from a callback.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Length.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Length

- `Length(Validatable $rule)`
- `Length(Rule $rule)`

Validates the length of the given input against a given rule.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Max.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Max

- `Max(Validatable $rule)`
- `Max(Rule $rule)`

Validates the maximum value of the input against a given rule.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Min.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Min

- `Min(Validatable $rule)`
- `Min(Rule $rule)`

Validates the minimum value of the input against a given rule.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/NoneOf.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NoneOf

- `NoneOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`
- `NoneOf(Rule $rule1, Rule $rule2, Rule ...$rule)`

Validates if NONE of the given validators validate:

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Not.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Not

- `Not(Validatable $rule)`
- `Not(Rule $rule)`

Negates any rule.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/NullOr.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NullOr

- `NullOr(Validatable $rule)`
- `NullOr(Rule $rule)`

Validates the input using a defined rule when the input is not `null`.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/OneOf.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OneOf

- `OneOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`
- `OneOf(Rule $rule1, Rule $rule2, Rule ...$rule)`

Will validate if exactly one inner validator passes.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Property.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Property

- `Property(string $propertyName, Validatable $rule)`
- `Property(string $propertyName, Rule $rule)`

Validates an object property against a given rule.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/PropertyOptional.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PropertyOptional

- `PropertyOptional(string $propertyName, Validatable $rule)`
- `PropertyOptional(string $propertyName, Rule $rule)`

Validates an object property against a given rule only if the property exists.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/UndefOr.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# UndefOr

- `UndefOr(Validatable $rule)`
- `UndefOr(Rule $rule)`

Validates the input using a defined rule when the input is not `null` or an empty string (`''`).

Expand Down
4 changes: 2 additions & 2 deletions docs/rules/When.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# When

- `When(Validatable $if, Validatable $then)`
- `When(Validatable $if, Validatable $then, Validatable $else)`
- `When(Rule $if, Rule $then)`
- `When(Rule $if, Rule $then, Rule $else)`

A ternary validator that accepts three parameters.

Expand Down
12 changes: 6 additions & 6 deletions library/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public function withRuleNamespace(string $rulesNamespace): self
/**
* @param mixed[] $arguments
*/
public function rule(string $ruleName, array $arguments = []): Validatable
public function rule(string $ruleName, array $arguments = []): Rule
{
return $this->createRuleSpec($this->transformer->transform(new RuleSpec($ruleName, $arguments)));
}

private function createRuleSpec(RuleSpec $ruleSpec): Validatable
private function createRuleSpec(RuleSpec $ruleSpec): Rule
{
$rule = $this->createRule($ruleSpec->name, $ruleSpec->arguments);
if ($ruleSpec->wrapper !== null) {
Expand All @@ -82,16 +82,16 @@ private function createRuleSpec(RuleSpec $ruleSpec): Validatable
/**
* @param mixed[] $arguments
*/
private function createRule(string $ruleName, array $arguments = []): Validatable
private function createRule(string $ruleName, array $arguments = []): Rule
{
foreach ($this->rulesNamespaces as $namespace) {
try {
/** @var class-string<Validatable> $name */
/** @var class-string<Rule> $name */
$name = $namespace . '\\' . ucfirst($ruleName);
$reflection = new ReflectionClass($name);
if (!$reflection->isSubclassOf(Validatable::class)) {
if (!$reflection->isSubclassOf(Rule::class)) {
throw new InvalidClassException(
sprintf('"%s" must be an instance of "%s"', $name, Validatable::class)
sprintf('"%s" must be an instance of "%s"', $name, Rule::class)
);
}

Expand Down
4 changes: 2 additions & 2 deletions library/Helpers/CanBindEvaluateRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
namespace Respect\Validation\Helpers;

use Respect\Validation\Result;
use Respect\Validation\Validatable;
use Respect\Validation\Rule;

trait CanBindEvaluateRule
{
private function bindEvaluate(Validatable $binded, Validatable $binder, mixed $input): Result
private function bindEvaluate(Rule $binded, Rule $binder, mixed $input): Result
{
if ($binder->getName() !== null && $binded->getName() === null) {
$binded->setName($binder->getName());
Expand Down
6 changes: 3 additions & 3 deletions library/Helpers/CanExtractRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

namespace Respect\Validation\Helpers;

use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Composite;
use Respect\Validation\Rules\Not;
use Respect\Validation\Validatable;
use Respect\Validation\Validator;
use Throwable;

use function count;

trait CanExtractRules
{
private function extractSiblingSuitableRule(Validatable $rule, Throwable $throwable): Validatable
private function extractSiblingSuitableRule(Rule $rule, Throwable $throwable): Rule
{
$this->assertSingleRule($rule, $throwable);

Expand All @@ -30,7 +30,7 @@ private function extractSiblingSuitableRule(Validatable $rule, Throwable $throwa
return $rule;
}

private function assertSingleRule(Validatable $rule, Throwable $throwable): void
private function assertSingleRule(Rule $rule, Throwable $throwable): void
{
if ($rule instanceof Not) {
$this->assertSingleRule($rule->getRule(), $throwable);
Expand Down
4 changes: 2 additions & 2 deletions library/Message/StandardRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Respect\Stringifier\Stringifiers\CompositeStringifier;
use Respect\Validation\Mode;
use Respect\Validation\Result;
use Respect\Validation\Validatable;
use Respect\Validation\Rule;

use function is_bool;
use function is_scalar;
Expand Down Expand Up @@ -60,7 +60,7 @@ function (array $matches) use ($parameters, $translator) {
}

/** @return array<Template> */
private function extractTemplates(Validatable $rule): array
private function extractTemplates(Rule $rule): array
{
if (!isset($this->templates[$rule::class])) {
$reflection = new ReflectionClass($rule);
Expand Down
4 changes: 2 additions & 2 deletions library/Message/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
namespace Respect\Validation\Message;

use Attribute;
use Respect\Validation\Validatable;
use Respect\Validation\Rule;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Template
{
public function __construct(
public readonly string $default,
public readonly string $inverted,
public readonly string $id = Validatable::TEMPLATE_STANDARD,
public readonly string $id = Rule::TEMPLATE_STANDARD,
) {
}
}
Loading

0 comments on commit e6af762

Please sign in to comment.