Completed
Push — master ( 9e0eff...358dda )
by Guillaume
02:44
created

RawValueValidator::mustBeArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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