Add Calendar class

This commit is contained in:
Kijin Sung 2016-03-17 16:22:18 +09:00
parent 63006e025a
commit 15a0c591dc
2 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,132 @@
<?php
namespace Rhymix\Framework;
/**
* The calendar class.
*/
class Calendar
{
/**
* This method returns the English name of a month, e.g. 9 = 'September'.
*
* @param int $month_number
* @param bool $long_format (optional, default is true)
* @return string
*/
public static function getMonthName($month_number, $long_format = true)
{
$month_number = intval($month_number, 10);
if (!is_between($month_number, 1, 12))
{
return false;
}
return date($long_format ? 'F' : 'M', mktime(0, 0, 0, $month_number, 1));
}
/**
* This method returns the day on which a month begins.
*
* 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday.
* If you do not specify a year, the current year is assumed.
*
* @param int $month_number
* @param int $year (optional)
* @return int
*/
public static function getMonthStartDayOfWeek($month_number, $year = null)
{
$month_number = intval($month_number, 10);
if (!is_between($month_number, 1, 12))
{
return false;
}
return (int)date('w', mktime(0, 0, 0, $month_number, 1, $year ?: date('Y')));
}
/**
* This method returns the number of days in a month, e.g. February 2016 has 29 days.
*
* If you do not specify a year, the current year is assumed.
* You must specify a year to get the number of days in February.
*
* @param int $month_number
* @param int $year (optional)
* @return int
*/
public static function getMonthDays($month_number, $year = null)
{
$month_number = intval($month_number, 10);
if (!is_between($month_number, 1, 12))
{
return false;
}
return (int)date('t', mktime(0, 0, 0, $month_number, 1, $year ?: date('Y')));
}
/**
* This method returns a complete calendar for a month.
*
* The return value is an array with six members, each representing a week.
* Each week is an array with seven members, each representing a day.
* 6 weeks are returned. Empty cells are represented by nulls.
*
* If you do not specify a year, the current year is assumed.
*
* @param int $month_number
* @param int $year (optional)
* @param int $start_dow (optional)
* @return array
*/
public static function getMonthCalendar($month_number, $year = null, $start_dow = 0)
{
$month_number = intval($month_number, 10);
if (!is_between($month_number, 1, 12))
{
return false;
}
if (!is_between($start_dow, 0, 6))
{
return false;
}
if (!$year || !is_between($year, 1000, 9999))
{
$year = date('Y');
}
$start = self::getMonthStartDayOfWeek($month_number, $year);
$count = self::getMonthDays($month_number, $year);
$initial_blank_cells = (7 + $start - $start_dow) % 7;
$final_blank_cells = 42 - $count - $initial_blank_cells;
$temp = [];
for ($i = 0; $i < $initial_blank_cells; $i++)
{
$temp[] = null;
}
for ($i = 0; $i < $count; $i++)
{
$temp[] = $i + 1;
}
for ($i = 0; $i < $final_blank_cells; $i++)
{
$temp[] = null;
}
$return = [];
for ($i = 0; $i < 6; $i++)
{
$week = [];
for ($j = 0; $j < 7; $j++)
{
$week[] = array_shift($temp);
}
$return[] = $week;
}
return $return;
}
}

View file

@ -0,0 +1,52 @@
<?php
class CalendarTest extends \Codeception\TestCase\Test
{
public function testGetMonthName()
{
$this->assertEquals('January', Rhymix\Framework\Calendar::getMonthName('01'));
$this->assertEquals('October', Rhymix\Framework\Calendar::getMonthName('10'));
$this->assertEquals('Nov', Rhymix\Framework\Calendar::getMonthName(11, false));
$this->assertEquals('Dec', Rhymix\Framework\Calendar::getMonthName(12, false));
}
public function testGetMonthStartDayOfWeek()
{
$this->assertEquals(5, Rhymix\Framework\Calendar::getMonthStartDayOfWeek(1, 2016));
$this->assertEquals(1, Rhymix\Framework\Calendar::getMonthStartDayOfWeek(2, 2016));
$this->assertEquals(2, Rhymix\Framework\Calendar::getMonthStartDayOfWeek(3, 2016));
$this->assertEquals(5, Rhymix\Framework\Calendar::getMonthStartDayOfWeek(4, 2016));
}
public function testGetMonthDays()
{
$this->assertEquals(30, Rhymix\Framework\Calendar::getMonthDays(11, 2015));
$this->assertEquals(31, Rhymix\Framework\Calendar::getMonthDays(12, 2015));
$this->assertEquals(31, Rhymix\Framework\Calendar::getMonthDays(1, 2016));
$this->assertEquals(29, Rhymix\Framework\Calendar::getMonthDays(2, 2016));
}
public function testGetMonthCalendar()
{
$target_201508 = array(
array(null, null, null, null, null, null, 1),
array(2, 3, 4, 5, 6, 7, 8),
array(9, 10, 11, 12, 13, 14, 15),
array(16, 17, 18, 19, 20, 21, 22),
array(23, 24, 25, 26, 27, 28, 29),
array(30, 31, null, null, null, null, null),
);
$target_201603 = array(
array(null, null, 1, 2, 3, 4, 5),
array(6, 7, 8, 9, 10, 11, 12),
array(13, 14, 15, 16, 17, 18, 19),
array(20, 21, 22, 23, 24, 25, 26),
array(27, 28, 29, 30, 31, null, null),
array(null, null, null, null, null, null, null),
);
$this->assertEquals($target_201508, Rhymix\Framework\Calendar::getMonthCalendar(8, 2015));
$this->assertEquals($target_201603, Rhymix\Framework\Calendar::getMonthCalendar(3, 2016));
}
}