Skip to content

Commit

Permalink
Merge pull request #13 from pauci/feature/php-8-1
Browse files Browse the repository at this point in the history
feat: Add php 8.1 support; Drop php 7.3 and 7.4 support
  • Loading branch information
pauci authored Dec 20, 2021
2 parents d9b78e1 + 6eb6559 commit ab74790
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 240 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
coverage: none

- name: Get composer cache directory
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: 7.4
php-version: 8.1

- name: Get composer cache directory
id: composer-cache
Expand All @@ -77,7 +77,7 @@ jobs:

strategy:
matrix:
php-versions: ['7.3', '7.4', '8.0']
php-versions: ['8.0', '8.1']

steps:
- name: Checkout code
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"issues": "https://github.com/pauci/datetime/issues"
},
"require": {
"php": "^7.3 | ^8.0",
"php": "~8.0.0||~8.1.0",
"ext-json": "*"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12.58",
"phpstan/phpstan-phpunit": "^0.12.16",
"phpstan/phpstan": "^1.2",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5"
},
Expand Down
74 changes: 35 additions & 39 deletions src/DateInterval.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

declare(strict_types=1);

namespace Pauci\DateTime;

class DateInterval extends \DateInterval implements \JsonSerializable
class DateInterval extends \DateInterval implements \JsonSerializable, \Stringable
{
/**
* @param \DateInterval $dateInterval
* @return DateInterval
* @throws \Exception
*/
public static function fromDateInterval(\DateInterval $dateInterval): self
public static function fromDateInterval(\DateInterval $dateInterval): static
{
$interval = new self('P0D');
$interval = new static('P0D');
$interval->y = $dateInterval->y;
$interval->m = $dateInterval->m;
$interval->d = $dateInterval->d;
Expand All @@ -21,17 +20,11 @@ public static function fromDateInterval(\DateInterval $dateInterval): self
$interval->s = $dateInterval->s;
$interval->invert = $dateInterval->invert;
$interval->days = $dateInterval->days;

return $interval;
}

/**
* @param int $years
* @param int $months
* @param int $days
* @param int $hours
* @param int $minutes
* @param int $seconds
* @return DateInterval
* @throws \Exception
*/
public static function fromParts(
Expand All @@ -41,76 +34,79 @@ public static function fromParts(
int $hours = 0,
int $minutes = 0,
int $seconds = 0
): self {
$interval = new self('P0D');
): static {
$interval = new static('P0D');
$interval->y = $years;
$interval->m = $months;
$interval->d = $days;
$interval->h = $hours;
$interval->i = $minutes;
$interval->s = $seconds;

return $interval;
}

final public function __construct(string $duration)
{
parent::__construct($duration);
}

/**
* @param string $interval
* @return DateInterval
* @throws \Exception
*/
public static function fromString(string $interval): self
public static function fromString(string $interval): static
{
return new self($interval);
return new static($interval);
}

/**
* @return string
*/
public function __toString(): string
{
return $this->toString();
}

/**
* @return string
*/
public function jsonSerialize(): string
{
return $this->toString();
}

/**
* @return string
*/
public function toString(): string
{
$dateString = '';
$datePart = '';

if ($this->y !== 0) {
$dateString .= $this->y . 'Y';
$datePart .= $this->y . 'Y';
}

if ($this->m !== 0) {
$dateString .= $this->m . 'M';
$datePart .= $this->m . 'M';
}

if ($this->d !== 0) {
$dateString .= $this->d . 'D';
$datePart .= $this->d . 'D';
}

$timeString = '';
$timePart = '';

if ($this->h !== 0) {
$timeString .= $this->h . 'H';
$timePart .= $this->h . 'H';
}

if ($this->i !== 0) {
$timeString .= $this->i . 'M';
$timePart .= $this->i . 'M';
}

if ($this->s !== 0) {
$timeString .= $this->s . 'S';
$timePart .= $this->s . 'S';
}

if ($timeString === '') {
if ($dateString === '') {
if ($timePart === '') {
if ($datePart === '') {
return 'P0D';
}
return 'P' . $dateString;

return 'P' . $datePart;
}
return 'P' . $dateString . 'T' . $timeString;

return 'P' . $datePart . 'T' . $timePart;
}
}
Loading

0 comments on commit ab74790

Please sign in to comment.