mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
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:
commit
a3ddd405db
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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}',
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user