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

Added integration with aeon-php/calendar library #123

Merged
merged 1 commit into from
Feb 23, 2021
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \
DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2016-04-27 13:00:00")); // 2 years, 1 day from now
```

## Aeon Calendar

[Aeon PHP](https://aeon-php.org/) is a date&time oriented set of libraries.

```php
use Coduo\PHPHumanizer\DateTimeHumanizer;

$timeUnit = TimeUnit::days(2)
->add(TimeUnit::hours(3))
->add(TimeUnit::minutes(25))
->add(TimeUnit::seconds(30))
->add(TimeUnit::milliseconds(200));

DateTimeHumanizer::timeUnit($timeUnit); // 2 days, 3 hours, 25 minutes, and 30.2 seconds
```

Currently we support following languages:
* [Azerbaijani](src/Coduo/PHPHumanizer/Resources/translations/difference.az.yml)
* [English](src/Coduo/PHPHumanizer/Resources/translations/difference.en.yml)
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"require": {
"php": "^7.4 | ^8.0",
"symfony/translation": "^4.4|^5.0",
"symfony/yaml": "^4.4|^5.0"
"symfony/yaml": "^4.4|^5.0",
"aeon-php/calendar": ">=0.16.1"
},
"require-dev": {
"thunderer/shortcode": "^0.7",
Expand All @@ -40,6 +41,10 @@
"ext-intl": "Required if you are going to use humanizer with locales different than en_EN"
},
"scripts": {
"build": [
"@static:analyze",
"@test"
],
"cs:php:fix": [
"tools/php-cs-fixer fix --using-cache=no"
],
Expand Down
69 changes: 62 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/Coduo/PHPHumanizer/Aeon/Calendar/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the PHP Humanizer Library.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Coduo\PHPHumanizer\Aeon\Calendar;

use Aeon\Calendar\Unit;
use Coduo\PHPHumanizer\CollectionHumanizer;
use Symfony\Contracts\Translation\TranslatorInterface;

final class Formatter
{
private TranslatorInterface $translator;

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

public function timeUnit(Unit $unit, string $locale = 'en') : string
{
$parts = [];

foreach ((new UnitCompound($unit))->components() as $component) {
$parts[] = $this->translator->trans(
'compound.'.$component->getUnit()->getName(),
['%count%' => $component->getQuantity()],
'difference',
$locale
);
}

return CollectionHumanizer::oxford($parts, null, $locale);
}
}
83 changes: 83 additions & 0 deletions src/Coduo/PHPHumanizer/Aeon/Calendar/UnitCompound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

/*
* This file is part of the PHP Humanizer Library.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Coduo\PHPHumanizer\Aeon\Calendar;

use Aeon\Calendar\RelativeTimeUnit;
use Aeon\Calendar\TimeUnit;
use Aeon\Calendar\Unit;
use Coduo\PHPHumanizer\DateTime\DateIntervalCompound;
use Coduo\PHPHumanizer\DateTime\Difference\CompoundResult;
use Coduo\PHPHumanizer\DateTime\Unit\Day;
use Coduo\PHPHumanizer\DateTime\Unit\Hour;
use Coduo\PHPHumanizer\DateTime\Unit\Minute;
use Coduo\PHPHumanizer\DateTime\Unit\Month;
use Coduo\PHPHumanizer\DateTime\Unit\Second;
use Coduo\PHPHumanizer\DateTime\Unit\Year;

final class UnitCompound
{
private Unit $unit;

public function __construct(Unit $timeUnit)
{
$this->unit = $timeUnit;
}

/**
* @return array<CompoundResult>
*/
public function components() : array
{
$unit = $this->unit;
$compoundResults = [];

if ($unit instanceof RelativeTimeUnit) {
if ($unit->inYears()) {
$compoundResults[] = new CompoundResult(new Year(), $unit->inYears());
}

if ($unit->inCalendarMonths()) {
$compoundResults[] = new CompoundResult(new Month(), $unit->inCalendarMonths());
}

return (new DateIntervalCompound($unit->toDateInterval()))->components();
}

if ($unit instanceof TimeUnit) {
if ($unit->inDaysAbs() > 0) {
$compoundResults[] = new CompoundResult(new Day(), $unit->inDaysAbs());
}

if ($unit->inTimeHours()) {
$compoundResults[] = new CompoundResult(new Hour(), $unit->inTimeHours());
}

if ($unit->inTimeMinutes()) {
$compoundResults[] = new CompoundResult(new Minute(), $unit->inTimeMinutes());
}

if ($unit->inTimeSeconds()) {
$seconds = $unit->inTimeSeconds();

if ($unit->inTimeMilliseconds() > 0) {
$seconds += $unit->inTimeMilliseconds() / 1000;
}

$compoundResults[] = new CompoundResult(new Second(), $seconds);
}

return $compoundResults;
}

throw new \RuntimeException('Unsupported unit type ' . \get_class($unit));
}
}
58 changes: 58 additions & 0 deletions src/Coduo/PHPHumanizer/DateTime/DateIntervalCompound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/*
* This file is part of the PHP Humanizer Library.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Coduo\PHPHumanizer\DateTime;

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

final class DateIntervalCompound
{
private \DateInterval $dateInterval;

public function __construct(\DateInterval $dateInterval)
{
$this->dateInterval = $dateInterval;
}

/**
* @return array<CompoundResult>
*/
public function components() : array
{
/* @var Unit[] $units */
$units = [
new Year(),
new Month(),
new Day(),
new Hour(),
new Minute(),
new Second(),
];


/** @var array<CompoundResult> $compoundResults */
$compoundResults = [];

foreach ($units as $unit) {
if ($this->dateInterval->{$unit->getDateIntervalSymbol()} > 0) {
$compoundResults[] = new CompoundResult($unit, (int) $this->dateInterval->{$unit->getDateIntervalSymbol()});
}
}

return $compoundResults;
}
}
6 changes: 3 additions & 3 deletions src/Coduo/PHPHumanizer/DateTime/Difference.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

final class Difference
{
private \DateTime $fromDate;
private \DateTimeInterface $fromDate;

private \DateTime $toDate;
private \DateTimeInterface $toDate;

/**
* @psalm-suppress PropertyNotSetInConstructor
Expand All @@ -33,7 +33,7 @@ final class Difference

private ?int $quantity = null;

public function __construct(\DateTime $fromDate, \DateTime $toDate)
public function __construct(\DateTimeInterface $fromDate, \DateTimeInterface $toDate)
{
$this->fromDate = $fromDate;
$this->toDate = $toDate;
Expand Down
26 changes: 13 additions & 13 deletions src/Coduo/PHPHumanizer/DateTime/Difference/CompoundResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@
final class CompoundResult
{
private Unit $unit;
private int $quantity;

public function __construct(Unit $unit, int $quantity)
{
$this->unit = $unit;
$this->quantity = $quantity;
}
/**
* @var int|float
*/
private $quantity;

public function setQuantity(int $quantity): void
/**
* @param int|float $quantity
*/
public function __construct(Unit $unit, $quantity)
{
$this->unit = $unit;
$this->quantity = $quantity;
}

public function getQuantity() : int
/**
* @return int|float
*/
public function getQuantity()
{
return $this->quantity;
}

public function setUnit(Unit $unit): void
{
$this->unit = $unit;
}

public function getUnit(): Unit
{
return $this->unit;
Expand Down
Loading