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

DateTimeTransformer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 14 3
A throwInvalidConstraintExceptionForDateTime() 0 8 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