From 98b203fe691ec1ea9d6d19e916827185b575b05a Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Fri, 16 Oct 2015 22:43:07 -0400 Subject: Move DateParser class to Core namespace --- app/Core/DateParser.php | 241 +++++++++++++++++++++ app/Model/DateParser.php | 241 --------------------- app/ServiceProvider/ClassProvider.php | 2 +- tests/units/Core/DateParserTest.php | 84 +++++++ .../Formatter/TaskFilterCalendarFormatterTest.php | 2 +- .../Formatter/TaskFilterGanttFormatterTest.php | 2 +- .../Formatter/TaskFilterICalendarFormatterTest.php | 2 +- tests/units/Model/DateParserTest.php | 84 ------- tests/units/Model/TaskFilterTest.php | 2 +- 9 files changed, 330 insertions(+), 330 deletions(-) create mode 100644 app/Core/DateParser.php delete mode 100644 app/Model/DateParser.php create mode 100644 tests/units/Core/DateParserTest.php delete mode 100644 tests/units/Model/DateParserTest.php diff --git a/app/Core/DateParser.php b/app/Core/DateParser.php new file mode 100644 index 00000000..026cf305 --- /dev/null +++ b/app/Core/DateParser.php @@ -0,0 +1,241 @@ += $start && $date <= $end; + } + + /** + * Get the total number of hours between 2 datetime objects + * Minutes are rounded to the nearest quarter + * + * @access public + * @param DateTime $d1 + * @param DateTime $d2 + * @return float + */ + public function getHours(DateTime $d1, DateTime $d2) + { + $seconds = $this->getRoundedSeconds(abs($d1->getTimestamp() - $d2->getTimestamp())); + return round($seconds / 3600, 2); + } + + /** + * Round the timestamp to the nearest quarter + * + * @access public + * @param integer $seconds Timestamp + * @return integer + */ + public function getRoundedSeconds($seconds) + { + return (int) round($seconds / (15 * 60)) * (15 * 60); + } + + /** + * Return a timestamp if the given date format is correct otherwise return 0 + * + * @access public + * @param string $value Date to parse + * @param string $format Date format + * @return integer + */ + public function getValidDate($value, $format) + { + $date = DateTime::createFromFormat($format, $value); + + if ($date !== false) { + $errors = DateTime::getLastErrors(); + if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) { + $timestamp = $date->getTimestamp(); + return $timestamp > 0 ? $timestamp : 0; + } + } + + return 0; + } + + /** + * Parse a date and return a unix timestamp, try different date formats + * + * @access public + * @param string $value Date to parse + * @return integer + */ + public function getTimestamp($value) + { + foreach ($this->getAllFormats() as $format) { + $timestamp = $this->getValidDate($value, $format); + + if ($timestamp !== 0) { + return $timestamp; + } + } + + return 0; + } + + /** + * Get ISO8601 date from user input + * + * @access public + * @param string $value Date to parse + * @return string + */ + public function getIsoDate($value) + { + return date('Y-m-d', ctype_digit($value) ? $value : $this->getTimestamp($value)); + } + + /** + * Get all combinations of date/time formats + * + * @access public + * @return string[] + */ + public function getAllFormats() + { + $formats = array(); + + foreach ($this->getDateFormats() as $date) { + foreach ($this->getTimeFormats() as $time) { + $formats[] = $date.' '.$time; + } + } + + return array_merge($formats, $this->getDateFormats()); + } + + /** + * Return the list of supported date formats (for the parser) + * + * @access public + * @return string[] + */ + public function getDateFormats() + { + return array( + $this->config->get('application_date_format', 'm/d/Y'), + 'Y-m-d', + 'Y_m_d', + ); + } + + /** + * Return the list of supported time formats (for the parser) + * + * @access public + * @return string[] + */ + public function getTimeFormats() + { + return array( + 'H:i', + 'g:i A', + 'g:iA', + ); + } + + /** + * Return the list of available date formats (for the config page) + * + * @access public + * @return array + */ + public function getAvailableFormats() + { + return array( + 'm/d/Y' => date('m/d/Y'), + 'd/m/Y' => date('d/m/Y'), + 'Y/m/d' => date('Y/m/d'), + 'd.m.Y' => date('d.m.Y'), + ); + } + + /** + * Remove the time from a timestamp + * + * @access public + * @param integer $timestamp Timestamp + * @return integer + */ + public function removeTimeFromTimestamp($timestamp) + { + return mktime(0, 0, 0, date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp)); + } + + /** + * Get a timetstamp from an ISO date format + * + * @access public + * @param string $date + * @return integer + */ + public function getTimestampFromIsoFormat($date) + { + return $this->removeTimeFromTimestamp(ctype_digit($date) ? $date : strtotime($date)); + } + + /** + * Format date (form display) + * + * @access public + * @param array $values Database values + * @param string[] $fields Date fields + * @param string $format Date format + */ + public function format(array &$values, array $fields, $format = '') + { + if ($format === '') { + $format = $this->config->get('application_date_format'); + } + + foreach ($fields as $field) { + if (! empty($values[$field])) { + $values[$field] = date($format, $values[$field]); + } + else { + $values[$field] = ''; + } + } + } + + /** + * Convert date (form input data) + * + * @access public + * @param array $values Database values + * @param string[] $fields Date fields + * @param boolean $keep_time Keep time or not + */ + public function convert(array &$values, array $fields, $keep_time = false) + { + foreach ($fields as $field) { + if (! empty($values[$field]) && ! is_numeric($values[$field])) { + $timestamp = $this->getTimestamp($values[$field]); + $values[$field] = $keep_time ? $timestamp : $this->removeTimeFromTimestamp($timestamp); + } + } + } +} diff --git a/app/Model/DateParser.php b/app/Model/DateParser.php deleted file mode 100644 index 8159341d..00000000 --- a/app/Model/DateParser.php +++ /dev/null @@ -1,241 +0,0 @@ -= $start && $date <= $end; - } - - /** - * Get the total number of hours between 2 datetime objects - * Minutes are rounded to the nearest quarter - * - * @access public - * @param DateTime $d1 - * @param DateTime $d2 - * @return float - */ - public function getHours(DateTime $d1, DateTime $d2) - { - $seconds = $this->getRoundedSeconds(abs($d1->getTimestamp() - $d2->getTimestamp())); - return round($seconds / 3600, 2); - } - - /** - * Round the timestamp to the nearest quarter - * - * @access public - * @param integer $seconds Timestamp - * @return integer - */ - public function getRoundedSeconds($seconds) - { - return (int) round($seconds / (15 * 60)) * (15 * 60); - } - - /** - * Return a timestamp if the given date format is correct otherwise return 0 - * - * @access public - * @param string $value Date to parse - * @param string $format Date format - * @return integer - */ - public function getValidDate($value, $format) - { - $date = DateTime::createFromFormat($format, $value); - - if ($date !== false) { - $errors = DateTime::getLastErrors(); - if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) { - $timestamp = $date->getTimestamp(); - return $timestamp > 0 ? $timestamp : 0; - } - } - - return 0; - } - - /** - * Parse a date and return a unix timestamp, try different date formats - * - * @access public - * @param string $value Date to parse - * @return integer - */ - public function getTimestamp($value) - { - foreach ($this->getAllFormats() as $format) { - $timestamp = $this->getValidDate($value, $format); - - if ($timestamp !== 0) { - return $timestamp; - } - } - - return 0; - } - - /** - * Get ISO8601 date from user input - * - * @access public - * @param string $value Date to parse - * @return string - */ - public function getIsoDate($value) - { - return date('Y-m-d', ctype_digit($value) ? $value : $this->getTimestamp($value)); - } - - /** - * Get all combinations of date/time formats - * - * @access public - * @return string[] - */ - public function getAllFormats() - { - $formats = array(); - - foreach ($this->getDateFormats() as $date) { - foreach ($this->getTimeFormats() as $time) { - $formats[] = $date.' '.$time; - } - } - - return array_merge($formats, $this->getDateFormats()); - } - - /** - * Return the list of supported date formats (for the parser) - * - * @access public - * @return string[] - */ - public function getDateFormats() - { - return array( - $this->config->get('application_date_format', 'm/d/Y'), - 'Y-m-d', - 'Y_m_d', - ); - } - - /** - * Return the list of supported time formats (for the parser) - * - * @access public - * @return string[] - */ - public function getTimeFormats() - { - return array( - 'H:i', - 'g:i A', - 'g:iA', - ); - } - - /** - * Return the list of available date formats (for the config page) - * - * @access public - * @return array - */ - public function getAvailableFormats() - { - return array( - 'm/d/Y' => date('m/d/Y'), - 'd/m/Y' => date('d/m/Y'), - 'Y/m/d' => date('Y/m/d'), - 'd.m.Y' => date('d.m.Y'), - ); - } - - /** - * Remove the time from a timestamp - * - * @access public - * @param integer $timestamp Timestamp - * @return integer - */ - public function removeTimeFromTimestamp($timestamp) - { - return mktime(0, 0, 0, date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp)); - } - - /** - * Get a timetstamp from an ISO date format - * - * @access public - * @param string $date - * @return integer - */ - public function getTimestampFromIsoFormat($date) - { - return $this->removeTimeFromTimestamp(ctype_digit($date) ? $date : strtotime($date)); - } - - /** - * Format date (form display) - * - * @access public - * @param array $values Database values - * @param string[] $fields Date fields - * @param string $format Date format - */ - public function format(array &$values, array $fields, $format = '') - { - if ($format === '') { - $format = $this->config->get('application_date_format'); - } - - foreach ($fields as $field) { - if (! empty($values[$field])) { - $values[$field] = date($format, $values[$field]); - } - else { - $values[$field] = ''; - } - } - } - - /** - * Convert date (form input data) - * - * @access public - * @param array $values Database values - * @param string[] $fields Date fields - * @param boolean $keep_time Keep time or not - */ - public function convert(array &$values, array $fields, $keep_time = false) - { - foreach ($fields as $field) { - if (! empty($values[$field]) && ! is_numeric($values[$field])) { - $timestamp = $this->getTimestamp($values[$field]); - $values[$field] = $keep_time ? $timestamp : $this->removeTimeFromTimestamp($timestamp); - } - } - } -} diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index 4acebfeb..9df2dede 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -29,7 +29,6 @@ class ClassProvider implements ServiceProviderInterface 'Config', 'Currency', 'CustomFilter', - 'DateParser', 'File', 'LastLogin', 'Link', @@ -79,6 +78,7 @@ class ClassProvider implements ServiceProviderInterface 'ProjectGanttFormatter', ), 'Core' => array( + 'DateParser', 'Helper', 'HttpClient', 'Lexer', diff --git a/tests/units/Core/DateParserTest.php b/tests/units/Core/DateParserTest.php new file mode 100644 index 00000000..0d345784 --- /dev/null +++ b/tests/units/Core/DateParserTest.php @@ -0,0 +1,84 @@ +container); + + $this->assertTrue($d->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00'))); + $this->assertFalse($d->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 16:00:00'), new DateTime('2015-03-14 17:00:00'))); + } + + public function testRoundSeconds() + { + $d = new DateParser($this->container); + $this->assertEquals('16:30', date('H:i', $d->getRoundedSeconds(strtotime('16:28')))); + $this->assertEquals('16:00', date('H:i', $d->getRoundedSeconds(strtotime('16:02')))); + $this->assertEquals('16:15', date('H:i', $d->getRoundedSeconds(strtotime('16:14')))); + $this->assertEquals('17:00', date('H:i', $d->getRoundedSeconds(strtotime('16:58')))); + } + + public function testGetHours() + { + $d = new DateParser($this->container); + + $this->assertEquals(1, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00'))); + $this->assertEquals(2.5, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:30:00'))); + $this->assertEquals(2.75, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:45:00'))); + $this->assertEquals(3, $d->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 17:58:00'))); + $this->assertEquals(3, $d->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 11:58:00'))); + } + + public function testValidDate() + { + $d = new DateParser($this->container); + + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('2014-03-05', 'Y-m-d'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('2014_03_05', 'Y_m_d'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('05/03/2014', 'd/m/Y'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('03/05/2014', 'm/d/Y'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('3/5/2014', 'm/d/Y'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('5/3/2014', 'd/m/Y'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('5/3/14', 'd/m/y'))); + $this->assertEquals(0, $d->getValidDate('5/3/14', 'd/m/Y')); + $this->assertEquals(0, $d->getValidDate('5-3-2014', 'd/m/Y')); + } + + public function testGetTimestamp() + { + $d = new DateParser($this->container); + + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('2014-03-05'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('2014_03_05'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('03/05/2014'))); + $this->assertEquals('2014-03-25 17:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18 pm'))); + $this->assertEquals('2014-03-25 05:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18 am'))); + $this->assertEquals('2014-03-25 05:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18am'))); + $this->assertEquals('2014-03-25 23:14', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 23:14'))); + $this->assertEquals('2014-03-29 23:14', date('Y-m-d H:i', $d->getTimestamp('2014_03_29 23:14'))); + $this->assertEquals('2014-03-29 23:14', date('Y-m-d H:i', $d->getTimestamp('2014-03-29 23:14'))); + } + + public function testConvert() + { + $d = new DateParser($this->container); + + $values = array( + 'date_due' => '2015-01-25', + 'date_started' => '2015_01_25', + ); + + $d->convert($values, array('date_due', 'date_started')); + + $this->assertEquals(mktime(0, 0, 0, 1, 25, 2015), $values['date_due']); + $this->assertEquals('2015-01-25', date('Y-m-d', $values['date_due'])); + + $this->assertEquals(mktime(0, 0, 0, 1, 25, 2015), $values['date_started']); + $this->assertEquals('2015-01-25', date('Y-m-d', $values['date_started'])); + } +} diff --git a/tests/units/Formatter/TaskFilterCalendarFormatterTest.php b/tests/units/Formatter/TaskFilterCalendarFormatterTest.php index d5ff532b..a42e865f 100644 --- a/tests/units/Formatter/TaskFilterCalendarFormatterTest.php +++ b/tests/units/Formatter/TaskFilterCalendarFormatterTest.php @@ -6,7 +6,7 @@ use Kanboard\Formatter\TaskFilterCalendarFormatter; use Kanboard\Model\Project; use Kanboard\Model\User; use Kanboard\Model\TaskCreation; -use Kanboard\Model\DateParser; +use Kanboard\Core\DateParser; use Kanboard\Model\Category; use Kanboard\Model\Subtask; use Kanboard\Model\Config; diff --git a/tests/units/Formatter/TaskFilterGanttFormatterTest.php b/tests/units/Formatter/TaskFilterGanttFormatterTest.php index 86815089..14804784 100644 --- a/tests/units/Formatter/TaskFilterGanttFormatterTest.php +++ b/tests/units/Formatter/TaskFilterGanttFormatterTest.php @@ -5,7 +5,7 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Formatter\TaskFilterGanttFormatter; use Kanboard\Model\Project; use Kanboard\Model\TaskCreation; -use Kanboard\Model\DateParser; +use Kanboard\Core\DateParser; class TaskFilterGanttFormatterTest extends Base { diff --git a/tests/units/Formatter/TaskFilterICalendarFormatterTest.php b/tests/units/Formatter/TaskFilterICalendarFormatterTest.php index cd5057d9..915cdda2 100644 --- a/tests/units/Formatter/TaskFilterICalendarFormatterTest.php +++ b/tests/units/Formatter/TaskFilterICalendarFormatterTest.php @@ -7,7 +7,7 @@ use Kanboard\Formatter\TaskFilterICalendarFormatter; use Kanboard\Model\Project; use Kanboard\Model\User; use Kanboard\Model\TaskCreation; -use Kanboard\Model\DateParser; +use Kanboard\Core\DateParser; use Kanboard\Model\Category; use Kanboard\Model\Subtask; use Kanboard\Model\Config; diff --git a/tests/units/Model/DateParserTest.php b/tests/units/Model/DateParserTest.php deleted file mode 100644 index 5d8d6ab1..00000000 --- a/tests/units/Model/DateParserTest.php +++ /dev/null @@ -1,84 +0,0 @@ -container); - - $this->assertTrue($d->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00'))); - $this->assertFalse($d->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 16:00:00'), new DateTime('2015-03-14 17:00:00'))); - } - - public function testRoundSeconds() - { - $d = new DateParser($this->container); - $this->assertEquals('16:30', date('H:i', $d->getRoundedSeconds(strtotime('16:28')))); - $this->assertEquals('16:00', date('H:i', $d->getRoundedSeconds(strtotime('16:02')))); - $this->assertEquals('16:15', date('H:i', $d->getRoundedSeconds(strtotime('16:14')))); - $this->assertEquals('17:00', date('H:i', $d->getRoundedSeconds(strtotime('16:58')))); - } - - public function testGetHours() - { - $d = new DateParser($this->container); - - $this->assertEquals(1, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00'))); - $this->assertEquals(2.5, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:30:00'))); - $this->assertEquals(2.75, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:45:00'))); - $this->assertEquals(3, $d->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 17:58:00'))); - $this->assertEquals(3, $d->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 11:58:00'))); - } - - public function testValidDate() - { - $d = new DateParser($this->container); - - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('2014-03-05', 'Y-m-d'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('2014_03_05', 'Y_m_d'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('05/03/2014', 'd/m/Y'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('03/05/2014', 'm/d/Y'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('3/5/2014', 'm/d/Y'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('5/3/2014', 'd/m/Y'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('5/3/14', 'd/m/y'))); - $this->assertEquals(0, $d->getValidDate('5/3/14', 'd/m/Y')); - $this->assertEquals(0, $d->getValidDate('5-3-2014', 'd/m/Y')); - } - - public function testGetTimestamp() - { - $d = new DateParser($this->container); - - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('2014-03-05'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('2014_03_05'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('03/05/2014'))); - $this->assertEquals('2014-03-25 17:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18 pm'))); - $this->assertEquals('2014-03-25 05:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18 am'))); - $this->assertEquals('2014-03-25 05:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18am'))); - $this->assertEquals('2014-03-25 23:14', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 23:14'))); - $this->assertEquals('2014-03-29 23:14', date('Y-m-d H:i', $d->getTimestamp('2014_03_29 23:14'))); - $this->assertEquals('2014-03-29 23:14', date('Y-m-d H:i', $d->getTimestamp('2014-03-29 23:14'))); - } - - public function testConvert() - { - $d = new DateParser($this->container); - - $values = array( - 'date_due' => '2015-01-25', - 'date_started' => '2015_01_25', - ); - - $d->convert($values, array('date_due', 'date_started')); - - $this->assertEquals(mktime(0, 0, 0, 1, 25, 2015), $values['date_due']); - $this->assertEquals('2015-01-25', date('Y-m-d', $values['date_due'])); - - $this->assertEquals(mktime(0, 0, 0, 1, 25, 2015), $values['date_started']); - $this->assertEquals('2015-01-25', date('Y-m-d', $values['date_started'])); - } -} diff --git a/tests/units/Model/TaskFilterTest.php b/tests/units/Model/TaskFilterTest.php index 20e71622..b668b7cc 100644 --- a/tests/units/Model/TaskFilterTest.php +++ b/tests/units/Model/TaskFilterTest.php @@ -6,7 +6,7 @@ use Kanboard\Model\Project; use Kanboard\Model\User; use Kanboard\Model\TaskFilter; use Kanboard\Model\TaskCreation; -use Kanboard\Model\DateParser; +use Kanboard\Core\DateParser; use Kanboard\Model\Category; use Kanboard\Model\Subtask; use Kanboard\Model\Config; -- cgit v1.2.3