Completed
Pull Request — master (#30)
by
unknown
03:40
created

throwInvalidConstraintExceptionForDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Transformer;
6
7
use Linio\Component\Input\Exception\InvalidConstraintException;
8
9
class DateTimeTransformer implements TransformerInterface
10
{
11
    public function transform($value)
12
    {
13
        if (empty($value)) {
14
            $this->throwInvalidConstraintExceptionForDateTime($value);
15
        }
16
17
        try {
18
            $date = new \DateTime($value);
19
        } catch (\Exception $e) {
20
            $this->throwInvalidConstraintExceptionForDateTime($value);
21
        }
22
23
        return $date;
24
    }
25
26
    public function throwInvalidConstraintExceptionForDateTime(string $value)
27
    {
28
        throw new InvalidConstraintException(sprintf(
29
                'Value "%s" is not of type %s',
30
                $value,
31
                \DateTime::class)
32
        );
33
    }
34
}
35