@@ -18,15 +18,15 @@ discard block |
||
| 18 | 18 | return $obj; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - private function tryToGetDateObjFromString($obj) { |
|
| 22 | - try { |
|
| 23 | - $date = new \DateTimeImmutable($obj); |
|
| 21 | + private function tryToGetDateObjFromString($obj) { |
|
| 22 | + try { |
|
| 23 | + $date = new \DateTimeImmutable($obj); |
|
| 24 | 24 | if ($this->dateMatchesFormats($date, $obj)) $obj = $date; |
| 25 | - } |
|
| 26 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 27 | - } |
|
| 28 | - return $obj; |
|
| 29 | - } |
|
| 25 | + } |
|
| 26 | + catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 27 | + } |
|
| 28 | + return $obj; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | 31 | private function dateMatchesFormats($date, $str) { |
| 32 | 32 | foreach ($this->dateFormats as list($format, $len)) { |
@@ -35,17 +35,17 @@ discard block |
||
| 35 | 35 | return false; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - private function isIterable($obj) { |
|
| 39 | - return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
| 40 | - } |
|
| 38 | + private function isIterable($obj) { |
|
| 39 | + return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - private function isPossiblyDateString($obj) { |
|
| 43 | - return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
| 44 | - } |
|
| 42 | + private function isPossiblyDateString($obj) { |
|
| 43 | + return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | 46 | private function checkCache($obj, $reset) { |
| 47 | 47 | if ($reset) $this->processCache = new \SplObjectStorage(); |
| 48 | - if (!is_object($obj)) return false; |
|
| 48 | + if (!is_object($obj)) return false; |
|
| 49 | 49 | |
| 50 | 50 | if ($this->processCache->contains($obj)) return $obj; |
| 51 | 51 | else $this->processCache->attach($obj, true); |
@@ -11,26 +11,35 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | public function replaceDates($obj, $reset = true) { |
| 13 | 13 | //prevent infinite recursion, only process each object once |
| 14 | - if ($this->checkCache($obj, $reset)) return $obj; |
|
| 14 | + if ($this->checkCache($obj, $reset)) { |
|
| 15 | + return $obj; |
|
| 16 | + } |
|
| 15 | 17 | |
| 16 | - if ($this->isIterable($obj)) foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
| 17 | - if ($this->isPossiblyDateString($obj)) $obj = $this->tryToGetDateObjFromString($obj); |
|
| 18 | + if ($this->isIterable($obj)) { |
|
| 19 | + foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
| 20 | + } |
|
| 21 | + if ($this->isPossiblyDateString($obj)) { |
|
| 22 | + $obj = $this->tryToGetDateObjFromString($obj); |
|
| 23 | + } |
|
| 18 | 24 | return $obj; |
| 19 | 25 | } |
| 20 | 26 | |
| 21 | 27 | private function tryToGetDateObjFromString($obj) { |
| 22 | 28 | try { |
| 23 | 29 | $date = new \DateTimeImmutable($obj); |
| 24 | - if ($this->dateMatchesFormats($date, $obj)) $obj = $date; |
|
| 25 | - } |
|
| 26 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 30 | + if ($this->dateMatchesFormats($date, $obj)) { |
|
| 31 | + $obj = $date; |
|
| 32 | + } |
|
| 33 | + } catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 27 | 34 | } |
| 28 | 35 | return $obj; |
| 29 | 36 | } |
| 30 | 37 | |
| 31 | 38 | private function dateMatchesFormats($date, $str) { |
| 32 | 39 | foreach ($this->dateFormats as list($format, $len)) { |
| 33 | - if ($date->format($format) == substr($str, 0, $len)) return true; |
|
| 40 | + if ($date->format($format) == substr($str, 0, $len)) { |
|
| 41 | + return true; |
|
| 42 | + } |
|
| 34 | 43 | } |
| 35 | 44 | return false; |
| 36 | 45 | } |
@@ -44,11 +53,18 @@ discard block |
||
| 44 | 53 | } |
| 45 | 54 | |
| 46 | 55 | private function checkCache($obj, $reset) { |
| 47 | - if ($reset) $this->processCache = new \SplObjectStorage(); |
|
| 48 | - if (!is_object($obj)) return false; |
|
| 56 | + if ($reset) { |
|
| 57 | + $this->processCache = new \SplObjectStorage(); |
|
| 58 | + } |
|
| 59 | + if (!is_object($obj)) { |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 49 | 62 | |
| 50 | - if ($this->processCache->contains($obj)) return $obj; |
|
| 51 | - else $this->processCache->attach($obj, true); |
|
| 63 | + if ($this->processCache->contains($obj)) { |
|
| 64 | + return $obj; |
|
| 65 | + } else { |
|
| 66 | + $this->processCache->attach($obj, true); |
|
| 67 | + } |
|
| 52 | 68 | |
| 53 | 69 | return false; |
| 54 | 70 | } |