azmeuk /
feed-io
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php declare(strict_types=1); |
||
| 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 implements DateTimeBuilderInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Supported date formats |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $dateFormats = [ |
||
| 23 | \DateTime::RFC2822, |
||
| 24 | \DateTime::ATOM, |
||
| 25 | \DateTime::RFC3339, |
||
| 26 | \DateTime::RFC3339_EXTENDED, |
||
| 27 | \DateTime::RSS, |
||
| 28 | \DateTime::W3C, |
||
| 29 | 'Y-m-d\TH:i:s.uP', |
||
| 30 | 'Y-m-d\TH:i:s.uvP', |
||
| 31 | 'Y-m-d\TH:i:s', |
||
| 32 | 'Y-m-d', |
||
| 33 | 'd/m/Y', |
||
| 34 | 'D, d M Y H:i O', |
||
| 35 | 'D, d M Y H:i:s O', |
||
| 36 | 'D M d Y H:i:s e', |
||
| 37 | '*, m#d#Y - H:i', |
||
| 38 | 'D, d M Y H:i:s \U\T', |
||
| 39 | ]; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \DateTimeZone |
||
| 43 | */ |
||
| 44 | protected $feedTimezone; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \DateTimeZone |
||
| 48 | */ |
||
| 49 | protected $serverTimezone; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var LoggerInterface |
||
| 53 | */ |
||
| 54 | protected $logger; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $lastGuessedFormat = \DateTime::RFC2822; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param \Psr\Log\LoggerInterface $logger |
||
| 63 | */ |
||
| 64 | 80 | public function __construct(LoggerInterface $logger = null) |
|
| 65 | { |
||
| 66 | 80 | if (is_null($logger)) { |
|
| 67 | 67 | $logger = new NullLogger; |
|
| 68 | } |
||
| 69 | 80 | $this->logger = $logger; |
|
| 70 | 80 | $this->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|
| 71 | 80 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param $dateFormat |
||
| 75 | * @return DateTimeBuilder |
||
| 76 | */ |
||
| 77 | 24 | public function addDateFormat(string $dateFormat) : DateTimeBuilderInterface |
|
| 78 | { |
||
| 79 | 24 | $this->dateFormats[] = $dateFormat; |
|
| 80 | |||
| 81 | 24 | return $this; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param array $dateFormats |
||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | 3 | public function setDateFormats(array $dateFormats) : DateTimeBuilderInterface |
|
| 89 | { |
||
| 90 | 3 | $this->dateFormats = $dateFormats; |
|
| 91 | |||
| 92 | 3 | return $this; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | 15 | public function getLastGuessedFormat() : string |
|
| 99 | { |
||
| 100 | 15 | return $this->lastGuessedFormat; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Tries to guess the date's format from the list |
||
| 105 | * @param string $date |
||
| 106 | * @return string|null date Format |
||
| 107 | */ |
||
| 108 | 17 | public function guessDateFormat(string $date) : ? string |
|
| 109 | { |
||
| 110 | 17 | foreach ($this->dateFormats as $format) { |
|
| 111 | 17 | $test = \DateTime::createFromFormat($format, $date); |
|
| 112 | 17 | if ($test instanceof \DateTime) { |
|
| 113 | 15 | $this->lastGuessedFormat = $format; |
|
| 114 | |||
| 115 | 15 | return $format; |
|
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | 2 | return null; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
| 124 | * @param string $string |
||
| 125 | * @return \DateTime |
||
| 126 | */ |
||
| 127 | 15 | public function convertToDateTime(string $string) : \DateTime |
|
| 128 | { |
||
| 129 | 15 | $string = trim($string); |
|
| 130 | 15 | foreach ([$this->getLastGuessedFormat(), $this->guessDateFormat($string) ] as $format) { |
|
| 131 | 15 | $date = $this->newDate((string) $format, $string); |
|
| 132 | 15 | if ($date instanceof \DateTime) { |
|
| 133 | 14 | $date->setTimezone($this->getTimezone()); |
|
| 134 | |||
| 135 | 14 | return $date; |
|
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | 1 | return $this->stringToDateTime($string); |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Creates a DateTime instance for the given string if the format was not catch from the list |
||
| 144 | * @param string $string |
||
| 145 | * @return \DateTime |
||
| 146 | * @throws InvalidArgumentException |
||
| 147 | */ |
||
| 148 | 2 | public function stringToDateTime(string $string) : \DateTime |
|
| 149 | { |
||
| 150 | 2 | $this->logger->notice("unsupported date format, use strtotime() to build the DateTime instance : {$string}"); |
|
| 151 | |||
| 152 | 2 | if (false === strtotime($string)) { |
|
| 153 | 1 | throw new \InvalidArgumentException('Impossible to convert date : '.$string); |
|
| 154 | } |
||
| 155 | 1 | $date = new \DateTime($string, $this->getFeedTimezone()); |
|
| 156 | 1 | $date->setTimezone($this->getTimezone()); |
|
| 157 | |||
| 158 | 1 | return $date; |
|
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return \DateTimeZone |
||
| 163 | */ |
||
| 164 | 16 | public function getFeedTimezone() : ? \DateTimeZone |
|
| 165 | { |
||
| 166 | 16 | return $this->feedTimezone; |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Specifies the feed's timezone. Do this it the timezone is missing |
||
| 171 | * |
||
| 172 | * @param \DateTimeZone $timezone |
||
| 173 | */ |
||
| 174 | public function setFeedTimezone(\DateTimeZone $timezone) : void |
||
| 175 | { |
||
| 176 | $this->feedTimezone = $timezone; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Resets feedTimezone to null. |
||
| 181 | */ |
||
| 182 | public function resetFeedTimezone() : void |
||
| 183 | { |
||
| 184 | $this->feedTimezone = null; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return \DateTimeZone |
||
| 189 | */ |
||
| 190 | 16 | public function getServerTimezone() : ? \DateTimeZone |
|
| 191 | { |
||
| 192 | 16 | return $this->serverTimezone; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param \DateTimeZone $timezone |
||
| 197 | */ |
||
| 198 | 80 | public function setServerTimezone(\DateTimeZone $timezone) : void |
|
| 199 | { |
||
| 200 | 80 | $this->serverTimezone = $timezone; |
|
| 201 | 80 | } |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return \DateTimeZone |
||
| 205 | */ |
||
| 206 | 16 | public function getTimezone() : ? \DateTimeZone |
|
| 207 | { |
||
| 208 | 16 | return $this->getServerTimezone(); |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param \DateTimeZone $timezone |
||
| 213 | */ |
||
| 214 | 80 | public function setTimezone(\DateTimeZone $timezone) : void |
|
| 215 | { |
||
| 216 | 80 | $this->setServerTimezone($timezone); |
|
| 217 | 80 | } |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param $format |
||
| 221 | * @param $string |
||
| 222 | * @return \DateTime |
||
| 223 | */ |
||
| 224 | 15 | protected function newDate(string $format, string $string) |
|
| 225 | { |
||
| 226 | 15 | if (!! $this->getFeedTimezone()) { |
|
| 227 | return \DateTime::createFromFormat($format, $string, $this->getFeedTimezone()); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 228 | } |
||
| 229 | |||
| 230 | 15 | return \DateTime::createFromFormat($format, $string); |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
The expression
\DateTime::createFromFormat($format, $string); of type DateTime|false adds false to the return on line 230 which is incompatible with the return type documented by FeedIo\Rule\DateTimeBuilder::newDate of type DateTime. It seems like you forgot to handle an error condition.
Loading history...
|
|||
| 231 | } |
||
| 232 | } |
||
| 233 |