public function testPlural() { try { UString::plural(12, ['страница', 'страниц']); $this->fail('Expected exception not thrown'); } catch (\InvalidArgumentException $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e); $this->assertEquals('Param $forms must contains three words.', $e->getMessage()); } $result = UString::plural(5, ['машина', 'машины', 'машин']); $expected = 'машин'; $this->assertEquals($expected, $result); $result = UString::plural(2, ['машина', 'машины', 'машин']); $expected = 'машины'; $this->assertEquals($expected, $result); $result = UString::plural(1, ['машина', 'машины', 'машин'], true); $expected = '1 машина'; $this->assertEquals($expected, $result); $result = UString::plural(34, ['содат', 'солдата', 'солдат']); $expected = 'солдата'; $this->assertEquals($expected, $result); $result = UString::plural(178, ['содат', 'солдата', 'солдат'], true); $expected = '178 солдат'; $this->assertEquals($expected, $result); }
/** * Get time diff in human-readable format. * * @param mixed $periodStartDate * @param mixed $periodEndDate * * @return string * * @throws \InvalidArgumentException * @throws \RuntimeException */ public static function timeDiff($periodStartDate, $periodEndDate) { /** * @var \DateTime $fromDate * @var \DateTime $toDate */ // @codeCoverageIgnoreStart $dateRange = new DateRange($periodStartDate, $periodEndDate); $fromDate = $dateRange->getRangeBegin(); $toDate = $dateRange->getRangeEnd(); // @codeCoverageIgnoreEnd $diff = $fromDate->diff($toDate); // @codeCoverageIgnoreStart if ($diff === false) { throw new \RuntimeException('Unexpected runtime error.'); } // @codeCoverageIgnoreEnd if ($diff->invert === 1) { throw new \InvalidArgumentException('$periodStartDate argument can not be greater than $periodEndDate argument.'); } $translations = static::loadTranslations(__FUNCTION__); if ($diff->y >= 1) { $text = UString::plural($diff->y, $translations['year'], true); } elseif ($diff->m >= 1) { $text = UString::plural($diff->m, $translations['month'], true); } elseif ($diff->d >= 7) { $text = UString::plural(ceil($diff->d / 7), $translations['week'], true); } elseif ($diff->d >= 1) { $text = UString::plural($diff->d, $translations['day'], true); } elseif ($diff->h >= 1) { $text = UString::plural($diff->h, $translations['hour'], true); } elseif ($diff->i >= 1) { $text = UString::plural($diff->i, $translations['minute'], true); } elseif ($diff->s >= 1) { $text = UString::plural($diff->s, $translations['second'], true); } else { $text = UString::plural(0, $translations['second'], true); } return trim($text) . ' ' . $translations['ago']; }