Completed
Pull Request — master (#19)
by John
05:56 queued 03:42
created

AbstractFieldParserDate::getDateFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Graze\CiffRenderer\Parser\FieldParser\FieldParserDate;
4
5
use Graze\CiffRenderer\Parser\FieldParser\FieldParserFixedText;
6
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
7
use Graze\CiffRenderer\Parser\FieldParser\FieldParserDate\DateFormatter\DateFormatterFactory;
8
use Graze\CiffRenderer\Exception\UnsupportedDateFormatException;
9
10
abstract class AbstractFieldParserDate extends FieldParserFixedText implements FieldParserInterface
11
{
12
    /**
13
     * @var DateFormatterFactory
14
     */
15
    private $dateFormatterFactory;
16
17
    /**
18
     * @param DateFormatterFactory $dateFormatterFactory
19
     */
20
    public function __construct(DateFormatterFactory $dateFormatterFactory)
21
    {
22
        $this->dateFormatterFactory = $dateFormatterFactory;
23
    }
24
25
    /**
26
     * @return string
27
     * @throws UnsupportedDateFormatException
28
     */
29
    public function getText()
30
    {
31
        try {
32
            $dateFormat = $this->getDateFormat();
33
            $dateFormatter = $this->dateFormatterFactory->getFormatter($dateFormat);
34
            return $dateFormatter->format($this->getDateTime(), $dateFormat);
35
        } catch (\Exception $e) {
36
            throw new UnsupportedDateFormatException($this->getDateFormat());
37
        }
38
    }
39
40
    /**
41
     * Fetch a DateTime object set to the correct date/time.
42
     *
43
     * @return \DateTimeInterface
44
     */
45
    abstract protected function getDateTime();
46
47
    /**
48
     * @return string
49
     */
50
    public function getDateFormat()
51
    {
52
        return (string) $this->xmlField->Data->Object->Default;
53
    }
54
55
    /**
56
     * @return FieldParserInterface
57
     */
58
    public static function factory()
59
    {
60
        return new static(
61
            new DateFormatterFactory()
62
        );
63
    }
64
}
65