Completed
Push — issue/87 ( bbd942 )
by Alex
04:44
created

DateTimeBuilder::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Rule;
12
13
use Psr\Log\LoggerInterface;
14
use Psr\Log\NullLogger;
15
16
class DateTimeBuilder
17
{
18
    /**
19
     * Supported date formats
20
     * @var array
21
     */
22
    protected $dateFormats = [
23
        \DateTime::RFC2822,
24
        \DateTime::ATOM,
25
        \DateTime::RFC3339,
26
        \DateTime::RSS,
27
        \DateTime::W3C,
28
        'Y-m-d\TH:i:s.uP',
29
        'Y-m-d\TH:i:s',
30
        'Y-m-d',
31
        'd/m/Y',
32
        'D, d M Y H:i O',
33
        'D, d M Y H:i:s O',
34
        'D M d Y H:i:s e',
35
        '*, m#d#Y - H:i',
36
    ];
37
38
    /**
39
     * @var \DateTimeZone
40
     */
41
    protected $timezone;
42
43
    /**
44
     * @var LoggerInterface
45
     */
46
    protected $logger;
47
48
    /**
49
     * @var string
50
     */
51
    protected $lastGuessedFormat = \DateTime::RFC2822;
52
53
    /**
54
     * @param \Psr\Log\LoggerInterface        $logger
55
     */
56 41
    public function __construct(LoggerInterface $logger = null)
57
    {
58 41
        if ( is_null($logger) ) {
59 34
            $logger = new NullLogger;
60 34
        }
61 41
        $this->logger = $logger;
62 41
        $this->setTimezone(new \DateTimeZone(date_default_timezone_get()));
63 41
    }
64
65
    /**
66
     * @param $dateFormat
67
     * @return $this
68
     */
69 10
    public function addDateFormat($dateFormat)
70
    {
71 10
        $this->dateFormats[] = $dateFormat;
72
73 10
        return $this;
74
    }
75
76
    /**
77
     * @param  array $dateFormats
78
     * @return $this
79
     */
80
    public function setDateFormats(array $dateFormats)
81
    {
82
        $this->dateFormats = $dateFormats;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return string
89
     */
90 5
    public function getLastGuessedFormat()
91
    {
92 5
        return $this->lastGuessedFormat;
93
    }
94
95
    /**
96
     *
97
     * @param  string                   $date
98
     * @return string|false             date Format
99
     * @throws InvalidArgumentException
100
     */
101 5
    public function guessDateFormat($date)
102
    {
103 5
        foreach ($this->dateFormats as $format) {
104 5
            $test = \DateTime::createFromFormat($format, $date);
105 5
            if ($test instanceof \DateTime) {
106 5
                $this->lastGuessedFormat = $format;
107
108 5
                return $format;
109
            }
110 3
        }
111
112
        return false;
113
    }
114
115
    /**
116
     * Creates a DateTime instance for the given string. Default format is RFC2822
117
     * @param  string                   $string
118
     * @return \DateTime
119
     */
120 5
    public function convertToDateTime($string)
121
    {
122 5
        $string = trim($string);
123 5
        foreach ([$this->getLastGuessedFormat(), $this->guessDateFormat($string) ] as $format) {
124 5
            $date = \DateTime::createFromFormat($format, $string);
125 5
            if ($date instanceof \DateTime) {
126 5
                $date->setTimezone($this->getTimezone());
127
128 5
                return $date;
129
            }
130 3
        }
131
132
        return $this->stringToDateTime($string);
133
    }
134
135
    /**
136
     * Creates a DateTime instance for the given string if the format was not catch from the list
137
     * @param  string                   $string
138
     * @return \DateTime
139
     * @throws InvalidArgumentException
140
     */
141
    public function stringToDateTime($string)
142
    {
143
        $this->logger->notice("unsupported date format, use strtotime() to build the DateTime instance : {$string}");
144
145
        if ( false === strtotime($string) ) {
146
            throw new \InvalidArgumentException('Impossible to convert date : '.$string);
147
        }
148
        $date = new \DateTime($string);
149
        $date->setTimezone($this->getTimezone());
150
151
        return $date;
152
    }
153
154
    /**
155
     * @return \DateTimeZone
156
     */
157 5
    public function getTimezone()
158
    {
159 5
        return $this->timezone;
160
    }
161
162
    /**
163
     * @param \DateTimeZone $timezone
164
     */
165 41
    public function setTimezone(\DateTimeZone $timezone)
166
    {
167 41
        $this->timezone = $timezone;
168 41
    }
169
}
170