Skip to content

Commit 2d5a5bf

Browse files
authored
Add providers (#13)
* Add DateRangeProvider * Add FiniteDateRangeProvider * Test provider
1 parent d9773a7 commit 2d5a5bf

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

src/DateRangeProvider.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* This file is part of Zee Project.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @see https://github.com/zee/
9+
*/
10+
11+
namespace Zee\DateRange;
12+
13+
/**
14+
* Date range provider.
15+
*
16+
* Interface for builders for predefined ranges.
17+
*/
18+
interface DateRangeProvider
19+
{
20+
public function getDateRange(): DateRangeInterface;
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* This file is part of Zee Project.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @see https://github.com/zee/
9+
*/
10+
11+
namespace Zee\DateRange\Providers;
12+
13+
use DateTimeInterface;
14+
use Zee\DateRange\DateRange;
15+
use Zee\DateRange\DateRangeInterface;
16+
use Zee\DateRange\DateRangeProvider;
17+
18+
/**
19+
* Finite date range provider.
20+
*/
21+
final class FiniteDateRangeProvider implements DateRangeProvider
22+
{
23+
/**
24+
* @var DateTimeInterface
25+
*/
26+
private $startDate;
27+
28+
/**
29+
* @var DateTimeInterface
30+
*/
31+
private $endDate;
32+
33+
/**
34+
* @param DateTimeInterface $startDate
35+
* @param DateTimeInterface $endDate
36+
*/
37+
public function __construct(DateTimeInterface $startDate, DateTimeInterface $endDate)
38+
{
39+
$this->startDate = $startDate;
40+
$this->endDate = $endDate;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function getDateRange(): DateRangeInterface
47+
{
48+
return new DateRange($this->startDate, $this->endDate);
49+
}
50+
}

tests/DateRangeProviderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* This file is part of Zee Project.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @see https://github.com/zee/
9+
*/
10+
11+
namespace Zee\DateRange;
12+
13+
use DateTimeImmutable;
14+
use PHPUnit\Framework\TestCase;
15+
use Zee\DateRange\Providers\FiniteDateRangeProvider;
16+
17+
/**
18+
* Class DateRangeProviderTest.
19+
*/
20+
final class DateRangeProviderTest extends TestCase
21+
{
22+
/**
23+
* @test
24+
*/
25+
public function usingBuilder()
26+
{
27+
$yesterday = new DateTimeImmutable('-1 day');
28+
$tomorrow = new DateTimeImmutable('+1 day');
29+
$calculator = function (FiniteDateRangeProvider $dateRangeProvider) {
30+
$range = $dateRangeProvider->getDateRange();
31+
$interval = $range->getDateInterval();
32+
33+
return $interval->days;
34+
};
35+
36+
$dateRangeProvider = new FiniteDateRangeProvider($yesterday, $tomorrow);
37+
$result = $calculator($dateRangeProvider);
38+
39+
self::assertSame(2, $result);
40+
}
41+
}

tests/DateRangeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* Class DateRangeTest.
1919
*/
20-
class DateRangeTest extends TestCase
20+
final class DateRangeTest extends TestCase
2121
{
2222
/**
2323
* @test

0 commit comments

Comments
 (0)