Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e8dc265
Addition of Afrikaans (af) language translation (with spec tests)
sarelvdwalt Oct 29, 2015
e441c40
Addition of contribution markdown file
sarelvdwalt Oct 29, 2015
db117e8
Update CONTRIBUTING.md
sarelvdwalt Oct 29, 2015
7e1071e
Update CONTRIBUTING.md
sarelvdwalt Oct 29, 2015
7e9ee2e
Add Norwegian translation
dagaa Oct 29, 2015
b0759cb
Update CONTRIBUTING.md
sarelvdwalt Oct 29, 2015
5ddd97f
Merge pull request #28 from sarelvdwalt/master
norzechowicz Oct 29, 2015
c697c4c
Added Norwegian locale specs
dagaa Oct 29, 2015
c14d211
Addition of Portuguese (pt) language translation (with spec tests)
lightglitch Oct 29, 2015
e7fad9a
Merge pull request #31 from lightglitch/master
norzechowicz Oct 29, 2015
de617f8
Merge pull request #29 from dagaa/master
norzechowicz Oct 29, 2015
9eee3f5
Oxford collection
defrag Oct 27, 2015
8682764
fix append size
Oct 29, 2015
2e657b5
more tests
Oct 29, 2015
4f58615
Merge pull request #33 from smeeckaert/bugfix/truncate-length-append
norzechowicz Oct 29, 2015
53ebf13
Merge pull request #22 from defrag/feature/oxford
norzechowicz Oct 29, 2015
923751e
Change string functions into multibyte string functions
norberttech Oct 29, 2015
076454a
Added oxford formatter translations for the italian language.
omissis Oct 29, 2015
b630913
Fixed typo.
omissis Oct 29, 2015
b983725
Merge pull request #35 from omissis/oxford-italian-translations
norzechowicz Oct 29, 2015
3dfd14e
Add Dutch Oxford translations.
doenietzomoeilijk Oct 29, 2015
0e75f17
Merge pull request #38 from doenietzomoeilijk/master
norzechowicz Oct 29, 2015
3468e47
Merge pull request #34 from norzechowicz/mb-functions
norzechowicz Oct 30, 2015
bb6b3ec
Corrects a typo
Oct 30, 2015
2bb6c32
Merge pull request #42 from Gnomino/master
norzechowicz Oct 30, 2015
cbe6457
Merge branch 'master' of git://github.com/smeeckaert/php-humanizer in…
norberttech Oct 30, 2015
93a09b7
Reorganize WordTruncate and HtmlTruncate
norberttech Oct 30, 2015
ea62be3
Remove comments
norberttech Oct 30, 2015
ed8569b
bugfix refacto
Oct 30, 2015
d5df1e4
remove default parameters
Nov 2, 2015
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
23 changes: 23 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#A quick guide to contribute to the project:

##Installing the dev environment

1. Fork the repo
2. Clone the repo to local
3. Install dependencies: `composer install` (this assumes you have 'composer' aliased to whereever your composer.phar lives)
4. Run the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate:
`./bin/phpspec run --format=pretty`

##The actual contribution

1. Make the changes/additions to the code, committing often and making clear what you've done
2. Make sure you write tests for your code, located in the folder structure `spec/Coduo/PHPHumanizer/...`
3. Run your tests (often and while coding): `./bin/phpspec run --format=pretty`

##Coding Standards

Try use similar coding standards to what you see in the project to keep things clear to the contributors. If you're unsure, it's always a safe bet to fall-back to the PSR standards.

[PSR-1: Basic Coding Standard](http://www.php-fig.org/psr/psr-1/)

[PSR-2: Coding Style Guide](http://www.php-fig.org/psr/psr-2/)
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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');
}

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;
}
}
53 changes: 53 additions & 0 deletions spec/Coduo/PHPHumanizer/CollectionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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]);
}
}

function it_humanizes_collections_for_dutch_locale()
{
$examples = array(
array(array("Michal"), null, 'Michal'),
array(array("Michal", "Norbert"), null, 'Michal en Norbert'),
array(array("Michal", "Norbert", "Lukasz"), 2, 'Michal, Norbert, en 1 andere'),
array(array("Michal", "Norbert", "Lukasz", "Pawel"), 2, 'Michal, Norbert, en 2 andere'),
array(array("Michal", "Norbert", "Lukasz", "Pawel"), null, 'Michal, Norbert, Lukasz, en Pawel'),
);

foreach ($examples as $example) {
$this->oxford($example[0], $example[1], 'nl')->shouldReturn($example[2]);
}
}
}
110 changes: 110 additions & 0 deletions spec/Coduo/PHPHumanizer/DateTimeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,63 @@ function it_humanize_difference_between_dates_for_pl_locale()
}
}

