Example #1
0
 /**
  * @param null $format
  * @param null|FormatsInterface $formatsInterface
  *
  * @return string
  */
 public function format($format = null, $formatsInterface = null)
 {
     // set default format
     if ($format === null) {
         $format = \DateTime::ISO8601;
     }
     // handle diverse format types
     if ($formatsInterface instanceof FormatsInterface) {
         $format = $formatsInterface->format($format);
     }
     // handle ordinals
     if (strpos($format, 'S') !== false) {
         preg_match_all('/(\\wS)/', $format, $matches);
         if (count($matches) >= 1) {
             foreach ($matches[1] as $part) {
                 $token = substr($part, 0, 1);
                 $number = $this->format($token);
                 $format = str_replace($part, $this->formatOrdinal($number, $token), $format);
             }
         }
     }
     // handle text
     if (strpos($format, '[') !== false) {
         preg_match_all('/\\[([^\\[]*)\\]/', $format, $matches);
         foreach ($matches[1] as $part) {
             $format = preg_replace('/\\[' . $part . '\\]/u', preg_replace('/(\\w)/u', '\\\\\\1', $part), $format);
         }
     }
     // prepare locale formats
     $format = MomentLocale::prepareSpecialLocaleTags($format);
     // render moment
     $format = parent::format($format);
     // render locale format
     $format = MomentLocale::renderSpecialLocaleTags($format);
     return $format;
 }