generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79ad9a9
commit 4b00584
Showing
9 changed files
with
416 additions
and
3 deletions.
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
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Exception; | ||
|
||
use Ergebnis\License\Year; | ||
|
||
final class InvalidRange extends \InvalidArgumentException implements Exception | ||
{ | ||
public static function startYearGreaterThanEndYear(Year $start, Year $end): self | ||
{ | ||
return new self(\sprintf( | ||
'Start year "%s" can not be greater than end year "%s".', | ||
$start->toString(), | ||
$end->toString() | ||
)); | ||
} | ||
|
||
public static function startYearGreaterThanCurrentYear(Year $start, Year $current): self | ||
{ | ||
return new self(\sprintf( | ||
'Start year "%s" can not be greater than current year "%s".', | ||
$start->toString(), | ||
$current->toString() | ||
)); | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License; | ||
|
||
interface Period | ||
{ | ||
public function toString(): string; | ||
} |
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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License; | ||
|
||
final class Range implements Period | ||
{ | ||
private $value; | ||
|
||
private function __construct(string $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @param Year $start | ||
* @param Year $end | ||
* | ||
* @throws Exception\InvalidRange | ||
* | ||
* @return Period | ||
*/ | ||
public static function including(Year $start, Year $end): Period | ||
{ | ||
if ($start->greaterThan($end)) { | ||
throw Exception\InvalidRange::startYearGreaterThanEndYear( | ||
$start, | ||
$end | ||
); | ||
} | ||
|
||
if ($start->equals($end)) { | ||
return $start; | ||
} | ||
|
||
return new self(\sprintf( | ||
'%s-%s', | ||
$start->toString(), | ||
$end->toString() | ||
)); | ||
} | ||
|
||
/** | ||
* @param Year $start | ||
* @param \DateTimeZone $timeZone | ||
* | ||
* @throws Exception\InvalidRange | ||
* | ||
* @return Period | ||
*/ | ||
public static function since(Year $start, \DateTimeZone $timeZone): Period | ||
{ | ||
$now = new \DateTimeImmutable( | ||
'now', | ||
$timeZone | ||
); | ||
|
||
$current = Year::fromString($now->format('Y')); | ||
|
||
if ($start->greaterThan($current)) { | ||
throw Exception\InvalidRange::startYearGreaterThanCurrentYear( | ||
$start, | ||
$current | ||
); | ||
} | ||
|
||
if ($start->equals($current)) { | ||
return $start; | ||
} | ||
|
||
return new self(\sprintf( | ||
'%s-%s', | ||
$start->toString(), | ||
$current->toString() | ||
)); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Test\Unit\Exception; | ||
|
||
use Ergebnis\License\Exception\InvalidRange; | ||
use Ergebnis\License\Year; | ||
use Ergebnis\Test\Util\Helper; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\License\Exception\InvalidRange | ||
* | ||
* @uses \Ergebnis\License\Year | ||
*/ | ||
final class InvalidRangeTest extends Framework\TestCase | ||
{ | ||
use Helper; | ||
|
||
public function testFromValueReturnsInvalidYear(): void | ||
{ | ||
$start = Year::fromString('2020'); | ||
$end = Year::fromString('2019'); | ||
|
||
$exception = InvalidRange::startYearGreaterThanEndYear( | ||
$start, | ||
$end | ||
); | ||
|
||
$expected = \sprintf( | ||
'Start year "%s" can not be greater than end year "%s".', | ||
$start->toString(), | ||
$end->toString() | ||
); | ||
|
||
self::assertSame($expected, $exception->getMessage()); | ||
} | ||
} |
Oops, something went wrong.