Merge pull request #1140 from samsonasik/implement-todo-max-day

implements @todo max day in current month at Time::setDay()
This commit is contained in:
Lonnie Ezell 2018-08-06 22:21:27 -05:00 committed by GitHub
commit a3ddd405db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 4 deletions

View File

@ -13,6 +13,11 @@ class I18nException extends FrameworkException implements ExceptionInterface
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)

View File

@ -631,8 +631,6 @@ class Time extends DateTime
/**
* Sets the day of the month.
*
* @todo check max days in month (localized)
*
* @param $value
*
* @return \CodeIgniter\I18n\Time
@ -642,7 +640,14 @@ class Time extends DateTime
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);
}

View File

@ -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}',

View File

@ -405,6 +405,25 @@ class TimeTest extends \CIUnitTestCase
$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()