Skip to content

Commit

Permalink
Move out getName() from RuleInterface (#640)
Browse files Browse the repository at this point in the history
* Move out RuleInterface::getName()

* Adjust docs and rename interface

* Update CHANGELOG [skip ci]

* Fix Psalm and tests

* Apply fixes from StyleCI

* More removing

* Simplify getName() for abstract classes

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
arogachev and StyleCIBot authored Nov 30, 2023
1 parent ec8c356 commit e052235
Show file tree
Hide file tree
Showing 79 changed files with 193 additions and 275 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
- Bug #632: Fix property name usage in error messages of rules in `Nested` rule (@vjik)
- Enh #636: Improve psalm annotations in `Result` class (@vjik)
- Enh #637: Add German translation (@took)
- Chg #634: Move `getName()` method from `RuleInterface` to `RuleWithOptionsInterface` (@arogachev)
- Chg #634: Rename `RuleWithOptionsInterface` to `DumpedRuleInterface` (@arogachev)
- Chg #634: Use FQCN as a name for built-in rules during export with `RulesDumper` (@arogachev)
- Chg #634: Use FQCN as a name for rules not implementing `DumpedRuleInterface` during export with `RulesDumper`
- Enh #622: Use `json_validate()` built-in PHP function in `JsonHandler` if code is run with PHP 8.3 (@arogachev)
- Enh #639: Simplify validation of JSON in `JsonHandler` using built-in PHP functions for PHP versions below 8.3
(@arogachev)
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/en/client-side-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ the output will be:
[
'name' => [
[
'length',
'Yiisoft\Validator\Rule\Length',
'min' => 4,
'max' => 10,
'exactly' => null,
Expand Down
5 changes: 0 additions & 5 deletions docs/guide/en/configuring-rules-via-php-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,6 @@ final class Yaml implements RuleInterface
) {
}

public function getName(): string
{
return 'yaml';
}

public function getHandler(): string
{
return YamlHandler::class;
Expand Down
17 changes: 1 addition & 16 deletions docs/guide/en/creating-custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ final class RgbColor implements RuleInterface
public function __construct(
public readonly string $message = 'Invalid RGB color value.',
) {
}

public function getName(): string
{
return 'rgbColor';
}
}

public function getHandler(): string
{
Expand Down Expand Up @@ -118,11 +113,6 @@ final class RgbColor implements RuleInterface
) {
}

public function getName(): string
{
return 'rgbColor';
}

public function getHandler(): string
{
return RgbColorHandler::class;
Expand Down Expand Up @@ -330,11 +320,6 @@ final class Yaml implements RuleInterface
) {
}

public function getName(): string
{
return 'yaml';
}

public function getHandler(): string
{
return YamlHandler::class;
Expand Down
18 changes: 13 additions & 5 deletions src/RuleWithOptionsInterface.php → src/DumpedRuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
namespace Yiisoft\Validator;

/**
* An extended version of {@see RuleInterface} which allows exporting a rule options in addition to a rule name. It's
* useful for passing to frontend for further identification and implementing client-side validation. If you don't need
* that (for example for REST API), use {@see RuleInterface} instead.
* An extended version of {@see RuleInterface} which allows exporting a rule options and name customization. It's useful
* for passing to frontend for further identification and implementing client-side validation. If you don't need that
* (for example for REST API), use {@see RuleInterface} instead.
*/
interface RuleWithOptionsInterface extends RuleInterface
interface DumpedRuleInterface extends RuleInterface
{
/**
* Returns the name of a rule used during conversion to array. It's used for identification on the frontend with
* further implementing of client-side validation.
*
* @return string A rule name.
*/
public function getName(): string;

/**
* Gets a rule options as associative array. It's used for identification on the frontend with further implementing
* of client-side validation. Usually it's just a mapping between rule property names and values.
Expand Down Expand Up @@ -46,7 +54,7 @@ interface RuleWithOptionsInterface extends RuleInterface
* ```
*
* Note that the values that can't be serialized to frontend such as callable must be excluded because they will be
* useless on frontend. No exception will be thrown, so it's on the conscience of developer.
* useless on frontend. No exceptions are thrown in such cases.
*
* @return array A rule options.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Helper/RulesDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use InvalidArgumentException;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;

use function is_int;
use function is_string;
Expand All @@ -16,7 +16,7 @@
* The array is usually passed to the client to use it in client-side validation.
*
* @see RuleInterface
* @see RuleWithOptionsInterface
* @see DumpedRuleInterface
*/
final class RulesDumper
{
Expand Down Expand Up @@ -96,10 +96,10 @@ private static function fetchOptions(iterable $rules): array

if (is_iterable($rule)) {
$options = self::fetchOptions($rule);
} elseif ($rule instanceof RuleWithOptionsInterface) {
} elseif ($rule instanceof DumpedRuleInterface) {
$options = array_merge([$rule->getName()], $rule->getOptions());
} elseif ($rule instanceof RuleInterface) {
$options = [$rule->getName()];
$options = [$rule::class];
} else {
throw new InvalidArgumentException(sprintf(
'Every rule must implement "%s". Type "%s" given.',
Expand Down
9 changes: 7 additions & 2 deletions src/Rule/AbstractCompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -34,7 +34,7 @@
* @psalm-import-type WhenType from WhenInterface
*/
abstract class AbstractCompare implements
RuleWithOptionsInterface,
DumpedRuleInterface,
SkipOnEmptyInterface,
SkipOnErrorInterface,
WhenInterface
Expand Down Expand Up @@ -169,6 +169,11 @@ public function __construct(
}
}

public function getName(): string
{
return self::class;
}

/**
* Get value to be compared with.
*
Expand Down
9 changes: 7 additions & 2 deletions src/Rule/AbstractNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -26,7 +26,7 @@
* @psalm-import-type WhenType from WhenInterface
*/
abstract class AbstractNumber implements
RuleWithOptionsInterface,
DumpedRuleInterface,
SkipOnErrorInterface,
WhenInterface,
SkipOnEmptyInterface
Expand Down Expand Up @@ -113,6 +113,11 @@ public function __construct(
$this->pattern = $pattern;
}

public function getName(): string
{
return self::class;
}

/**
* Get lower limit of the number. `null` means no lower limit.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/AtLeast.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -27,7 +27,7 @@
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class AtLeast implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
final class AtLeast implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
Expand Down Expand Up @@ -74,7 +74,7 @@ public function __construct(

public function getName(): string
{
return 'atLeast';
return self::class;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/BooleanValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -27,7 +27,7 @@
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class BooleanValue implements RuleWithOptionsInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
final class BooleanValue implements DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
Expand Down Expand Up @@ -91,7 +91,7 @@ public function __construct(

public function getName(): string
{
return 'boolean';
return self::class;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -26,7 +26,7 @@
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Callback implements
RuleWithOptionsInterface,
DumpedRuleInterface,
SkipOnErrorInterface,
WhenInterface,
SkipOnEmptyInterface,
Expand Down Expand Up @@ -71,7 +71,7 @@ public function __construct(

public function getName(): string
{
return 'callback';
return self::class;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/Rule/Compare.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,4 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Compare extends AbstractCompare
{
public function getName(): string
{
return 'compare';
}
}
6 changes: 3 additions & 3 deletions src/Rule/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\Helper\RulesDumper;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\ValidatorInterface;
Expand Down Expand Up @@ -74,7 +74,7 @@
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
class Composite implements
RuleWithOptionsInterface,
DumpedRuleInterface,
SkipOnEmptyInterface,
SkipOnErrorInterface,
WhenInterface,
Expand Down Expand Up @@ -136,7 +136,7 @@ public function __construct(

public function getName(): string
{
return 'composite';
return self::class;
}

#[ArrayShape([
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -27,7 +27,7 @@
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Count implements
RuleWithOptionsInterface,
DumpedRuleInterface,
SkipOnErrorInterface,
WhenInterface,
SkipOnEmptyInterface,
Expand Down Expand Up @@ -109,7 +109,7 @@ public function __construct(

public function getName(): string
{
return 'count';
return self::class;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\Helper\RulesDumper;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\ValidatorInterface;
Expand Down Expand Up @@ -63,7 +63,7 @@
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Each implements
RuleWithOptionsInterface,
DumpedRuleInterface,
SkipOnEmptyInterface,
SkipOnErrorInterface,
WhenInterface,
Expand Down Expand Up @@ -123,7 +123,7 @@ public function __construct(

public function getName(): string
{
return 'each';
return self::class;
}

public function propagateOptions(): void
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -26,7 +26,7 @@
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Email implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
final class Email implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
Expand Down Expand Up @@ -126,7 +126,7 @@ public function __construct(

public function getName(): string
{
return 'email';
return self::class;
}

/**
Expand Down
Loading

0 comments on commit e052235

Please sign in to comment.