Skip to content

Commit 31065f9

Browse files
authored
Added integration with aeon-php/calendar library (coduo#123)
1 parent d6addbb commit 31065f9

File tree

14 files changed

+463
-88
lines changed

14 files changed

+463
-88
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \
236236
DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2016-04-27 13:00:00")); // 2 years, 1 day from now
237237
```
238238

239+
## Aeon Calendar
240+
241+
[Aeon PHP](https://aeon-php.org/) is a date&time oriented set of libraries.
242+
243+
```php
244+
use Coduo\PHPHumanizer\DateTimeHumanizer;
245+
246+
$timeUnit = TimeUnit::days(2)
247+
->add(TimeUnit::hours(3))
248+
->add(TimeUnit::minutes(25))
249+
->add(TimeUnit::seconds(30))
250+
->add(TimeUnit::milliseconds(200));
251+
252+
DateTimeHumanizer::timeUnit($timeUnit); // 2 days, 3 hours, 25 minutes, and 30.2 seconds
253+
```
254+
239255
Currently we support following languages:
240256
* [Azerbaijani](src/Coduo/PHPHumanizer/Resources/translations/difference.az.yml)
241257
* [English](src/Coduo/PHPHumanizer/Resources/translations/difference.en.yml)

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"require": {
1818
"php": "^7.4 | ^8.0",
1919
"symfony/translation": "^4.4|^5.0",
20-
"symfony/yaml": "^4.4|^5.0"
20+
"symfony/yaml": "^4.4|^5.0",
21+
"aeon-php/calendar": ">=0.16.1"
2122
},
2223
"require-dev": {
2324
"thunderer/shortcode": "^0.7",
@@ -40,6 +41,10 @@
4041
"ext-intl": "Required if you are going to use humanizer with locales different than en_EN"
4142
},
4243
"scripts": {
44+
"build": [
45+
"@static:analyze",
46+
"@test"
47+
],
4348
"cs:php:fix": [
4449
"tools/php-cs-fixer fix --using-cache=no"
4550
],

composer.lock

Lines changed: 62 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the PHP Humanizer Library.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Coduo\PHPHumanizer\Aeon\Calendar;
13+
14+
use Aeon\Calendar\Unit;
15+
use Coduo\PHPHumanizer\CollectionHumanizer;
16+
use Symfony\Contracts\Translation\TranslatorInterface;
17+
18+
final class Formatter
19+
{
20+
private TranslatorInterface $translator;
21+
22+
public function __construct(TranslatorInterface $translator)
23+
{
24+
$this->translator = $translator;
25+
}
26+
27+
public function timeUnit(Unit $unit, string $locale = 'en') : string
28+
{
29+
$parts = [];
30+
31+
foreach ((new UnitCompound($unit))->components() as $component) {
32+
$parts[] = $this->translator->trans(
33+
'compound.'.$component->getUnit()->getName(),
34+
['%count%' => $component->getQuantity()],
35+
'difference',
36+
$locale
37+
);
38+
}
39+
40+
return CollectionHumanizer::oxford($parts, null, $locale);
41+
}
42+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the PHP Humanizer Library.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Coduo\PHPHumanizer\Aeon\Calendar;
13+
14+
use Aeon\Calendar\RelativeTimeUnit;
15+
use Aeon\Calendar\TimeUnit;
16+
use Aeon\Calendar\Unit;
17+
use Coduo\PHPHumanizer\DateTime\DateIntervalCompound;
18+
use Coduo\PHPHumanizer\DateTime\Difference\CompoundResult;
19+
use Coduo\PHPHumanizer\DateTime\Unit\Day;
20+
use Coduo\PHPHumanizer\DateTime\Unit\Hour;
21+
use Coduo\PHPHumanizer\DateTime\Unit\Minute;
22+
use Coduo\PHPHumanizer\DateTime\Unit\Month;
23+
use Coduo\PHPHumanizer\DateTime\Unit\Second;
24+
use Coduo\PHPHumanizer\DateTime\Unit\Year;
25+
26+
final class UnitCompound
27+
{
28+
private Unit $unit;
29+
30+
public function __construct(Unit $timeUnit)
31+
{
32+
$this->unit = $timeUnit;
33+
}
34+
35+
/**
36+
* @return array<CompoundResult>
37+
*/
38+
public function components() : array
39+
{
40+
$unit = $this->unit;
41+
$compoundResults = [];
42+
43+
if ($unit instanceof RelativeTimeUnit) {
44+
if ($unit->inYears()) {
45+
$compoundResults[] = new CompoundResult(new Year(), $unit->inYears());
46+
}
47+
48+
if ($unit->inCalendarMonths()) {
49+
$compoundResults[] = new CompoundResult(new Month(), $unit->inCalendarMonths());
50+
}
51+
52+
return (new DateIntervalCompound($unit->toDateInterval()))->components();
53+
}
54+
55+
if ($unit instanceof TimeUnit) {
56+
if ($unit->inDaysAbs() > 0) {
57+
$compoundResults[] = new CompoundResult(new Day(), $unit->inDaysAbs());
58+
}
59+
60+
if ($unit->inTimeHours()) {
61+
$compoundResults[] = new CompoundResult(new Hour(), $unit->inTimeHours());
62+
}
63+
64+
if ($unit->inTimeMinutes()) {
65+
$compoundResults[] = new CompoundResult(new Minute(), $unit->inTimeMinutes());
66+
}
67+
68+
if ($unit->inTimeSeconds()) {
69+
$seconds = $unit->inTimeSeconds();
70+
71+
if ($unit->inTimeMilliseconds() > 0) {
72+
$seconds += $unit->inTimeMilliseconds() / 1000;
73+
}
74+
75+
$compoundResults[] = new CompoundResult(new Second(), $seconds);
76+
}
77+
78+
return $compoundResults;
79+
}
80+
81+
throw new \RuntimeException('Unsupported unit type ' . \get_class($unit));
82+
}
83+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the PHP Humanizer Library.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Coduo\PHPHumanizer\DateTime;
13+
14+
use Coduo\PHPHumanizer\DateTime\Difference\CompoundResult;
15+
use Coduo\PHPHumanizer\DateTime\Unit\Day;
16+
use Coduo\PHPHumanizer\DateTime\Unit\Hour;
17+
use Coduo\PHPHumanizer\DateTime\Unit\Minute;
18+
use Coduo\PHPHumanizer\DateTime\Unit\Month;
19+
use Coduo\PHPHumanizer\DateTime\Unit\Second;
20+
use Coduo\PHPHumanizer\DateTime\Unit\Year;
21+
22+
final class DateIntervalCompound
23+
{
24+
private \DateInterval $dateInterval;
25+
26+
public function __construct(\DateInterval $dateInterval)
27+
{
28+
$this->dateInterval = $dateInterval;
29+
}
30+
31+
/**
32+
* @return array<CompoundResult>
33+
*/
34+
public function components() : array
35+
{
36+
/* @var Unit[] $units */
37+
$units = [
38+
new Year(),
39+
new Month(),
40+
new Day(),
41+
new Hour(),
42+
new Minute(),
43+
new Second(),
44+
];
45+
46+
47+
/** @var array<CompoundResult> $compoundResults */
48+
$compoundResults = [];
49+
50+
foreach ($units as $unit) {
51+
if ($this->dateInterval->{$unit->getDateIntervalSymbol()} > 0) {
52+
$compoundResults[] = new CompoundResult($unit, (int) $this->dateInterval->{$unit->getDateIntervalSymbol()});
53+
}
54+
}
55+
56+
return $compoundResults;
57+
}
58+
}

src/Coduo/PHPHumanizer/DateTime/Difference.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
final class Difference
2424
{
25-
private \DateTime $fromDate;
25+
private \DateTimeInterface $fromDate;
2626

27-
private \DateTime $toDate;
27+
private \DateTimeInterface $toDate;
2828

2929
/**
3030
* @psalm-suppress PropertyNotSetInConstructor
@@ -33,7 +33,7 @@ final class Difference
3333

3434
private ?int $quantity = null;
3535

36-
public function __construct(\DateTime $fromDate, \DateTime $toDate)
36+
public function __construct(\DateTimeInterface $fromDate, \DateTimeInterface $toDate)
3737
{
3838
$this->fromDate = $fromDate;
3939
$this->toDate = $toDate;

src/Coduo/PHPHumanizer/DateTime/Difference/CompoundResult.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,29 @@
1616
final class CompoundResult
1717
{
1818
private Unit $unit;
19-
private int $quantity;
2019

21-
public function __construct(Unit $unit, int $quantity)
22-
{
23-
$this->unit = $unit;
24-
$this->quantity = $quantity;
25-
}
20+
/**
21+
* @var int|float
22+
*/
23+
private $quantity;
2624

27-
public function setQuantity(int $quantity): void
25+
/**
26+
* @param int|float $quantity
27+
*/
28+
public function __construct(Unit $unit, $quantity)
2829
{
30+
$this->unit = $unit;
2931
$this->quantity = $quantity;
3032
}
3133

32-
public function getQuantity() : int
34+
/**
35+
* @return int|float
36+
*/
37+
public function getQuantity()
3338
{
3439
return $this->quantity;
3540
}
3641

37-
public function setUnit(Unit $unit): void
38-
{
39-
$this->unit = $unit;
40-
}
41-
4242
public function getUnit(): Unit
4343
{
4444
return $this->unit;

0 commit comments

Comments
 (0)