Skip to content

Commit fab278a

Browse files
authored
ISSUE-113: Increments a year in a Season's end date, if start month > end month (#114)
* Increments a year in a Season's end date if start month > end month Also adds to public methods to fetch those years but keeps the original private $year as-is (used on the constructor) to avoid breaking the contract. Also adds some basic tests (and fixes testWinterValues() expected end dates) * Spacing * Spacing v/s tabs * More Coding style * reuse local var * Parser return is explicit ETDF Value, breaking static analysis. Maybe this fixes PHP Stan?
1 parent bd17b7f commit fab278a

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

src/Model/Season.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
class Season implements EdtfValue, HasPrecision {
1212
use CoversTrait;
1313

14+
/**
15+
* The initial year.
16+
* A Season could span into a next one.
17+
* @var int
18+
*/
1419
private int $year;
20+
1521
private int $season;
1622

1723
private ExtDate $start;
@@ -20,9 +26,11 @@ class Season implements EdtfValue, HasPrecision {
2026
public function __construct( int $year, int $season ) {
2127
$this->year = $year;
2228
$this->season = $season;
23-
24-
$this->start = new ExtDate( $year, $this->generateStartMonth() );
25-
$this->end = new ExtDate( $year, $this->generateEndMonth() );
29+
$startMonth = $this->generateStartMonth();
30+
$this->start = new ExtDate( $year, $startMonth );
31+
$endMonth = $this->generateEndMonth();
32+
$year = $endMonth < $startMonth ? ($year + 1) : $year;
33+
$this->end = new ExtDate( $year, $endMonth );
2634
}
2735

2836
private function generateStartMonth(): int {
@@ -99,6 +107,12 @@ public function getMin(): int {
99107
return $this->start->getMin();
100108
}
101109

110+
/**
111+
* Returns the Original Year present in the Season EDTF string definition.
112+
* Some seasons can expand to the next year.
113+
*
114+
* @return int
115+
*/
102116
public function getYear(): int {
103117
return $this->year;
104118
}
@@ -169,6 +183,14 @@ public function getEndMonth(): int {
169183
return $this->end->getMonth();
170184
}
171185

186+
public function getStartYear(): int {
187+
return $this->start->getYear();
188+
}
189+
190+
public function getEndYear(): int {
191+
return $this->end->getYear();
192+
}
193+
172194
public function precision(): int {
173195
return self::PRECISION_SEASON;
174196
}

tests/Unit/Model/SeasonTest.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,25 @@
1616
class SeasonTest extends TestCase {
1717

1818
public function testCreate(): void {
19+
// Quarter 1
1920
$season = new Season( 2010, 33 );
2021
$this->assertSame( 2010, $season->getYear() );
2122
$this->assertSame( 33, $season->getSeason() );
2223
$this->assertSame( [ 1, 2, 3 ], $season->getMonths() );
2324
$this->assertSame( 1, $season->getStartMonth() );
2425
$this->assertSame( 3, $season->getEndMonth() );
26+
$this->assertSame( 2010, $season->getStartYear() );
27+
$this->assertSame( 2010, $season->getEndYear() );
28+
29+
// Winter spanning into next year.
30+
$season = new Season( 1937, 24 );
31+
$this->assertSame( 1937, $season->getYear() );
32+
$this->assertSame( 24, $season->getSeason() );
33+
$this->assertSame( [ 12, 1, 2 ], $season->getMonths() );
34+
$this->assertSame( 12, $season->getStartMonth() );
35+
$this->assertSame( 2, $season->getEndMonth() );
36+
$this->assertSame( 1937, $season->getStartYear() );
37+
$this->assertSame( 1938, $season->getEndYear() );
2538
}
2639

2740
public function testSpringValues(): void {
@@ -43,9 +56,9 @@ public function testAutumnValues(): void {
4356
}
4457

4558
public function testWinterValues(): void {
46-
$this->assertSeasonValues( '2010-24', '2010-12-01', '2010-02-28' );
47-
$this->assertSeasonValues( '2010-28', '2010-12-01', '2010-02-28' );
48-
$this->assertSeasonValues( '2010-32', '2010-12-01', '2010-02-28' );
59+
$this->assertSeasonValues( '2010-24', '2010-12-01', '2011-02-28' );
60+
$this->assertSeasonValues( '2010-28', '2010-12-01', '2011-02-28' );
61+
$this->assertSeasonValues( '2010-32', '2010-12-01', '2011-02-28' );
4962
}
5063

5164
public function testQuarterValues(): void {
@@ -74,14 +87,22 @@ private function assertSeasonValues( string $input, string $expectedStart, strin
7487
$seasonStart = Carbon::createFromTimestamp( $season->getMin() );
7588
$seasonEnd = Carbon::createFromTimestamp( $season->getMax() );
7689

77-
// start season validation
90+
// Fetch max/min years from public methods.
91+
if ($season instanceof Season) {
92+
$seasonStartYearFromMethod = $season->getStartYear();
93+
$seasonEndYearFromMethod = $season->getEndYear();
94+
// start season validation
95+
$this->assertSame( $expectedStart->year, $seasonStartYearFromMethod);
96+
$this->assertSame( $expectedEnd->year, $seasonEndYearFromMethod);
97+
}
98+
7899
$this->assertSame( $expectedStart->year, $seasonStart->year );
79100
$this->assertSame( $expectedStart->month, $seasonStart->month );
80101
$this->assertSame( $expectedStart->day, $seasonStart->day );
81102

82-
// end season validation
83103
$this->assertSame( $expectedEnd->year, $seasonEnd->year );
84104
$this->assertSame( $expectedEnd->month, $seasonEnd->month );
85105
$this->assertSame( $expectedEnd->day, $seasonEnd->day );
106+
// end season validation.
86107
}
87108
}

0 commit comments

Comments
 (0)