function it_humanize_difference_between_dates_for_af_locale()
{
$examples = array(
array("2014-04-26 13:00:00", "2014-04-26 13:00:00", 'nou nou'),
array("2014-04-26 13:00:00", "2014-04-26 13:00:05", '5 sekondes van nou af'),
array("2014-04-26 13:00:00", "2014-04-26 12:59:00", '1 minuut gelede'),
array("2014-04-26 13:00:00", "2014-04-26 12:45:00", '15 minute gelede'),
array("2014-04-26 13:00:00", "2014-04-26 13:15:00", '15 minute van nou af'),
array("2014-04-26 13:00:00", "2014-04-26 14:00:00", '1 uur van nou af'),
array("2014-04-26 13:00:00", "2014-04-26 15:00:00", '2 ure van nou af'),
array("2014-04-26 13:00:00", "2014-04-26 12:00:00", '1 uur gelede'),
array("2014-04-26 13:00:00", "2014-04-26 11:00:00", '2 ure gelede'),
array("2014-04-26 13:00:00", "2014-04-26 12:00:00", '1 uur gelede'),
array("2014-04-26", "2014-04-25", '1 dag gelede'),
array("2014-04-26", "2014-04-24", '2 dae gelede'),
array("2014-04-26", "2014-04-28", '2 dae van nou af'),
array("2014-04-01", "2014-04-15", '2 weke van nou af'),
array("2014-04-15", "2014-04-07", '1 week gelede'),
array("2014-01-01", "2014-04-01", '3 maande van nou af'),
array("2014-05-01", "2014-04-01", '1 maand gelede'),
array("2015-05-01", "2014-04-01", '1 jaar gelede'),
array("2014-05-01", "2016-04-01", '2 jaar van nou af'),
array("2014-05-01", "2009-04-01", '5 jaar gelede'),
);

foreach ($examples as $example) {
$this->difference(new \DateTime($example[0]), new \DateTime($example[1]), 'af')->shouldReturn($example[2]);
}
}

function it_humanize_difference_between_dates_for_pt_locale()
{
$examples = array(
array("2014-04-26 13:00:00", "2014-04-26 13:00:00", 'agora'),
array("2014-04-26 13:00:00", "2014-04-26 13:00:05", '5 segundos a partir de agora'),
array("2014-04-26 13:00:00", "2014-04-26 12:59:00", '1 minuto atrás'),
array("2014-04-26 13:00:00", "2014-04-26 12:45:00", '15 minutos atrás'),
array("2014-04-26 13:00:00", "2014-04-26 13:15:00", '15 minutos a partir de agora'),
array("2014-04-26 13:00:00", "2014-04-26 14:00:00", '1 hora a partir de agora'),
array("2014-04-26 13:00:00", "2014-04-26 15:00:00", '2 horas a partir de agora'),
array("2014-04-26 13:00:00", "2014-04-26 12:00:00", '1 hora atrás'),
array("2014-04-26", "2014-04-25", '1 dia atrás'),
array("2014-04-26", "2014-04-24", '2 dias atrás'),
array("2014-04-26", "2014-04-28", '2 dias a partir de agora'),
array("2014-04-01", "2014-04-15", '2 semanas a partir de agora'),
array("2014-04-15", "2014-04-07", '1 semana atrás'),
array("2014-01-01", "2014-04-01", '3 meses a partir de agora'),
array("2014-05-01", "2014-04-01", '1 mês atrás'),
array("2015-05-01", "2014-04-01", '1 ano atrás'),
array("2014-05-01", "2016-04-01", '2 anos a partir de agora'),
);

foreach ($examples as $example) {
$this->difference(new \DateTime($example[0]), new \DateTime($example[1]), 'pt')->shouldReturn($example[2]);
}
}

function it_humanizes_precise_difference_between_dates()
{
$examples = array(
Expand Down Expand Up @@ -100,6 +157,24 @@ function it_humanizes_precise_difference_between_dates_for_pl_locale()
}
}

