-
-
Notifications
You must be signed in to change notification settings - Fork 108
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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'); | ||
} | ||
|
||
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"); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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]); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -49,5 +49,4 @@ public function getUnit() | |
{ | ||
return $this->unit; | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.