Skip to content

Commit

Permalink
Merge pull request #8 from phpmentors-jp/fix-threemonth-trait-enddate
Browse files Browse the repository at this point in the history
[DateTime] fix threemonthly trait calcuration with non 1st start date
  • Loading branch information
hidenorigoto committed Mar 23, 2016
2 parents 7ae2c06 + 3fec89e commit 32c7886
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/DateTime/Period/ThreeMonthlyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace PHPMentors\DomainCommons\DateTime\Period;

use PHPMentors\DomainCommons\DateTime\Date;
use PHPMentors\DomainCommons\DateTime\Term;

trait ThreeMonthlyTrait
Expand Down Expand Up @@ -39,16 +40,18 @@ public function iterate()
$start = clone $this->start;
while (true) {
$end = clone $start->addMonths(2);
$end = min($end, $this->end);
$end = new \DateTime($end->format('Y-m-t'));
$end = new Date($end->format('Y-m-t'));
if ($end > $this->end) {
$end = $this->end;
}

if ($this->_termFactory) {
yield call_user_func($this->_termFactory, $start, $end);
} else {
yield new Term($start, $end);
}

$start = $start->addMonths(3);
$start = $end->addDays(1);
if ($start > $this->end) {
break;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/DateTime/ThreeMonthlyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public function everyTheeMonthsTest()
['2015-11-01', '2016-01-31'],
['2016-02-01', '2016-03-31'],
]],
['2015-02-01', '2015-04-15', [
['2015-02-01', '2015-04-15'],
]],
['2015-02-24', '2015-07-09', [
['2015-02-24', '2015-04-30'],
['2015-05-01', '2015-07-09'],
]],
];
}
}

0 comments on commit 32c7886

Please sign in to comment.