Skip to content

Commit

Permalink
Use convention to idenfity custom templates
Browse files Browse the repository at this point in the history
With this convention, it's much simpler to identify whether an exception
has a custom template or if that template came from the rule itself.
This commit is a preparation for further changes.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 14, 2024
1 parent 26e9fb9 commit 7199c5e
Show file tree
Hide file tree
Showing 24 changed files with 41 additions and 40 deletions.
3 changes: 2 additions & 1 deletion library/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Respect\Validation\Message\TemplateRenderer;

use function count;
use function preg_match;

class ValidationException extends InvalidArgumentException implements Exception
{
Expand Down Expand Up @@ -88,7 +89,7 @@ public function updateParams(array $params): void

public function hasCustomTemplate(): bool
{
return $this->getTemplateString() === $this->template;
return preg_match('/__[a-z_]+_/', $this->template) === 0;
}

private function getTemplateString(): string
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/AbstractFilterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

abstract class AbstractFilterRule extends AbstractRule
{
public const TEMPLATE_EXTRA = 'extra';
public const TEMPLATE_EXTRA = '__extra__';

private readonly string $additionalChars;

Expand Down
4 changes: 2 additions & 2 deletions library/Rules/AbstractRelated.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

abstract class AbstractRelated extends AbstractRule
{
public const TEMPLATE_NOT_PRESENT = 'not_present';
public const TEMPLATE_INVALID = 'invalid';
public const TEMPLATE_NOT_PRESENT = '__not_present__';
public const TEMPLATE_INVALID = '__invalid__';

abstract public function hasReference(mixed $input): bool;

Expand Down
4 changes: 2 additions & 2 deletions library/Rules/AllOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
)]
class AllOf extends AbstractComposite
{
public const TEMPLATE_NONE = 'none';
public const TEMPLATE_SOME = 'some';
public const TEMPLATE_NONE = '__none__';
public const TEMPLATE_SOME = '__some__';

public function assert(mixed $input): void
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/AlwaysInvalid.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)]
final class AlwaysInvalid extends AbstractRule
{
public const TEMPLATE_SIMPLE = 'simple';
public const TEMPLATE_SIMPLE = '__simple__';

public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)]
final class CreditCard extends AbstractRule
{
public const TEMPLATE_BRANDED = 'branded';
public const TEMPLATE_BRANDED = '__branded__';

public const ANY = 'Any';

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class DateTime extends AbstractRule
{
use CanValidateDateTime;

public const TEMPLATE_FORMAT = 'format';
public const TEMPLATE_FORMAT = '__format__';

public function __construct(
private readonly ?string $format = null
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
)]
final class Ip extends AbstractRule
{
public const TEMPLATE_NETWORK_RANGE = 'network_range';
public const TEMPLATE_NETWORK_RANGE = '__network_range__';

private ?string $range = null;

Expand Down
8 changes: 4 additions & 4 deletions library/Rules/KeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
)]
final class KeySet extends AbstractWrapper implements NonNegatable
{
public const TEMPLATE_NONE = 'none';
public const TEMPLATE_SOME = 'some';
public const TEMPLATE_STRUCTURE = 'structure';
public const TEMPLATE_STRUCTURE_EXTRA = 'structure_extra';
public const TEMPLATE_NONE = '__none__';
public const TEMPLATE_SOME = '__some__';
public const TEMPLATE_STRUCTURE = '__structure__';
public const TEMPLATE_STRUCTURE_EXTRA = '__structure_extra__';

/**
* @var mixed[]
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/KeyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)]
final class KeyValue extends AbstractRule
{
public const TEMPLATE_COMPONENT = 'component';
public const TEMPLATE_COMPONENT = '__component__';

public function __construct(
private readonly int|string $comparedKey,
Expand Down
12 changes: 6 additions & 6 deletions library/Rules/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
)]
final class Length extends AbstractRule
{
public const TEMPLATE_LOWER = 'lower';
public const TEMPLATE_GREATER = 'greater';
public const TEMPLATE_GREATER_INCLUSIVE = 'greater_inclusive';
public const TEMPLATE_EXACT = 'exact';
public const TEMPLATE_LOWER_INCLUSIVE = 'lower_inclusive';
public const TEMPLATE_BOTH = 'both';
public const TEMPLATE_LOWER = '__lower__';
public const TEMPLATE_GREATER = '__greater__';
public const TEMPLATE_GREATER_INCLUSIVE = '__greater_inclusive__';
public const TEMPLATE_EXACT = '__exact__';
public const TEMPLATE_LOWER_INCLUSIVE = '__lower_inclusive__';
public const TEMPLATE_BOTH = '__both__';

public function __construct(
private readonly ?int $minValue = null,
Expand Down
4 changes: 2 additions & 2 deletions library/Rules/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
)]
final class Not extends AbstractRule
{
public const TEMPLATE_NONE = 'none';
public const TEMPLATE_SOME = 'some';
public const TEMPLATE_NONE = '__none__';
public const TEMPLATE_SOME = '__some__';

private readonly Validatable $rule;

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/NotBlank.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)]
final class NotBlank extends AbstractRule
{
public const TEMPLATE_NAMED = 'named';
public const TEMPLATE_NAMED = '__named__';

public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
)]
final class NotEmpty extends AbstractRule
{
public const TEMPLATE_NAMED = 'named';
public const TEMPLATE_NAMED = '__named__';

public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/NotOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class NotOptional extends AbstractRule
{
use CanValidateUndefined;

public const TEMPLATE_NAMED = 'named';
public const TEMPLATE_NAMED = '__named__';

public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Nullable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)]
final class Nullable extends AbstractWrapper
{
public const TEMPLATE_NAMED = 'named';
public const TEMPLATE_NAMED = '__named__';

public function assert(mixed $input): void
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class Optional extends AbstractWrapper
{
use CanValidateUndefined;

public const TEMPLATE_NAMED = 'named';
public const TEMPLATE_NAMED = '__named__';

public function assert(mixed $input): void
{
Expand Down
4 changes: 2 additions & 2 deletions library/Rules/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
)]
final class Phone extends AbstractRule
{
public const TEMPLATE_FOR_COUNTRY = 'for_country';
public const TEMPLATE_INTERNATIONAL = 'international';
public const TEMPLATE_FOR_COUNTRY = '__for_country__';
public const TEMPLATE_INTERNATIONAL = '__international__';

private readonly ?Countries\Country $country;

Expand Down
6 changes: 3 additions & 3 deletions library/Rules/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
)]
final class Size extends AbstractRule
{
public const TEMPLATE_LOWER = 'lower';
public const TEMPLATE_GREATER = 'greater';
public const TEMPLATE_BOTH = 'both';
public const TEMPLATE_LOWER = '__lower__';
public const TEMPLATE_GREATER = '__greater__';
public const TEMPLATE_BOTH = '__both__';

private readonly ?float $minValue;

Expand Down
4 changes: 2 additions & 2 deletions library/Rules/Sorted.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
)]
final class Sorted extends AbstractRule
{
public const TEMPLATE_ASCENDING = 'ascending';
public const TEMPLATE_DESCENDING = 'descending';
public const TEMPLATE_ASCENDING = '__ascending__';
public const TEMPLATE_DESCENDING = '__descending__';

public const ASCENDING = 'ASC';
public const DESCENDING = 'DESC';
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)]
final class Uuid extends AbstractRule
{
public const TEMPLATE_VERSION = 'version';
public const TEMPLATE_VERSION = '__version__';

private const PATTERN_FORMAT = '/^[0-9a-f]{8}-[0-9a-f]{4}-%s[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i';

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/VideoUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)]
final class VideoUrl extends AbstractRule
{
public const TEMPLATE_SERVICE = 'service';
public const TEMPLATE_SERVICE = '__service__';

private const SERVICES = [
// phpcs:disable Generic.Files.LineLength.TooLong
Expand Down
2 changes: 1 addition & 1 deletion library/Validatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

interface Validatable
{
public const TEMPLATE_STANDARD = 'standard';
public const TEMPLATE_STANDARD = '__standard__';

public function assert(mixed $input): void;

Expand Down
4 changes: 2 additions & 2 deletions library/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
)]
final class Validator extends AllOf
{
public const TEMPLATE_NONE = 'none';
public const TEMPLATE_SOME = 'some';
public const TEMPLATE_NONE = '__none__';
public const TEMPLATE_SOME = '__some__';

public static function create(): self
{
Expand Down

0 comments on commit 7199c5e

Please sign in to comment.