|
6 | 6 | [![Coverage Status][ico-scrutinizer]][link-scrutinizer] |
7 | 7 | [![Quality Score][ico-code-quality]][link-code-quality] |
8 | 8 |
|
9 | | -*TBD* |
| 9 | +Implementation of the **Date Range** missing in PHP. |
10 | 10 |
|
11 | 11 | ## Install |
12 | 12 |
|
13 | | -Via [Composer](https://getcomposer.org) |
| 13 | +Using [Composer](https://getcomposer.org) |
14 | 14 |
|
15 | 15 | ```bash |
16 | 16 | composer require z-ee/date-range |
17 | 17 | ``` |
18 | 18 |
|
19 | 19 | ## Usage |
20 | 20 |
|
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 | +~~~ |
22 | 143 |
|
23 | 144 | ## Testing |
24 | 145 |
|
|
0 commit comments