Skip to content

Commit

Permalink
Merge branch '2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemoody committed Feb 4, 2024
2 parents 84f1d32 + d8cd6a3 commit 13afaa0
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 21 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Message placeholder conversion
# Message placeholder conversion

Messages in Validation usually have placeholders that are in between "{{" and
"}}" characters. To replace those placeholders with the real parameters, we need
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# List of rules
# List of rules by category

## Arrays

Expand Down
File renamed without changes.
43 changes: 35 additions & 8 deletions docs/rules/DateTime.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@

# DateTime

- `DateTime()`
- `DateTime(string $format)`

Validates whether an input is a date/time or not. The `$format` argument should
be in accordance to PHP's [date()](http://php.net/date) function.
Validates whether an input is a date/time or not.

The `$format` argument should be in accordance to [DateTime::format()][]. See more in the [Formats](#formats) section.

When a `$format` is not given its default value is `Y-m-d H:i:s`.

```php
v::dateTime()->validate('2009-01-01'); // true
```

Also accepts strtotime values:
Also accepts [strtotime()](http://php.net/strtotime) values:

```php
v::dateTime()->validate('now'); // true
Expand All @@ -31,18 +35,38 @@ v::dateTime('Y-m-d')->validate('01-01-2009'); // false

Format has no effect when validating DateTime instances.

Message template for this validator includes `{{format}}`.
Message template for this validator includes `{{sample}}`.

## Formats

Note that this rule validates whether the input **matches a given [DateTime::format()][] format** and **NOT if the input
can be parsed with a given [DateTimeImmutable::createFromFormat()][] format**. That makes the validation stricter but
offers some limitations.

The way [DateTimeImmutable::createFromFormat()][] parses an input allows for many different conversions. Overall
[DateTimeImmutable::createFromFormat()][] tend to be more lenient than [DateTime::format()][]. This might be what
you desire, and you may want to use [Callback](Callback.md) to create a custom validation.

```php
$input = '2014-04-12T23:20:50.052Z';

v::callback(fn($input) => is_string($input) && DateTime::createFromFormat(DateTime::RFC3339_EXTENDED, $input))
->validate($input); // true

v::dateTime(DateTime::RFC3339_EXTENDED)->validate($input); // false
```

## Categorization

- Date and Time

## Changelog

Version | Description
--------|-------------
2.2.4 | `v::dateTime('z')` is no longer supported.
2.0.0 | Created
| Version | Description |
|---------|--------------------------------------------|
| 2.3.0 | Validation became a lot stricter |
| 2.2.4 | `v::dateTime('z')` is no longer supported. |
| 2.0.0 | Created |

***
See also:
Expand All @@ -53,3 +77,6 @@ See also:
- [LeapYear](LeapYear.md)
- [MinAge](MinAge.md)
- [Time](Time.md)

[DateTimeImmutable::createFromFormat()]: https://www.php.net/datetimeimmutable.createfromformat
[DateTime::format()]: https://www.php.net/datetime.format
3 changes: 1 addition & 2 deletions library/Rules/AbstractSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use function in_array;
use function is_scalar;
use function mb_strtoupper;

abstract class AbstractSearcher extends AbstractRule
{
Expand All @@ -36,6 +35,6 @@ public function validate(mixed $input): bool
return false;
}

return in_array(mb_strtoupper((string) $input), $dataSource, true);
return in_array((string) $input, $dataSource, true);
}
}
27 changes: 20 additions & 7 deletions library/Rules/PublicDomainSuffix.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,38 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Attributes\Template;
use Respect\Validation\Helpers\CanValidateUndefined;
use Respect\Validation\Helpers\DomainInfo;

use function array_pop;
use function explode;
use function in_array;
use function is_scalar;
use function strtoupper;

#[Template(
'{{name}} must be a public domain suffix',
'{{name}} must be a public domain suffix',
)]
final class PublicDomainSuffix extends AbstractSearcher
final class PublicDomainSuffix extends AbstractRule
{
/**
* @return string[]
*/
protected function getDataSource(mixed $input = null): array
use CanValidateUndefined;

public function validate(mixed $input): bool
{
$parts = explode('.', $input);
if (!is_scalar($input)) {
return false;
}

$parts = explode('.', (string) $input);
$tld = array_pop($parts);

return (new DomainInfo($tld))->getPublicSuffixes();
$domainInfo = new DomainInfo($tld);
$dataSource = $domainInfo->getPublicSuffixes();
if ($this->isUndefined($input) && empty($dataSource)) {
return true;
}

return in_array(strtoupper((string) $input), $dataSource, true);
}
}
14 changes: 12 additions & 2 deletions tests/library/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use function realpath;
use function Respect\Stringifier\stringify;
use function sprintf;
use function strrchr;
use function substr;

abstract class RuleTestCase extends TestCase
{
Expand Down Expand Up @@ -71,15 +73,23 @@ public static function assertValidInput(Validatable $rule, mixed $input): void
{
self::assertTrue(
$rule->validate($input),
sprintf('Validation with input %s is expected to pass', stringify($input))
sprintf(
'%s should pass with %s',
substr((string) strrchr($rule::class, '\\'), 1),
stringify($rule->reportError($input)->getParams())
)
);
}

public static function assertInvalidInput(Validatable $rule, mixed $input): void
{
self::assertFalse(
$rule->validate($input),
sprintf('Validation with input %s it not expected to pass', stringify($input))
sprintf(
'%s should not pass with %s',
substr((string) strrchr($rule::class, '\\'), 1),
stringify($rule->reportError($input)->getParams())
)
);
}
}
1 change: 1 addition & 0 deletions tests/unit/Rules/CountryCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static function providerForValidInput(): array
public static function providerForInvalidInput(): array
{
return [
[new CountryCode(), 'ca'],
[new CountryCode(CountryCode::ALPHA2), 'USA'],
[new CountryCode(CountryCode::ALPHA3), 'US'],
[new CountryCode(CountryCode::NUMERIC), '000'],
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/Rules/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use DateTime as DateTimeMutable;
use DateTimeImmutable;
use DateTimeInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
Expand Down Expand Up @@ -79,6 +80,8 @@ public static function providerForValidInput(): array
[new DateTime('U'), 1464658596],
[new DateTime('h'), 6],
[new DateTime('Ym'), 202305],
[new DateTime(DateTimeInterface::RFC3339), '2018-02-23T12:00:00+00:00'],
[new DateTime(DateTimeInterface::RFC3339_EXTENDED), '2024-02-04T14:14:47.000+00:00'],
];
}

Expand All @@ -100,6 +103,8 @@ public static function providerForInvalidInput(): array
[new DateTime('c'), new DateTimeMutable()],
[new DateTime('c'), new DateTimeImmutable()],
[new DateTime('Y-m-d H:i:s'), '21-3-123:12:01'],
[new DateTime(DateTimeInterface::RFC3339_EXTENDED), '2005-12-30T01:02:03Z'],
[new DateTime(DateTimeInterface::RFC3339_EXTENDED), '1937-01-01T12:00:27.87+00:20'],
];
}
}
4 changes: 4 additions & 0 deletions tests/unit/Rules/PublicDomainSuffixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[CoversClass(PublicDomainSuffix::class)]
Expand Down Expand Up @@ -39,6 +40,9 @@ public static function providerForInvalidInput(): array
$rule = new PublicDomainSuffix();

return [
[$rule, []],
[$rule, null],
[$rule, new stdClass()],
[$rule, 'NONONONONONONONONON'],
[$rule, 'NONONONONONONONONON.uk'],
[$rule, 'invalid.com'],
Expand Down

0 comments on commit 13afaa0

Please sign in to comment.