-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
namespace morphos\Russian; | ||
|
||
use InvalidArgumentException; | ||
|
||
class TimeUnitSpeller extends \morphos\TimeUnitSpeller | ||
{ | ||
protected static $units = [ | ||
self::YEAR => 'год', | ||
self::MONTH => 'месяц', | ||
self::DAY => 'день', | ||
self::HOUR => 'час', | ||
self::MINUTE => 'минута', | ||
self::SECOND => 'секунда', | ||
]; | ||
|
||
const AGO = 'назад'; | ||
const IN = 'через'; | ||
|
||
const AND = 'и'; | ||
|
||
const JUST_NOW = 'только что'; | ||
|
||
public static function spellUnit($count, $unit) | ||
{ | ||
if (!isset(self::$units[$unit])) | ||
throw new InvalidArgumentException('Unknown time unit: '.$unit); | ||
|
||
// special case for YEAR >= 5 | ||
if ($unit == self::YEAR && Plurality::getNumeralForm($count) == Plurality::FIVE_OTHER) | ||
return $count.' лет'; | ||
|
||
return $count.' '.pluralize(self::$units[$unit], $count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
namespace morphos; | ||
|
||
use DateInterval; | ||
|
||
abstract class TimeUnitSpeller | ||
{ | ||
const YEAR = 'year'; | ||
const MONTH = 'month'; | ||
const DAY = 'day'; | ||
const HOUR = 'hour'; | ||
const MINUTE = 'minute'; | ||
const SECOND = 'second'; | ||
|
||
const AGO = 'ago'; | ||
const IN = 'in'; | ||
|
||
const AND = 'and'; | ||
|
||
const JUST_NOW = 'just now'; | ||
|
||
const DIRECTION = 1; | ||
const SEPARATE = 2; | ||
|
||
abstract public static function spellUnit($count, $unit); | ||
|
||
public static function spellInterval(DateInterval $interval, $options = 0) | ||
{ | ||
$parts = []; | ||
foreach ([ | ||
'y' => self::YEAR, | ||
'm' => self::MONTH, | ||
'd' => self::DAY, | ||
'h' => self::HOUR, | ||
'i' => self::MINUTE, | ||
's' => self::SECOND | ||
] as $interval_field => $unit) { | ||
if ($interval->{$interval_field} > 0) | ||
$parts[] = static::spellUnit($interval->{$interval_field}, $unit); | ||
} | ||
|
||
if (empty($parts)) | ||
return static::JUST_NOW; | ||
|
||
if ($options & self::SEPARATE && count($parts) > 1) { | ||
$last_part = array_pop($parts); | ||
$spelled = implode(', ', $parts).' '.static::AND.' '.$last_part; | ||
} else | ||
$spelled = implode(' ', $parts); | ||
|
||
if ($options & self::DIRECTION) { | ||
if ($interval->invert) | ||
$spelled = static::IN.' '.$spelled; | ||
else | ||
$spelled .= ' '.static::AGO; | ||
} | ||
|
||
return $spelled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace morphos\test\Russian; | ||
require __DIR__.'/../../vendor/autoload.php'; | ||
|
||
use DateInterval; | ||
use morphos\Russian\TimeUnitSpeller; | ||
|
||
class TimeUnitSpellerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider intervalsProvider() | ||
*/ | ||
public function testSpellInterval($interval, $options, $result) | ||
{ | ||
$this->assertEquals($result, TimeUnitSpeller::spellInterval(new DateInterval($interval), $options)); | ||
} | ||
|
||
public function intervalsProvider() | ||
{ | ||
return | ||
[ | ||
['P1Y5M10D', 0, '1 год 5 месяцев 10 дней'], | ||
['P10Y1MT5H', 0, '10 лет 1 месяц 5 часов'], | ||
['P3DT1H2M', 0, '3 дня 1 час 2 минуты'], | ||
['P10MT40M30S', 0, '10 месяцев 40 минут 30 секунд'], | ||
['P1Y5M10D', TimeUnitSpeller::SEPARATE, '1 год, 5 месяцев и 10 дней'], | ||
['P10Y1MT5H', TimeUnitSpeller::DIRECTION, '10 лет 1 месяц 5 часов назад'], | ||
['P3DT1H2M', TimeUnitSpeller::SEPARATE | TimeUnitSpeller::DIRECTION, '3 дня, 1 час и 2 минуты назад'], | ||
['P10MT40M30S', TimeUnitSpeller::SEPARATE, '10 месяцев, 40 минут и 30 секунд'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider timeUnitsProvider() | ||
*/ | ||
public function testSpellUnit($value, $unit, $result) | ||
{ | ||
$this->assertEquals($result, TimeUnitSpeller::spellUnit($value, $unit)); | ||
} | ||
|
||
public function timeUnitsProvider() | ||
{ | ||
return | ||
[ | ||
[5, TimeUnitSpeller::YEAR, '5 лет'], | ||
[5, TimeUnitSpeller::MONTH, '5 месяцев'], | ||
[5, TimeUnitSpeller::HOUR, '5 часов'], | ||
[5, TimeUnitSpeller::SECOND, '5 секунд'], | ||
]; | ||
} | ||
} |