Exemple #1
0
 /**
  * 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'];
 }
Exemple #2
0
 public function testContains()
 {
     $haystack = 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque';
     $needle = 'unde';
     $result = UString::contains($haystack, $needle);
     $this->assertTrue($result);
     $haystack = 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque';
     $needle = 'oMnis';
     $result = UString::contains($haystack, $needle);
     $this->assertTrue($result);
     $haystack = 'Sed ut perspiciatis unde omnis iste natus Error sit voluptatem accusantium doloremque';
     $needle = 'oMnis';
     $result = UString::contains($haystack, $needle, true);
     $this->assertFalse($result);
     $haystack = 'Sed ut perspiciatis unde omnis iste natus Error sit voluptatem accusantium doloremque';
     $needle = 'Error';
     $result = UString::contains($haystack, $needle, true);
     $this->assertTrue($result);
     $haystack = 'Sed ut perspiciatis unde omnis iste natus Error sit voluptatem accusantium doloremque';
     $needle = 'error';
     $result = UString::contains($haystack, $needle, true);
     $this->assertFalse($result);
 }