Skip to content

Commit

Permalink
Merge pull request #1140 from samsonasik/implement-todo-max-day
Browse files Browse the repository at this point in the history
implements @todo max day in current month at Time::setDay()
  • Loading branch information
lonnieezell authored Aug 7, 2018
2 parents 78e5f33 + 803a97d commit a3ddd40
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions system/I18n/Exceptions/I18nException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public static function forInvalidMonth(string $month)
public static function forInvalidDay(string $day)
{
return new static(lang('Time.invalidDay', [$day]));
}

public static function forInvalidOverDay(string $lastDay, string $day)
{
return new static(lang('Time.invalidOverDay', [$lastDay, $day]));
}

public static function forInvalidHour(string $hour)
Expand Down
11 changes: 8 additions & 3 deletions system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,6 @@ public function setMonth($value)
/**
* Sets the day of the month.
*
* @todo check max days in month (localized)
*
* @param $value
*
* @return \CodeIgniter\I18n\Time
Expand All @@ -642,7 +640,14 @@ public function setDay($value)
if ($value < 1 || $value > 31)
{
throw I18nException::forInvalidDay($value);
}
}

$date = $this->getYear() . '-' . $this->getMonth();
$lastDay = date('t', strtotime($date));
if ($value > $lastDay)
{
throw I18nException::forInvalidOverDay($lastDay, $value);
}

return $this->setValue('day', $value);
}
Expand Down
3 changes: 2 additions & 1 deletion system/Language/en/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
return [
'invalidMonth' => 'Months must be between 1 and 12. Given: {0}',
'invalidDay' => 'Days must be between 1 and 31. Given: {0}',
'invalidDay' => 'Days must be between 1 and 31. Given: {0}',
'invalidOverDay' => 'Days must be between 1 and {0}. Given: {1}',
'invalidHours' => 'Hours must be between 0 and 23. Given: {0}',
'invalidMinutes' => 'Minutes must be between 0 and 59. Given: {0}',
'invalidSeconds' => 'Seconds must be between 0 and 59. Given: {0}',
Expand Down
19 changes: 19 additions & 0 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,25 @@ public function testSetDay()
$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2017-05-15 00:00:00', $time2->toDateTimeString());
}

/**
* @expectedException \CodeIgniter\I18n\Exceptions\I18nException
*/
public function testSetDayOverMaxInCurrentMonth()
{
$time = Time::parse('Feb 02, 2009');
$time->setDay(29);
}

public function testSetDayNotOverMaxInCurrentMonth()
{
$time = Time::parse('Feb 02, 2012');
$time2 = $time->setDay(29);

$this->assertInstanceOf(Time::class, $time2);
$this->assertNotSame($time, $time2);
$this->assertEquals('2012-02-29 00:00:00', $time2->toDateTimeString());
}

public function testSetHour()
Expand Down

0 comments on commit a3ddd40

Please sign in to comment.