Skip to content

Commit

Permalink
Enhancement: Add Range
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 9, 2020
1 parent 79ad9a9 commit 4b00584
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 3 deletions.
7 changes: 5 additions & 2 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ use Ergebnis\License;
use Ergebnis\PhpCsFixer\Config;

$holder = License\Holder::fromString('Andreas Möller');
$year = License\Year::fromString('2020');
$range = License\Range::since(
License\Year::fromString('2020'),
new \DateTimeZone('UTC')
);

$header = <<<EOF
Copyright (c) {$year->toString()} {$holder->toString()}
Copyright (c) {$range->toString()} {$holder->toString()}
For the full copyright and license information, please view
the LICENSE file that was distributed with this source code.
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ For a full diff see [`675601b...master`][675601b...master].

* Added `Holder` ([#7]), by [@localheinz]
* Added `Year` ([#10]), by [@localheinz]
* Added `Range` ([#12]), by [@localheinz]

[675601b...master]: https://github.com/ergebnis/license/compare/675601b...master

[#7]: https://github.com/ergebnis/license/pull/7
[#10]: https://github.com/ergebnis/license/pull/10
[#12]: https://github.com/ergebnis/license/pull/12

[@localheinz]: https://github.com/localheinz
37 changes: 37 additions & 0 deletions src/Exception/InvalidRange.php
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()
));
}
}
19 changes: 19 additions & 0 deletions src/Period.php
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;
}
92 changes: 92 additions & 0 deletions src/Range.php
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;
}
}
12 changes: 11 additions & 1 deletion src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Ergebnis\License;

final class Year
final class Year implements Period
{
private $value;

Expand All @@ -38,6 +38,16 @@ public static function fromString(string $value): self
return new self($value);
}

public function greaterThan(self $other): bool
{
return $this->value > $other->value;
}

public function equals(self $other): bool
{
return $this->value === $other->value;
}

public function toString(): string
{
return $this->value;
Expand Down
50 changes: 50 additions & 0 deletions test/Unit/Exception/InvalidRangeTest.php
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());
}
}
Loading

0 comments on commit 4b00584

Please sign in to comment.