DatetimeGuesser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 3
b 0
f 0
dl 0
loc 26
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fake() 0 3 1
A getName() 0 3 1
A transform() 0 6 2
A supports() 0 5 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