Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oxford collection + phpcs fixer cleanup. #22

Merged
merged 1 commit into from
Oct 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ use Coduo\PHPHumanizer\Number;
echo Number::metricSuffix(1240000, 'pl'); // "1,24M"
```

## Collections

**Oxford**

```php
use Coduo\PHPHumanizer\Collection;

echo Collection::oxford(['Michal', 'Norbert', 'Lukasz', 'Pawel'], 2); // "Michal, Norbert, and 2 others"
echo Collection::oxford(['Michal', 'Norbert', 'Lukasz'], 2); // "Michal, Norbert, and 1 other"
echo Collection::oxford(['Michal', 'Norbert']); // "Michal and Norbert"
```

Oxford is using translator component, so you can use whatever string format you like.

## Date time

**Difference**
Expand Down
48 changes: 48 additions & 0 deletions spec/Coduo/PHPHumanizer/Collection/FormatterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace spec\Coduo\PHPHumanizer\Collection;
;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\Translation\Translator;

class FormatterSpec extends ObjectBehavior
{
function let(Translator $translator)
{
$this->beConstructedWith($translator);
$translator->trans(
'only_two',
array('%first%' => 'Michal', '%second%' => 'Norbert'),
'oxford'
)->willReturn('Michal and Norbert');

$translator->transChoice(
'comma_separated_with_limit',
1,
array('%count%' => 1, "%list%" => 'Michal, Norbert'),
'oxford'
)->willReturn('Michal, Norbert and 1 more');

$translator->trans(
'comma_separated',
array("%list%" => 'Michal, Norbert', '%last%' => "Lukasz"),
'oxford'
)->willReturn('Michal, Norbert and Lukasz');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But where is the oxford comma?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ur right, but some ppl in different communites always asked for ability to specify if you need comma or not, so the format is defined in translation file. In that case everyone can adjust this to their own locale. Tho maybe I should add the default comma in translation file.

}

function it_formats_two_elements()
{
$this->format(array("Michal", "Norbert"), null)->shouldReturn("Michal and Norbert");
}

function it_formats_elements_with_limit()
{
$this->format(array("Michal", "Norbert", "Lukasz"), 2)->shouldReturn("Michal, Norbert and 1 more");
}

function it_formats_elements_without_limit()
{
$this->format(array("Michal", "Norbert", "Lukasz"), null)->shouldReturn("Michal, Norbert and Lukasz");
}
}
45 changes: 45 additions & 0 deletions spec/Coduo/PHPHumanizer/Collection/OxfordSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace spec\Coduo\PHPHumanizer\Collection;

use Coduo\PHPHumanizer\Collection\Formatter;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\Translation\TranslatorInterface;

class OxfordSpec extends ObjectBehavior
{
private $formatter;

public function let(TranslatorInterface $translator)
{
$this->formatter = new Formatter(
$translator->getWrappedObject()
);
$this->beConstructedWith($this->formatter);
}
function it_returns_empty_string_when_collection_is_empty()
{
$this->format(array())->shouldReturn('');
}

function it_returns_collection_item_string_when_collection_has_one_element()
{
$this->format(array(new CollectionItem("Michal")))->shouldReturn('Michal');
}
}

class CollectionItem
{
private $name;

public function __construct($name)
{
$this->name = $name;
}

public function __toString()
{
return $this->name;
}
}
38 changes: 38 additions & 0 deletions spec/Coduo/PHPHumanizer/CollectionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace spec\Coduo\PHPHumanizer;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class CollectionSpec extends ObjectBehavior
{
function it_humanizes_collections()
{
$examples = array(
array(array("Michal"), null, 'Michal'),
array(array("Michal", "Norbert"), null, 'Michal and Norbert'),
array(array("Michal", "Norbert", "Lukasz"), 2, 'Michal, Norbert, and 1 other'),
array(array("Michal", "Norbert", "Lukasz", "Pawel"), 2, 'Michal, Norbert, and 2 others'),
array(array("Michal", "Norbert", "Lukasz", "Pawel"), null, 'Michal, Norbert, Lukasz, and Pawel'),
);

foreach ($examples as $example) {
$this->oxford($example[0], $example[1])->shouldReturn($example[2]);
}
}

function it_humanizes_collections_for_polish_locale()
{
$examples = array(
array(array("Michal"), null, 'Michal'),
array(array("Michal", "Norbert"), null, 'Michal i Norbert'),
array(array("Michal", "Norbert", "Lukasz"), 2, 'Michal, Norbert i 1 inny'),
array(array("Michal", "Norbert", "Lukasz", "Pawel"), 2, 'Michal, Norbert i 2 innych'),
);

foreach ($examples as $example) {
$this->oxford($example[0], $example[1], 'pl')->shouldReturn($example[2]);
}
}
}
19 changes: 19 additions & 0 deletions src/Coduo/PHPHumanizer/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Coduo\PHPHumanizer;

use Coduo\PHPHumanizer\Collection\Formatter;
use Coduo\PHPHumanizer\Collection\Oxford;
use Coduo\PHPHumanizer\Translator\Builder;

class Collection
{
public static function oxford($collection, $limit = null, $locale = 'en')
{
$oxford = new Oxford(
new Formatter(Builder::build($locale))
);

return $oxford->format($collection, $limit);
}
}
102 changes: 102 additions & 0 deletions src/Coduo/PHPHumanizer/Collection/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Coduo\PHPHumanizer\Collection;

use Symfony\Component\Translation\TranslatorInterface;

class Formatter
{
/**
* @var \Symfony\Component\Translation\TranslatorInterface
*/
private $translator;

/**
* @var string
*/
private $catalogue;

/**
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator, $catalogue = 'oxford')
{
$this->translator = $translator;
$this->catalogue = $catalogue;
}

public function format($collection, $limit = null)
{
$count = count($collection);

if (0 === $count) {
return '';
}

if (1 === $count) {
return (string) $collection[0];
}

if (2 === $count) {
return $this->formatOnlyTwo($collection);
}

if (null !== $limit) {
return $this->formatCommaSeparatedWithLimit($collection, $limit, $count);
}

return $this->formatCommaSeparated($collection, $count);
}

/**
* @param $collection
* @param $limit
* @param $count
*
* @return string
*/
private function formatCommaSeparatedWithLimit($collection, $limit, $count)
{
$display = array_map(function ($element) {
return (string) $element;
}, array_slice($collection, 0, $limit));

$moreCount = $count - count($display);

return $this->translator->transChoice('comma_separated_with_limit', $moreCount, array(
'%list%' => implode(', ', $display),
'%count%' => $moreCount,
), $this->catalogue);
}

/**
* @param $collection
* @param $count
*
* @return string
*/
private function formatCommaSeparated($collection, $count)
{
$display = array_map(function ($element) {
return (string) $element;
}, array_slice($collection, 0, $count - 1));

return $this->translator->trans('comma_separated', array(
'%list%' => implode(', ', $display),
'%last%' => (string) end($collection),
), $this->catalogue);
}

/**
* @param $collection
*
* @return string
*/
private function formatOnlyTwo($collection)
{
return $this->translator->trans('only_two', array(
'%first%' => (string) $collection[0],
'%second%' => (string) $collection[1],
), $this->catalogue);
}
}
21 changes: 21 additions & 0 deletions src/Coduo/PHPHumanizer/Collection/Oxford.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Coduo\PHPHumanizer\Collection;

class Oxford
{
/**
* @var Formatter
*/
private $formatter;

public function __construct(Formatter $formatter)
{
$this->formatter = $formatter;
}

public function format($collection, $limit = null)
{
return $this->formatter->format($collection, $limit);
}
}
2 changes: 2 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class DateTime
public static function difference(\DateTime $fromDate, \DateTime $toDate, $locale = 'en')
{
$formatter = new Formatter(Builder::build($locale));

return $formatter->formatDifference(new Difference($fromDate, $toDate), $locale);
}

public static function preciseDifference(\DateTime $fromDate, \DateTime $toDate, $locale = 'en')
{
$formatter = new PreciseFormatter(Builder::build($locale));

return $formatter->formatDifference(new PreciseDifference($fromDate, $toDate), $locale);
}
}
3 changes: 2 additions & 1 deletion src/Coduo/PHPHumanizer/DateTime/Difference.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function calculate()
new Hour(),
new Minute(),
new Second(),
new JustNow()
new JustNow(),
);

$absoluteMilliSecondsDiff = abs($this->toDate->getTimestamp() - $this->fromDate->getTimestamp()) * 1000;
Expand All @@ -86,6 +86,7 @@ private function calculate()
public function isPast()
{
$diff = $this->toDate->getTimestamp() - $this->fromDate->getTimestamp();

return ($diff > 0) ? false : true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ public function getUnit()
{
return $this->unit;
}

}
3 changes: 2 additions & 1 deletion src/Coduo/PHPHumanizer/DateTime/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function __construct(TranslatorInterface $translator)

/**
* @param Difference $difference
* @param string $locale
* @param string $locale
*
* @return string
*/
public function formatDifference(Difference $difference, $locale = 'en')
Expand Down
4 changes: 1 addition & 3 deletions src/Coduo/PHPHumanizer/DateTime/PreciseDifference.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

use Coduo\PHPHumanizer\DateTime\Unit\Day;
use Coduo\PHPHumanizer\DateTime\Unit\Hour;
use Coduo\PHPHumanizer\DateTime\Unit\JustNow;
use Coduo\PHPHumanizer\DateTime\Unit\Minute;
use Coduo\PHPHumanizer\DateTime\Unit\Month;
use Coduo\PHPHumanizer\DateTime\Unit\Second;
use Coduo\PHPHumanizer\DateTime\Unit\Week;
use Coduo\PHPHumanizer\DateTime\Unit\Year;
use Coduo\PHPHumanizer\DateTime\Difference\CompoundResult;

Expand Down Expand Up @@ -64,7 +62,6 @@ private function calculate()
$diff = $this->fromDate->diff($this->toDate);

foreach ($units as $unit) {

if ($diff->{$unit->getDateIntervalSymbol()} > 0) {
$this->units[] = $unit;
$this->compoundResults[] = new CompoundResult($unit, $diff->{$unit->getDateIntervalSymbol()});
Expand All @@ -75,6 +72,7 @@ private function calculate()
public function isPast()
{
$diff = $this->toDate->getTimestamp() - $this->fromDate->getTimestamp();

return ($diff > 0) ? false : true;
}
}
Loading