MomentJs.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPressPopularPosts\Moment\CustomFormats; |
| 4 | |
| 5 | use Moment\FormatsInterface; |
| 6 | |
| 7 | /** |
| 8 | * Class MomentJs |
| 9 | * @package Moment\CustomFormats |
| 10 | */ |
| 11 | class MomentJs implements FormatsInterface |
| 12 | { |
| 13 | /** |
| 14 | * @var array |
| 15 | */ |
| 16 | protected $tokens = array( |
| 17 | "M" => "n", // 1 2 ... 11 12 |
| 18 | "Mo" => "nS", // month: 1st 2nd ... 11th 12th |
| 19 | "MM" => "m", // 01 02 ... 11 12 |
| 20 | "MMM" => "M", // Jan Feb ... Nov Dec |
| 21 | "MMMM" => "F", // January February ... November December |
| 22 | "D" => "j", // 1 2 ... 30 30 |
| 23 | "Do" => "jS", // day: 1st 2nd ... 30th 31st |
| 24 | "DD" => "d", // 01 02 ... 30 31 |
| 25 | "DDD" => "z", // 1 2 ... 364 365 |
| 26 | "DDDo" => "zS", // day of year: 1st 2nd ... 364th 365th |
| 27 | "DDDD" => "zS", // day of year: 1st 2nd ... 364th 365th |
| 28 | "d" => "w", // 0 1 ... 5 6 |
| 29 | "do" => "wS", // day of week: 0th 1st ... 5th 6th |
| 30 | "dd" => "D", // ***Su Mo ... Fr Sa |
| 31 | "ddd" => "D", // Sun Mon ... Fri Sat |
| 32 | "dddd" => "l", // Sunday Monday ... Friday Saturday |
| 33 | "e" => "w", // 0 1 ... 5 6 |
| 34 | "E" => "N", // 1 2 ... 6 7 |
| 35 | "w" => "W", // 1 2 ... 52 53 |
| 36 | "wo" => "WS", // week of year: 1st 2nd ... 52nd 53rd |
| 37 | "ww" => "W", // ***01 02 ... 52 53 |
| 38 | "W" => "W", // 1 2 ... 52 53 |
| 39 | "Wo" => "WS", // week of year: 1st 2nd ... 52nd 53rd |
| 40 | "WW" => "W", // ***01 02 ... 52 53 |
| 41 | "YY" => "y", // 70 71 ... 29 30 |
| 42 | "YYYY" => "Y", // 1970 1971 ... 2029 2030 |
| 43 | "gg" => "o", // 70 71 ... 29 30 |
| 44 | "gggg" => "o", // ***1970 1971 ... 2029 2030 |
| 45 | "GG" => "o", // 70 71 ... 29 30 |
| 46 | "GGGG" => "o", // ***1970 1971 ... 2029 2030 |
| 47 | "A" => "A", // AM PM |
| 48 | "a" => "a", // am pm |
| 49 | "H" => "G", // 0 1 ... 22 23 |
| 50 | "HH" => "H", // 00 01 ... 22 23 |
| 51 | "h" => "g", // 1 2 ... 11 12 |
| 52 | "hh" => "h", // 01 02 ... 11 12 |
| 53 | "m" => "i", // 0 1 ... 58 59 |
| 54 | "mm" => "i", // ***00 01 ... 58 59 |
| 55 | "s" => "s", // 0 1 ... 58 59 |
| 56 | "ss" => "s", // ***00 01 ... 58 59 |
| 57 | "S" => "", // 0 1 ... 8 9 |
| 58 | "SS" => "", // 0 1 ... 98 99 |
| 59 | "SSS" => "", // 0 1 ... 998 999 |
| 60 | "z or zz" => "T", // EST CST ... MST PST |
| 61 | "Z" => "P", // -07:00 -06:00 ... +06:00 +07:00 |
| 62 | "ZZ" => "O", // -0700 -0600 ... +0600 +0700 |
| 63 | "X" => "U", // 1360013296 |
| 64 | "LTS" => "g:i:s A", // 8:30:15 PM |
| 65 | "LT" => "g:i A", // 8:30 PM |
| 66 | "L" => "m/d/Y", // 09/04/1986 |
| 67 | "l" => "n/j/Y", // 9/4/1986 |
| 68 | "LL" => "F jS Y", // September 4th 1986 |
| 69 | "ll" => "M j Y", // Sep 4 1986 |
| 70 | "LLL" => "F jS Y g:i A", // September 4th 1986 8:30 PM |
| 71 | "lll" => "M j Y g:i A", // Sep 4 1986 8:30 PM |
| 72 | "LLLL" => "l, F jS Y g:i A", // Thursday, September 4th 1986 8:30 PM |
| 73 | "llll" => "D, M j Y g:i A", // Thu, Sep 4 1986 8:30 PM |
| 74 | ); |
| 75 | |
| 76 | /** |
| 77 | * @param $format |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | public function format($format) |
| 82 | { |
| 83 | return $this->momentJsToPhp($format); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return array |
| 88 | */ |
| 89 | public function getTokens() |
| 90 | { |
| 91 | return (array)$this->tokens; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param array $options |
| 96 | * |
| 97 | * @return MomentJs |
| 98 | */ |
| 99 | public function setTokens(array $options) |
| 100 | { |
| 101 | $this->tokens = array_merge($this->tokens, $options); |
| 102 | |
| 103 | return $this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param String $format |
| 108 | * |
| 109 | * @return string |
| 110 | */ |
| 111 | protected function momentJsToPhp($format) |
| 112 | { |
| 113 | $tokens = $this->getTokens(); |
| 114 | |
| 115 | // find all tokens from string, using regular expression |
| 116 | $regExp = "/(\[[^\[]*\])|(\\\\)?(LTS?|LL?L?L?|l{1,4}|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|.)/"; |
| 117 | $matches = array(); |
| 118 | preg_match_all($regExp, $format, $matches); |
| 119 | |
| 120 | // if there is no match found then return the string as it is |
| 121 | // TODO: might return escaped string |
| 122 | if (empty($matches) || is_array($matches) === false) |
| 123 | { |
| 124 | return $format; |
| 125 | } |
| 126 | |
| 127 | // to match with extracted tokens |
| 128 | $momentTokens = array_keys($tokens); |
| 129 | $phpMatches = array(); |
| 130 | |
| 131 | // ---------------------------------- |
| 132 | |
| 133 | foreach ($matches[0] as $id => $match) |
| 134 | { |
| 135 | // if there is a matching php token in token list |
| 136 | if (in_array($match, $momentTokens)) |
| 137 | { |
| 138 | // use the php token instead |
| 139 | $string = $tokens[$match]; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | $string = $match; |
| 144 | } |
| 145 | |
| 146 | $phpMatches[$id] = $string; |
| 147 | } |
| 148 | |
| 149 | // join and return php specific tokens |
| 150 | return implode("", $phpMatches); |
| 151 | } |
| 152 | } |
| 153 |