function it_humanizes_precise_difference_between_dates_for_af_locale()
{
$examples = array(
array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minuut, 45 sekondes gelede'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 uur, 40 minute gelede'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 dag, 15 minute van nou af'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 dae, 2 ure van nou af'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 jaar, 2 dae, 4 ure van nou af'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 dae, 10 ure van nou af'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 dag, 1 uur, 40 minute gelede'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 jaar, 1 dag van nou af'),
);

foreach ($examples as $example) {
$this->preciseDifference(new \DateTime($example[0]), new \DateTime($example[1]), 'af')->shouldReturn($example[2]);
}
}

function it_humanizes_precise_difference_between_dates_for_de_locale()
{
$examples = array(
Expand Down Expand Up @@ -154,6 +229,24 @@ function it_humanizes_precise_difference_between_dates_for_fr_locale()
}
}

function it_humanizes_precise_difference_between_dates_for_pt_locale()
{
$examples = array(
array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minuto, 45 segundos atrás'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 hora, 40 minutos atrás'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 dia, 15 minutos a partir de agora'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 dias, 2 horas a partir de agora'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 ano, 2 dias, 4 horas a partir de agora'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 dias, 10 horas a partir de agora'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 dia, 1 hora, 40 minutos atrás'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 anos, 1 dia a partir de agora'),
);

foreach ($examples as $example) {
$this->preciseDifference(new \DateTime($example[0]), new \DateTime($example[1]), 'pt_BR')->shouldReturn($example[2]);
}
}

function it_humanizes_precise_difference_between_dates_for_pt_BR_locale()
{
$examples = array(
Expand Down Expand Up @@ -189,4 +282,21 @@ function it_humanizes_precise_difference_between_dates_for_it_locale()
$this->preciseDifference(new \DateTime($example[0]), new \DateTime($example[1]), 'it')->shouldReturn($example[2]);
}
}

function it_humanizes_precise_difference_between_dates_for_no_locale()
{
$examples = array(
array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minutt, 45 sekunder siden'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 time, 40 minutter siden'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 dag, 15 minutter fra nå'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 dager, 2 timer fra nå'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 år, 2 dager, 4 timer fra nå'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 dager, 10 timer fra nå'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 dag, 1 time, 40 minutter siden'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 år, 1 dag fra nå'),
);
foreach ($examples as $example) {
$this->preciseDifference(new \DateTime($example[0]), new \DateTime($example[1]), 'no')->shouldReturn($example[2]);
}
}
}
31 changes: 21 additions & 10 deletions spec/Coduo/PHPHumanizer/String/WordBreakpointSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@
class WordBreakpointSpec extends ObjectBehavior
{

function it_breakpoint()
function it_calculate_breakpoint_position_when_sentence_is_longer_than_characters_count()
{
$text = 'Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.';

$this->calculatePosition($text, 2)->shouldReturn(5);
$this->calculatePosition($text, 4)->shouldReturn(5);
$this->calculatePosition($text, 5)->shouldReturn(5);
$this->calculatePosition($text, 10)->shouldReturn(11);
$this->calculatePosition($text, 20)->shouldReturn(22);
$this->calculatePosition($text, -2)->shouldReturn(-2);
$this->calculatePosition($text, 0)->shouldReturn(5);
$this->calculatePosition('Lorem ipsum dolorem', 2)->shouldReturn(5);
$this->calculatePosition('Lorem ipsum dolorem', 4)->shouldReturn(5);
$this->calculatePosition('Lorem ipsum dolorem', 5)->shouldReturn(5);
$this->calculatePosition('Lorem ipsum dolorem', 10)->shouldReturn(11);
$this->calculatePosition('Lorem ipsum dolorem', -2)->shouldReturn(19);
$this->calculatePosition('Lorem ipsum dolorem', 0)->shouldReturn(5);
}

function it_calculate_breakpoint_position_when_sentence_is_shorter_than_characters_count()
{
$this->calculatePosition('Lorem ipsum dolorem', 20)->shouldReturn(19);
}

function it_calculate_breakpoint_position_when_characters_count_ends_in_last_word()
{
$this->calculatePosition('Lorem ipsum', 7)->shouldReturn(11);
}

function it_calculate_breakpoint_position_when_characters_count_ends_in_last_space()
{
$this->calculatePosition('Lorem ipsum', 5)->shouldReturn(5);
}
}
Loading