/** * @inheritdoc */ public function transform($value) { if (is_null($value) || empty($value)) { return null; } if ($value instanceof \DateTime) { return $value; } // check if strtotime matches if (false !== strtotime($value)) { return new \DateTime($value, $this->timezone); } // last resort $transformer = new FallbackTransformer(); return $transformer->transform($value); }
/** * @inheritdoc */ public function transform($value) { if (is_null($value) || empty($value)) { return null; } if ($value instanceof \DateTime) { return $value; } // "per direct" => now if (preg_match('/(per )?dire(c|k)t/i', $value) || preg_match('/(gelijk|heden)/i', $value)) { return new \DateTime('now', $this->timezone); } // [sinds] 2 [mnd|maand|maanden] if (preg_match('/^(sinds\\s)?(?P<months>\\d+)\\s(maand|maanden|mnd)\\s?\\+?/i', $value, $matches)) { return new \DateTime(sprintf('-%s months', $matches['months']), $this->timezone); } // [12] oktober 2012|'12 $regex = '/^(?P<day>\\d*\\s?)?(?P<month>' . $this->monthRegex . ')\\s+(?P<year>\\d{4}|\\\'\\d{2})/i'; if (preg_match($regex, $value, $matches)) { $year = $matches['year']; $month = array_search(mb_strtolower($matches['month']), $this->monthNames); $day = isset($matches['day']) && $matches['day'] !== '' ? $matches['day'] : 1; return $this->createDate($year, $month, $day); } // 12 oktober [2012|'12] $regex = '/^(?P<day>\\d{1,2})\\s?(?P<month>' . $this->monthRegex . ')(\\s+(?P<year>\\d{4}|\\\'\\d{2}))?/i'; if (preg_match($regex, $value, $matches)) { $year = isset($matches['year']) ? $matches['year'] : date('Y'); $month = array_search(mb_strtolower($matches['month']), $this->monthNames); $day = $matches['day']; return $this->createDate($year, $month, $day); } // oktober [12|2012] $regex = '/^(?P<month>' . $this->monthRegex . ')(\\s+(?P<day_or_year>\\d{4}|\\d{1,2}))?/i'; if (preg_match($regex, $value, $matches)) { $month = array_search(mb_strtolower($matches['month']), $this->monthNames); $year = date('Y'); $day = 1; if (isset($matches['day_or_year'])) { if (strlen($matches['day_or_year']) === 2) { $day = $matches['day_or_year']; } elseif (strlen($matches['day_or_year']) === 4) { $year = $matches['day_or_year']; } } return $this->createDate($year, $month, $day); } // [begin|eind] mei [2013] $regex = '/^(?P<day>begin|eind)?\\s?(?P<month>' . $this->monthRegex . ')(?P<year>\\s+\\d{4})?/i'; if (preg_match($regex, $value, $matches)) { $year = isset($matches['year']) ? $matches['year'] : date('Y'); $month = array_search(mb_strtolower($matches['month']), $this->monthNames); if (isset($matches['day'])) { $day = $matches['day'] === 'begin' ? 5 : 25; } else { $day = 1; } return $this->createDate($year, $month, $day); } $regexes = ['/^(?P<day>\\d{1,2})[\\-\\/](?P<month>\\d{1,2})[\\-\\/](?P<year>\\d{4}|\\\'?\\d{2})/i' => [null, null, null], '/^(?P<year>\\d{4})[\\-\\/](?P<month>\\d{1,2})[\\-\\/](?P<day>\\d{1,2})/i' => [null, null, null], '/^(?P<year>\\d{4})[\\-\\/](?P<month>\\d{1,2})/i' => [null, null, 1], '/^(?P<year>\\d{4})/i' => [null, 1, 1]]; foreach ($regexes as $regex => $defaults) { if (preg_match($regex, $value, $matches)) { list($defaultYear, $defaultMonth, $defaultDay) = $defaults; $year = isset($matches['year']) ? $matches['year'] : $defaultYear; $month = isset($matches['month']) ? $matches['month'] : $defaultMonth; $day = isset($matches['day']) ? $matches['day'] : $defaultDay; return $this->createDate($year, $month, $day); } } // check if strtotime matches if (false !== strtotime($value)) { return new \DateTime($value, $this->timezone); } // 26/03/2013 if (preg_match('/^(?P<day>\\d{1,2})\\/(?P<month>\\d{1,2})\\/(?<year>\\d{4})/i', $value, $matches)) { return $this->createDate($matches['year'], $matches['month'], $matches['day']); } // last resort $transformer = new FallbackTransformer('d-m-Y H:i:s', null, $this->timezone->getName()); return $transformer->transform($value); }