Passed
Pull Request — master (#646)
by
unknown
02:35
created

DateHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 27
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 25 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use DateTime;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Validator\Rule\DateTime. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\ValidationContext;
10
use Yiisoft\Validator\RuleHandlerInterface;
11
use Yiisoft\Validator\Exception\UnexpectedRuleException;
12
13
use function is_string;
14
15
/**
16
 * Validates that the value is a valid date.
17
 */
18
final class DateHandler implements RuleHandlerInterface
19
{
20
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
21
    {
22
        if (!$rule instanceof Date) {
23
            throw new UnexpectedRuleException(Date::class, $rule);
24
        }
25
26
        $result = new Result();
27
28
        if (!is_string($value) || empty($value)|| !preg_match($rule->getPattern(), $rule->getFormat()) ) {
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "||"; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
29
            return $result->addError($rule->getIncorrectInputMessage(), [
30
                'attribute' => $context->getTranslatedAttribute(),
31
                'type' => get_debug_type($value),
32
            ]);
33
        }
34
35
        $date = DateTime::createFromFormat($rule->getFormat(), $value);
36
37
        if ($date === false  || ($date->format($rule->getFormat()) !== $value)) {
38
             $result->addError($rule->getMessage(), [
39
                'attribute' => $context->getTranslatedAttribute(),
40
                'value' => $value,
41
            ]);
42
        }
43
44
        return $result;
45
    }
46
}
47