Skip to content

Commit 6e82969

Browse files
authored
Prepare v0.3 (#15)
* Update CHANGELOG * Update README
1 parent e99542f commit 6e82969

File tree

2 files changed

+135
-4
lines changed

2 files changed

+135
-4
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ Security - in case of vulnerabilities.
1818

1919
_TBD_
2020

21+
## [0.3.0] 2017-11-18
22+
23+
### Added
24+
- Added new methods `getInterval`, `getPeriod`, `split`
25+
- Added `DateRangeProvider` interface and basic provider `FiniteDateRangeProvider`
26+
27+
### Changed
28+
- Rename methods from using `time` ot `date`.
29+
- Refactored internals.
30+
2131
## [0.1.1] 2017-11-17
2232

2333
### Changed
2434
- Implement `State` pattern to control ranges objects.
2535
- Change JSON representation to array instead of string in previous version.
2636

27-
### Changed
37+
### Removed
2838
- Remove mutable object, leave only immutable.
2939
- Remove implementation of Serializable interface.
3040

README.md

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,140 @@
66
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
77
[![Quality Score][ico-code-quality]][link-code-quality]
88

9-
*TBD*
9+
Implementation of the **Date Range** missing in PHP.
1010

1111
## Install
1212

13-
Via [Composer](https://getcomposer.org)
13+
Using [Composer](https://getcomposer.org)
1414

1515
```bash
1616
composer require z-ee/date-range
1717
```
1818

1919
## Usage
2020

21-
*TBD*
21+
### DateRange - value object
22+
23+
Instantiating value object and accessing properties:
24+
25+
~~~php
26+
$range = new DateRange(new DateTime('-1 day'), new DateTime('+1 day'));
27+
// Checking if range has start date
28+
$range->hasStartDate();
29+
// Accessing range start date
30+
$range->getStartDate()->format('c');
31+
// Checking if range has end date
32+
$range->hasEndDate();
33+
// Accessing range end date
34+
$range->getEndDate()->format('c');
35+
// Checking if range is finite
36+
$range->isFinite();
37+
// Checking if range already started
38+
$range->isStarted();
39+
// Checking if range already ended
40+
$range->isEnded();
41+
// Checking if range started on specific date
42+
$range->isStartedOn(new DateTime());
43+
// Checking if range ended on specific date
44+
$range->isEndedOn(new DateTime());
45+
// Accessing range interval
46+
$range->getDateInterval()->format('%s');
47+
// Printing
48+
echo $range;
49+
// Representing as JSON
50+
json_encode($range);
51+
~~~
52+
53+
Iterating over the range:
54+
55+
~~~php
56+
$range = new DateRange(new DateTime('-1 day'), new DateTime('+1 day'));
57+
58+
foreach ($range->getDatePeriod(new DateInterval('P1D')) as $date) {
59+
echo $date->format('Y-m-d');
60+
}
61+
~~~
62+
63+
Splitting range into smaller ranges:
64+
65+
~~~php
66+
$range = new DateRange(new DateTime('-1 day'), new DateTime('+1 day'));
67+
68+
foreach ($range->split(new DateInterval('P1D')) as $range) {
69+
echo $range;
70+
}
71+
~~~
72+
73+
Date range is immutable, any changes resulting to new object:
74+
75+
~~~php
76+
$initial = new DateRange(new DateTime('-1 day'), new DateTime('+1 day'));
77+
$actual = $initial->setStartDate(new DateTime('now'));
78+
if ($initial === $actual) {
79+
throw new LogicException('Oh, ah');
80+
}
81+
~~~
82+
83+
### DateRangeProvider - the date ranges builder.
84+
85+
Using builder to create new range with specific rules:
86+
87+
~~~php
88+
class RangeForYear implements DateRangeProvider
89+
{
90+
/**
91+
* @var int
92+
*/
93+
private $year;
94+
95+
/**
96+
* @param int $year
97+
*/
98+
public function __construct(int $year)
99+
{
100+
$this->year = $year;
101+
}
102+
103+
/**
104+
* {@inheritdoc}
105+
*/
106+
public function getDateRange(): DateRangeInterface
107+
{
108+
return new DateRange(
109+
new DateTimeImmutable(DateTimeImmutable::createFromFormat('c', "{$this->year}-01-01T00:00:00Z")),
110+
new DateTimeImmutable(DateTimeImmutable::createFromFormat('c', "{$this->year}-12-31T23:59:59Z"))
111+
);
112+
}
113+
~~~
114+
115+
Your classes might depend on range provider instead of `DateRange`,
116+
useful when predefined ranges are more meaningful than range interface:
117+
118+
~~~php
119+
class ReportCalculator
120+
{
121+
public function calculate(DateRangeProvider $provider)
122+
{
123+
echo $provider->getDateRange();
124+
}
125+
}
126+
127+
$calculator->calculate(new RangeForYear(2017));
128+
$calculator->calculate(new RangeForQuarter(2017));
129+
$calculator->calculate(new RangeForMonth(2017));
130+
~~~
131+
132+
Even your class might require concrete range contact:
133+
134+
~~~php
135+
class ReportCalculator
136+
{
137+
public function calculate(FiniteDateRangeProvider $provider)
138+
{
139+
echo $provider->getDateRange();
140+
}
141+
}
142+
~~~
22143

23144
## Testing
24145

0 commit comments

Comments
 (0)