Пример #1
0
 public function format(Period $period)
 {
     // this method actually has a pretty bad smell on it.
     // it does what it's supposed to, but at one point or another it should be
     // refactored to use a combination of
     // the strategy and chain of responsibility-pattern
     // so the variants can be customized
     if ($period->getYears() != 0) {
         return $this->doFormat($this->lang, 'years', $period->getYears());
     }
     if ($period->getMonths() != 0) {
         return $this->doFormat($this->lang, 'months', $period->getMonths());
     }
     if ($period->getDays() != 0) {
         $amount = $period->getDays();
         // if the amount of days is larger than 6 we'll want to
         // express it in weeks instead
         if ($amount > 6 || $amount < -6) {
             $amount = intval($amount / 7);
             return $this->doFormat($this->lang, 'weeks', $amount);
         } else {
             return $this->doFormat($this->lang, 'days', $amount);
         }
     }
     if ($period->getHours() != 0) {
         return $this->doFormat($this->lang, 'hours', $period->getHours());
     }
     if ($period->getMinutes() != 0) {
         return $this->doFormat($this->lang, 'minutes', $period->getMinutes());
     }
     return $this->doFormat($this->lang, 'justNow', 0);
 }