Completed
Push — master ( d57f97...544bed )
by Guillaume
02:02
created

DateValidatorTrait::mustBeDate()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 7
eloc 17
nc 3
nop 4
crap 7
1
<?php
2
3
namespace AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pure;
4
5
use AmmitPhp\Ammit\UI\Resolver\UIValidationEngine;
6
use AmmitPhp\Ammit\UI\Resolver\Validator\InvalidArgumentException;
7
use AmmitPhp\Ammit\Domain\DateValidation;
8
use AmmitPhp\Ammit\UI\Resolver\Validator\UIValidatorInterface;
9
10
trait DateValidatorTrait
11
{
12
    /** @var UIValidationEngine */
13
    protected $validationEngine;
14
15
    /**
16
     * Exceptions are caught in order to be processed later
17
     * @param mixed $value Date Y-m-d ?
18
     *
19
     * @return \DateTime
20
     */
21
    public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
22
    {
23 1
        $dateValidation = new DateValidation();
24 1
        $this->validationEngine->validateFieldValue(
25 1
            $parentValidator ?: $this,
26 1
            function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) {
27 1
                if (null === $value || ! $dateValidation->isDateValid($value)) {
28 1
                    throw new InvalidArgumentException(
29 1
                        $exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value),
30 1
                        0,
31
                        $propertyPath,
32
                        $value
33
                    );
34
                }
35 1
            }
36
        );
37
38 1
        if (null === $value) {
39 1
            return $this->createDefaultDateTime(); // Invalid
40
        }
41
42 1
        $date = $dateValidation->createDateFromString($value);
43 1
        if ($date instanceof \DateTime) {
44 1
            return $date->setTime(0, 0, 0); // Valid
45
        }
46
47 1
        return $this->createDefaultDateTime(); // Invalid
48
    }
49
50
    /**
51
     * Exceptions are caught in order to be processed later
52
     * @param mixed $value null|DateTime Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 or null ?
53
     *
54
     * @return \DateTime|null
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\DateTime|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
     */
56 View Code Duplication
    public function mustBeDateTimeOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58 1
        if (empty($value)) {
59 1
            return null;
60
        }
61
62 1
        return $this->mustBeDateTime($value, $propertyPath, $parentValidator, $exceptionMessage);
63
    }
64
65
    /**
66
     * Exceptions are caught in order to be processed later
67
     * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ?
68
     *
69
     * @return \DateTime|false
70
     */
71
    public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
72
    {
73 1
        $dateValidation = new DateValidation();
74
75 1
        $this->validationEngine->validateFieldValue(
76 1
            $parentValidator ?: $this,
77 1
            function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) {
78 1
                if (null === $value || ! $dateValidation->isDateTimeValid($value)) {
79 1
                    throw new InvalidArgumentException(
80 1
                        $exceptionMessage ?: sprintf('Datetime "%s" format invalid, must be Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00.', $value),
81 1
                        0,
82
                        $propertyPath,
83
                        $value
84
                    );
85
                }
86 1
            }
87
        );
88
89 1
        if (null === $value) {
90 1
            return $this->createDefaultDateTime(); // Invalid
91
        }
92
93 1
        $date = $dateValidation->createDateTimeFromString($value);
94
95 1
        if ($date instanceof \DateTime) {
96 1
            return $date; // Valid
97
        }
98
99 1
        return $this->createDefaultDateTime();  // Invalid
100
    }
101
102
    private function createDefaultDateTime(): \DateTime
103
    {
104 1
        $date = new \DateTime();
105
106 1
        return $date->setTime(0, 0, 0);
107
    }
108
}
109