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

Move out getName() from RuleInterface #640

Merged
merged 9 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
- 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`
(@arogachev)

## 1.1.0 April 06, 2023

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
arogachev marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* 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
4 changes: 2 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
4 changes: 2 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
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
2 changes: 1 addition & 1 deletion src/Rule/Compare.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ final class Compare extends AbstractCompare
{
public function getName(): string
{
return 'compare';
return self::class;
}
}
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 @@ -116,7 +116,7 @@

$this->idnEmailPattern = $idnEmailPattern;

if ($enableIdn && !function_exists('idn_to_ascii')) {

Check warning on line 119 in src/Rule/Email.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ throw new InvalidArgumentException('IDN e-mail pattern can\'t be empty.'); } $this->idnEmailPattern = $idnEmailPattern; - if ($enableIdn && !function_exists('idn_to_ascii')) { + if (!$enableIdn && !function_exists('idn_to_ascii')) { // Tested via separate CI configuration (see ".github/workflows/build.yml"). // @codeCoverageIgnoreStart throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
// Tested via separate CI configuration (see ".github/workflows/build.yml").
// @codeCoverageIgnoreStart
throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
Expand All @@ -126,7 +126,7 @@

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Equal.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ public function __construct(

public function getName(): string
{
return 'equal';
return self::class;
}
}
2 changes: 1 addition & 1 deletion src/Rule/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ public function __construct(

public function getName(): string
{
return 'greaterThan';
return self::class;
}
}
2 changes: 1 addition & 1 deletion src/Rule/GreaterThanOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ public function __construct(

public function getName(): string
arogachev marked this conversation as resolved.
Show resolved Hide resolved
{
return 'greaterThanOrEqual';
return self::class;
}
}
Loading
Loading