* parsing of %*% variables * * @param $filter * @return array|bool */
($filter)
| 208 | * @return array|bool |
| 209 | */ |
| 210 | private function parseFilter($filter) { |
| 211 | preg_match_all("/(?<=%).*(?=%)/", $filter, $matches); |
| 212 | if (count($matches[0]) > 0) { |
| 213 | $filter = $matches[0][0]; |
| 214 | preg_match('/(first|second|third|fourth|last|next|current|to|yester)?/', $filter, $directionMatch); // direction |
| 215 | preg_match('/[0-9]+/', $filter, $offsetMatch); // how much |
| 216 | preg_match('/(day|days|week|weeks|month|months|year|years|quarter|quarters)$/', $filter, $unitMatch); // unit |
| 217 | |
| 218 | if (!$directionMatch[0] || !$unitMatch[0]) { |
| 219 | // no known text variables found |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | // if no offset is specified, apply 1 as default |
| 224 | !isset($offsetMatch[0]) ? $offset = 1 : $offset = $offsetMatch[0]; |
| 225 | |
| 226 | // remove "s" to unify e.g. weeks => week |
| 227 | $unit = rtrim($unitMatch[0], 's'); |
| 228 | $direction = strtolower($directionMatch[0]); |
| 229 | |
| 230 | if ($unit === 'quarter') { |
| 231 | $currentMonth = (int)date('n'); |
| 232 | $currentYear = (int)date('Y'); |
| 233 | $currentQuarter = (int)ceil($currentMonth / 3); |
| 234 | $currentQuarterIndex = (($currentYear - 1) * 4) + $currentQuarter - 1; |
| 235 | |
| 236 | $targetQuarter = $currentQuarter; |
| 237 | $targetYear = $currentYear; |
| 238 | if ($direction === 'last' || $direction === 'yester' || $direction === 'next') { |
| 239 | // Keep the target in the four-digit year range before converting the offset. |
| 240 | $maxOffset = $direction === 'next' ? (9999 * 4) - 1 - $currentQuarterIndex : $currentQuarterIndex; |
| 241 | $offset = ltrim((string)$offset, '0'); |
| 242 | $offset = $offset === '' ? '0' : $offset; |
| 243 | if (strlen($offset) > strlen((string)$maxOffset) |
| 244 | || (strlen($offset) === strlen((string)$maxOffset) && strcmp($offset, (string)$maxOffset) > 0)) { |
| 245 | return false; |
| 246 | } |
| 247 | $targetQuarterIndex = $currentQuarterIndex + ($direction === 'next' ? (int)$offset : -(int)$offset); |
| 248 | $targetYear = intdiv($targetQuarterIndex, 4) + 1; |
| 249 | $targetQuarter = ($targetQuarterIndex % 4) + 1; |
| 250 | } |
| 251 | |
| 252 | switch ($direction) { |
| 253 | case 'first': |
| 254 | $targetQuarter = 1; |
| 255 | break; |
| 256 | case 'second': |
| 257 | $targetQuarter = 2; |
| 258 | break; |
| 259 | case 'third': |
| 260 | $targetQuarter = 3; |
| 261 | break; |
| 262 | case 'fourth': |
| 263 | $targetQuarter = 4; |
| 264 | break; |
| 265 | case 'last': |
| 266 | case 'yester': |
| 267 | case 'next': |
no test coverage detected