DatetimeGuesser::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Guesser;
4
5
class DatetimeGuesser extends AbstractGuesser implements GuesserInterface
6
{
7
    public function supports(array $mapping)
8
    {
9
        $mapping = array_merge([ 'type' => null ], $mapping);
10
11
        return in_array($mapping['type'], ['datetime', 'date', 'time']);
12
    }
13
14
    public function transform($str, array $mapping = null)
15
    {
16
        try {
17
            return new \DateTime($str);
18
        } catch (\Exception $e) {
19
            throw new \Exception(sprintf('"%s" is not a supported date/time/datetime format. To know which formats are supported, please visit http://www.php.net/manual/en/datetime.formats.php', $str));
20
        }
21
    }
22
23
    public function fake(array $mapping)
24
    {
25
        return new \DateTime();
26
    }
27
28
    public function getName()
29
    {
30
        return 'datetime';
31
    }
32
}
33