Completed
Push — master ( 73f405...9e0eff )
by Guillaume
02:38
created

RawValueValidator::mustBeInteger()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 23
loc 23
ccs 10
cts 10
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 4
crap 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver\Validator;
5
6
use Assert\Assertion;
7
use Assert\InvalidArgumentException;
8
use Imedia\Ammit\Domain\DateValidation;
9
use Imedia\Ammit\UI\Resolver\Exception\UIValidationException;
10
use Imedia\Ammit\UI\Resolver\UIValidationEngine;
11
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure\BooleanValidatorTrait;
12
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure\FloatValidatorTrait;
13
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure\IntegerValidatorTrait;
14
15
/**
16
 * @author Guillaume MOREL <[email protected]>
17
 */
18
class RawValueValidator implements UIValidatorInterface
19
{
20
    use BooleanValidatorTrait;
21
    use IntegerValidatorTrait;
22
    use FloatValidatorTrait;
23
24
    /** @var UIValidationEngine */
25
    protected $validationEngine;
26
27
    public function __construct(UIValidationEngine $validationEngine)
28
    {
29 1
        $this->validationEngine = $validationEngine;
30 1
    }
31
32
    /**
33
     * Exceptions are caught in order to be processed later
34
     * @param mixed $value String ?
35
     *
36
     * @return mixed Untouched value
37
     */
38 View Code Duplication
    public function mustBeString($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...
39
    {
40 1
        $this->validationEngine->validateFieldValue(
41 1
            $parentValidator ?: $this,
42 1
            function() use ($value, $propertyPath, $exceptionMessage) {
43 1
                Assertion::string(
44
                    $value,
45
                    $exceptionMessage,
46
                    $propertyPath
47
                );
48 1
            }
49
        );
50
51 1
        return $value;
52
    }
53
54
    /**
55
     * Exceptions are caught in order to be processed later
56
     * @param mixed $value Array ?
57
     *
58
     * @return mixed Untouched value
59
     */
60 View Code Duplication
    public function mustBeArray($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...
61
    {
62 1
        $this->validationEngine->validateFieldValue(
63 1
            $parentValidator ?: $this,
64 1
            function() use ($value, $propertyPath, $exceptionMessage) {
65 1
                Assertion::isArray(
66
                    $value,
67
                    $exceptionMessage,
68
                    $propertyPath
69
                );
70 1
            }
71
        );
72
73 1
        return $value;
74
    }
75
76
    /**
77
     * Exceptions are caught in order to be processed later
78
     * @param mixed $value Date Y-m-d ?
79
     *
80
     * @return mixed Untouched value
81
     */
82 View Code Duplication
    public function mustBeDate($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...
83
    {
84 1
        $this->validationEngine->validateFieldValue(
85 1
            $parentValidator ?: $this,
86 1
            function() use ($value, $propertyPath, $exceptionMessage) {
87 1
                $dateValidation = new DateValidation();
88 1
                if (! $dateValidation->isDateValid($value)) {
89 1
                    throw new InvalidArgumentException(
90 1
                        $exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value),
91 1
                        0,
92
                        $propertyPath,
93
                        $value
94
                    );
95
                }
96 1
            }
97
        );
98
99 1
        return $value;
100
    }
101
102
    /**
103
     * Exceptions are caught in order to be processed later
104
     * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ?
105
     *
106
     * @return mixed Untouched value
107
     */
108 View Code Duplication
    public function mustBeDateTime($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...
109
    {
110 1
        $this->validationEngine->validateFieldValue(
111 1
            $parentValidator ?: $this,
112 1
            function() use ($value, $propertyPath, $exceptionMessage) {
113 1
                $dateValidation = new DateValidation();
114 1
                if (! $dateValidation->isDateTimeValid($value)) {
115 1
                    throw new InvalidArgumentException(
116 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),
117 1
                        0,
118
                        $propertyPath,
119
                        $value
120
                    );
121
                }
122 1
            }
123
        );
124
125 1
        return $value;
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
132
    {
133 1
        return UIValidationException::fromRaw($message, $propertyPath);
134
    }
135
}
136