Skip to content

Commit

Permalink
Require PHP 5.4 and Run cs fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Feb 21, 2016
1 parent 05c80ad commit a796be8
Show file tree
Hide file tree
Showing 57 changed files with 209 additions and 250 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
@@ -1,8 +1,6 @@
language: php

php:
- 5.3.3
- 5.3
- 5.4
- 5.5.9
- 5.5
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -11,13 +11,13 @@
}
],
"require": {
"php": ">=5.3.3",
"php": ">=5.4",
"nesbot/carbon": "^1.0",
"symfony/translation": "^2.0|^3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0|^5.0",
"satooshi/php-coveralls": "^0.6"
"satooshi/php-coveralls": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
137 changes: 53 additions & 84 deletions src/Date.php
Expand Up @@ -8,8 +8,8 @@
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorInterface;

class Date extends Carbon {

class Date extends Carbon
{
/**
* The Translator implementation.
*
Expand All @@ -33,20 +33,16 @@ class Date extends Carbon {
*/
public function __construct($time = null, $timezone = null)
{
if (is_int($time))
{
if (is_int($time)) {
$timestamp = $time;
$time = null;
}
else
{
} else {
$timestamp = null;
}

parent::__construct($time, $timezone);

if ($timestamp !== null)
{
if ($timestamp !== null) {
$this->setTimestamp($timestamp);
}
}
Expand Down Expand Up @@ -108,22 +104,21 @@ public function diffForHumans(Carbon $since = null, $absolute = false)
// Are we comparing against another date?
$relative = ! is_null($since);

if (is_null($since))
{
if (is_null($since)) {
$since = new static('now', $this->getTimezone());
}

// Are we comparing to a date in the future?
$future = $since->getTimestamp() < $this->getTimestamp();

$units = array(
$units = [
'second' => 60,
'minute' => 60,
'hour' => 24,
'day' => 7,
'week' => 30 / 7,
'month' => 12,
);
];

// Date difference
$difference = abs($since->getTimestamp() - $this->getTimestamp());
Expand All @@ -132,10 +127,8 @@ public function diffForHumans(Carbon $since = null, $absolute = false)
$unit = 'year';

// Select the best unit.
foreach ($units as $key => $value)
{
if ($difference < $value)
{
foreach ($units as $key => $value) {
if ($difference < $value) {
$unit = $key;
break;
}
Expand All @@ -146,37 +139,28 @@ public function diffForHumans(Carbon $since = null, $absolute = false)
$difference = floor($difference);

// Select the suffix.
if ($relative)
{
if ($relative) {
$suffix = $future ? 'after' : 'before';
}
else
{
} else {
$suffix = $future ? 'from_now' : 'ago';
}

// Some languages have different unit translations when used in combination
// with a specific suffix. Here we will check if there is an optional
// translation for that specific suffix and use it if it exists.
if ($lang->trans("${unit}_diff") != "${unit}_diff")
{
$ago = $lang->transChoice("${unit}_diff", $difference, array(':count' => $difference));
}
elseif ($lang->trans("${unit}_${suffix}") != "${unit}_${suffix}")
{
$ago = $lang->transChoice("${unit}_${suffix}", $difference, array(':count' => $difference));
}
else
{
$ago = $lang->transChoice($unit, $difference, array(':count' => $difference));
if ($lang->trans("${unit}_diff") != "${unit}_diff") {
$ago = $lang->transChoice("${unit}_diff", $difference, [':count' => $difference]);
} elseif ($lang->trans("${unit}_${suffix}") != "${unit}_${suffix}") {
$ago = $lang->transChoice("${unit}_${suffix}", $difference, [':count' => $difference]);
} else {
$ago = $lang->transChoice($unit, $difference, [':count' => $difference]);
}

if ($absolute)
{
if ($absolute) {
return $ago;
}

return $lang->transChoice($suffix, $difference, array(':time' => $ago));
return $lang->transChoice($suffix, $difference, [':time' => $ago]);
}

/**
Expand Down Expand Up @@ -210,21 +194,20 @@ public function until($since = null)
*/
public function format($format)
{
$replace = array();
$replace = [];

// Loop all format characters and check if we can translate them.
for ($i = 0; $i < strlen($format); $i++)
{
for ($i = 0; $i < strlen($format); $i++) {
$character = $format[$i];

// Check if we can replace it with a translated version.
if (in_array($character, array('D', 'l', 'F', 'M')))
{
if (in_array($character, ['D', 'l', 'F', 'M'])) {
// Check escaped characters.
if ($i > 0 and $format[$i - 1] == '\\') continue;
if ($i > 0 and $format[$i - 1] == '\\') {
continue;
}

switch ($character)
{
switch ($character) {
case 'D':
$key = parent::format('l');
break;
Expand All @@ -243,31 +226,28 @@ public function format($format)

// For declension support, we need to check if the month is lead by a numeric number.
// If so, we will use the second translation choice if it is available.
if (in_array($character, array('F', 'M')))
{
$choice = (($i - 2) >= 0 and in_array($format[$i - 2], array('d', 'j'))) ? 1 : 0;
if (in_array($character, ['F', 'M'])) {
$choice = (($i - 2) >= 0 and in_array($format[$i - 2], ['d', 'j'])) ? 1 : 0;

$translated = $lang->transChoice(strtolower($key), $choice);
}
else
{
} else {
$translated = $lang->trans(strtolower($key));
}

// Short notations.
if (in_array($character, array('D', 'M')))
{
if (in_array($character, ['D', 'M'])) {
$translated = mb_substr($translated, 0, 3);
}

// Add to replace list.
if ($translated and $original != $translated) $replace[$original] = $translated;
if ($translated and $original != $translated) {
$replace[$original] = $translated;
}
}
}

// Replace translations.
if ($replace)
{
if ($replace) {
return str_replace(array_keys($replace), array_values($replace), parent::format($format));
}

Expand All @@ -287,12 +267,11 @@ public function timespan($time = null, $timezone = null)
$lang = $this->getTranslator();

// Create Date instance if needed
if ( ! $time instanceof Date)
{
if (! $time instanceof self) {
$time = new static($time, $timezone);
}

$units = array('y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second');
$units = ['y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second'];

// Get DateInterval and cast to array
$interval = (array) $this->diff($time);
Expand All @@ -302,12 +281,13 @@ public function timespan($time = null, $timezone = null)
$interval['d'] = $interval['d'] % 7;

// Get ready to build
$str = array();
$str = [];

// Loop all units and build string
foreach ($units as $k => $unit)
{
if ($interval[$k]) $str[] = $lang->transChoice($unit, $interval[$k], array(':count' => $interval[$k]));
foreach ($units as $k => $unit) {
if ($interval[$k]) {
$str[] = $lang->transChoice($unit, $interval[$k], [':count' => $interval[$k]]);
}
}

return implode(', ', $str);
Expand All @@ -321,15 +301,11 @@ public function timespan($time = null, $timezone = null)
*/
public function add($interval)
{
if (is_string($interval))
{
if (is_string($interval)) {
// Check for ISO 8601
if (strtoupper(substr($interval, 0, 1)) == 'P')
{
if (strtoupper(substr($interval, 0, 1)) == 'P') {
$interval = new DateInterval($interval);
}
else
{
} else {
$interval = DateInterval::createFromDateString($interval);
}
}
Expand All @@ -345,15 +321,11 @@ public function add($interval)
*/
public function sub($interval)
{
if (is_string($interval))
{
if (is_string($interval)) {
// Check for ISO 8601
if (strtoupper(substr($interval, 0, 1)) == 'P')
{
if (strtoupper(substr($interval, 0, 1)) == 'P') {
$interval = new DateInterval($interval);
}
else
{
} else {
$interval = DateInterval::createFromDateString($interval);
}
}
Expand Down Expand Up @@ -382,9 +354,9 @@ public static function setLocale($locale)
// Use RFC 5646 for filenames.
$resource = __DIR__ . '/Lang/' . str_replace('_', '-', $locale) . '.php';

if (! file_exists($resource))
{
if (! file_exists($resource)) {
static::setLocale(static::getFallbackLocale());

return;
}

Expand Down Expand Up @@ -425,8 +397,7 @@ public static function getFallbackLocale()
*/
public static function getTranslator()
{
if (static::$translator === null)
{
if (static::$translator === null) {
static::$translator = new Translator('en');
static::$translator->addLoader('array', new ArrayLoader());
static::setLocale('en');
Expand Down Expand Up @@ -454,13 +425,12 @@ public static function setTranslator(TranslatorInterface $translator)
public static function translateTimeString($time)
{
// Don't run translations for english.
if (static::getLocale() == 'en')
{
if (static::getLocale() == 'en') {
return $time;
}

// All the language file items we can translate.
$keys = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
$keys = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];

// Get all the language lines of the current locale.
$all = static::getTranslator()->getCatalogue()->all();
Expand All @@ -470,5 +440,4 @@ public static function translateTimeString($time)
// Replace the translated words with the English words
return str_ireplace($lines, array_keys($lines), $time);
}

}
7 changes: 3 additions & 4 deletions src/DateServiceProvider.php
Expand Up @@ -2,8 +2,8 @@

use Illuminate\Support\ServiceProvider;

class DateServiceProvider extends ServiceProvider {

class DateServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
Expand Down Expand Up @@ -40,7 +40,6 @@ public function register()
*/
public function provides()
{
return array('Date');
return ['Date'];
}

}
4 changes: 2 additions & 2 deletions src/Lang/ar.php
@@ -1,6 +1,6 @@
<?php

return array(
return [

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -45,4 +45,4 @@
'saturday' => 'السبت',
'sunday' => 'الأحد',

);
];
4 changes: 2 additions & 2 deletions src/Lang/az.php
@@ -1,6 +1,6 @@
<?php

return array(
return [

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -45,4 +45,4 @@
'saturday' => 'Şənbə',
'sunday' => 'Bazar',

);
];

0 comments on commit a796be8

Please sign in to comment.