Skip to content

Commit

Permalink
Add TimeSpeller in English
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed Aug 25, 2017
1 parent f494d86 commit e90c315
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 20 deletions.
49 changes: 31 additions & 18 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

* [Pluralization](#pluralization)
* [Numerals](#numerals)
* [Cardinal](#cardinal)
* [Ordinal](#ordinal)
* [Time units and intervals](#time-units-and-intervals)

English morphology shortcuts:

- `morphos\English\pluralize($word, $count = 2)`
- `morphos\English\NounPluralization::pluralize($word, $count)`
- `morphos\English\CardinalNumeralGenerator::generate($number)`
- `morphos\English\OrdinalNumeralGenerator::generate($number)`
- `morphos\English\TimeSpeller::spellInterval(DateInterval $interval)`

English class hierarchy:

Expand All @@ -17,19 +21,20 @@ morphos\
CardinalNumeralGenerator
OrdinalNumeralGenerator
NounPluralization
TimeSpeller
```

## Pluralization (`Plurality`)
Pluralization a word in English:
## Pluralization
Pluralization a word in English is possible with `NounPluralization` class:

```php
use morphos\English\Plurality;
use morphos\English\NounPluralization;

echo '10 '.Plurality::pluralize('foot') => '10 feet'
echo '10 '.NounPluralization::pluralize('foot') => '10 feet'
// or if you don't know count of objects

$n = 1;
echo $n.' '.Plurality::pluralize('foot', $n) => '1 foot'
echo $n.' '.NounPluralization::pluralize('foot', $n) => '1 foot'
```

## Numerals
Expand All @@ -38,37 +43,45 @@ All number creation classes are similar and have one common methods:

- `string generate($number)` - Generates a cardinal numeral for a number.

### Cardinal (`CardinalNumeralGenerator`)
### Cardinal

_Creation of cardinal numerals in english language._
_Creation of cardinal numerals in english language (`CardinalNumeralGenerator`)._


```php
use morphos\English\CardinalNumeralGenerator;
```

Get text representation of a number:

```php
// Get text representation of a number:
$number = 4351;

CardinalNumeralGenerator::generate($number) => 'four thousand, three hundred fifty-one'
```

### Ordinal (`OrdinalNumeralGenerator`)
### Ordinal

_Creation of ordinal numerals in english language._
_Creation of ordinal numerals in english language (`OrdinalNumeralGenerator`)._


```php
use morphos\English\OrdinalNumeralGenerator;
```

Get text representation of a number or short form:

```php
// Get text representation of a number or short form:
$number = 4351;

OrdinalNumeralGenerator::generate($number) => 'four thousand, three hundred fifty-first'
OrdinalNumeralGenerator::generate($number, true) => '4351st'
```

## Time units and intervals

You can generate text representation of time interval with `TimeSpeller` class:

```php
use morphos\English\TimeSpeller;

// generate text representation of 5 years and 2 months
$interval = new DateInterval('P5Y2M');

TimeSpeller::spellInterval($interval) => '5 years 2 months'
TimeSpeller::spellInterval($interval, TimeSpeller::SEPARATE) => '5 years and 2 months'
```
2 changes: 1 addition & 1 deletion README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* [Числительные](#Числительные)
* [Количественные числительные](#Количественные-числительные)
* [Порядковые числительные](#Порядковые-числительные)
* [Окончание глаголов](#Окончание-глаголов)
* [Валюты](#Валюты)
* [Окончание глаголов](#Окончание-глаголов)
* [Временные интервалы](#Временные-интервалы)

Для русского языка доступны следующие классы и функции из пространства имён `morphos\Russian\`:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Tests & Quality: [![Build Status](https://travis-ci.org/wapmorgan/Morphos.svg)](
- [] Declension/Pluralization of nouns and adjectives (Russian, English)
- [] Generation numerals of numbers (количественные и порядковые) (Russian, English)
- [] Spelling out amounts of money (Russian)
- [] Spelling out time units and intervals (Russian)
- [] Spelling out time units and intervals (Russian, English)

## Table of contents

Expand Down
25 changes: 25 additions & 0 deletions src/English/TimeSpeller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace morphos\English;

use InvalidArgumentException;

class TimeSpeller extends \morphos\TimeSpeller
{
protected static $units = [
self::YEAR => 'year',
self::MONTH => 'month',
self::DAY => 'day',
self::HOUR => 'hour',
self::MINUTE => 'minute',
self::SECOND => 'second',
];

public static function spellUnit($count, $unit)
{
if (!isset(self::$units[$unit])) {
throw new InvalidArgumentException('Unknown time unit: '.$unit);
}

return $count.' '.NounPluralization::pluralize(self::$units[$unit], $count);
}
}
52 changes: 52 additions & 0 deletions tests/English/TimeSpellerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace morphos\test\English;

require __DIR__.'/../../vendor/autoload.php';

use DateInterval;
use morphos\English\TimeSpeller;

class TimeSpellerTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider intervalsProvider()
*/
public function testSpellInterval($interval, $options, $result)
{
$this->assertEquals($result, TimeSpeller::spellInterval(new DateInterval($interval), $options));
}

public function intervalsProvider()
{
return
[
['P1Y5M10D', 0, '1 year 5 months 10 days'],
['P10Y1MT5H', 0, '10 years 1 month 5 hours'],
['P3DT1H2M', 0, '3 days 1 hour 2 minutes'],
['P10MT40M30S', 0, '10 months 40 minutes 30 seconds'],
['P1Y5M10D', TimeSpeller::SEPARATE, '1 year, 5 months and 10 days'],
['P10Y1MT5H', TimeSpeller::DIRECTION, '10 years 1 month 5 hours ago'],
['P3DT1H2M', TimeSpeller::SEPARATE | TimeSpeller::DIRECTION, '3 days, 1 hour and 2 minutes ago'],
['P10MT40M30S', TimeSpeller::SEPARATE, '10 months, 40 minutes and 30 seconds'],
];
}

/**
* @dataProvider timeUnitsProvider()
*/
public function testSpellUnit($value, $unit, $result)
{
$this->assertEquals($result, TimeSpeller::spellUnit($value, $unit));
}

public function timeUnitsProvider()
{
return
[
[5, TimeSpeller::YEAR, '5 years'],
[5, TimeSpeller::MONTH, '5 months'],
[5, TimeSpeller::HOUR, '5 hours'],
[5, TimeSpeller::SECOND, '5 seconds'],
];
}
}

0 comments on commit e90c315

Please sign in to comment.