diff --git a/src/Constraint/ConstraintManager.php b/src/Constraint/ConstraintManager.php index da2bd83..7cbd1ac 100644 --- a/src/Constraint/ConstraintManager.php +++ b/src/Constraint/ConstraintManager.php @@ -26,9 +26,9 @@ public function __construct($constraints = array()) { */ public function getConstraints($constraint_class = NULL) { if ($constraint_class == NULL) { - return $this->constraint; + return $this->constraints; } else { - return $this->constraint[$constraint_class]; + return $this->constraints[$constraint_class]; } } diff --git a/src/Event/EventItemizer.php b/src/Event/EventItemizer.php index 744c0b6..ee360ba 100644 --- a/src/Event/EventItemizer.php +++ b/src/Event/EventItemizer.php @@ -216,7 +216,7 @@ private function createDayGranural($itemized = array()) { if ($this->event->isSameDay()) { if (!($this->event->getStartDate()->format('H:i') == '00:00' && $this->event->getEndDate()->format('H:i') == '23:59')) { - $itemized_same_day = $this->createHourlyGranular($start_date, $end_date->add(new \DateInterval('PT1M')), $interval, $start_date); + $itemized_same_day = $this->createHourlyGranular($start_date, $end_date->add(new \DateInterval('PT1M')), $interval); $itemized[EventItemizer::BAT_DAY][$sy][$sm]['d' . $sd] = -1; $itemized[EventItemizer::BAT_HOUR][$sy][$sm]['d' . $sd] = $itemized_same_day[EventItemizer::BAT_HOUR][$sy][$sm]['d' . $sd]; $itemized[EventItemizer::BAT_MINUTE][$sy][$sm]['d' . $sd] = $itemized_same_day[EventItemizer::BAT_MINUTE][$sy][$sm]['d' . $sd]; @@ -225,7 +225,7 @@ private function createDayGranural($itemized = array()) { else { // Deal with the start day unless it starts on midnight precisely at which point the whole day is booked if (!($this->event->getStartDate()->format('H:i') == '00:00')) { - $itemized_start = $this->createHourlyGranular($start_date, new \DateTime($start_date->format("Y-n-j 23:59:59")), $interval, $start_date); + $itemized_start = $this->createHourlyGranular($start_date, new \DateTime($start_date->format("Y-n-j 23:59:59")), $interval); $itemized[EventItemizer::BAT_DAY][$sy][$sm]['d' . $sd] = -1; $itemized[EventItemizer::BAT_HOUR][$sy][$sm]['d' . $sd] = $itemized_start[EventItemizer::BAT_HOUR][$sy][$sm]['d' . $sd]; $itemized[EventItemizer::BAT_MINUTE][$sy][$sm]['d' . $sd] = $itemized_start[EventItemizer::BAT_MINUTE][$sy][$sm]['d' . $sd]; @@ -242,7 +242,7 @@ private function createDayGranural($itemized = array()) { $itemized[EventItemizer::BAT_MINUTE][$ey][$em]['d' . $ed] = array(); } else { - $itemized_end = $this->createHourlyGranular(new \DateTime($end_date->format("Y-n-j 00:00:00")), $end_date->add(new \DateInterval('PT1M')), $interval, new \DateTime($end_date->format("Y-n-j 00:00:00"))); + $itemized_end = $this->createHourlyGranular(new \DateTime($end_date->format("Y-n-j 00:00:00")), $end_date->add(new \DateInterval('PT1M')), $interval); $itemized[EventItemizer::BAT_DAY][$ey][$em]['d' . $ed] = -1; $itemized[EventItemizer::BAT_HOUR][$ey][$em]['d' . $ed] = $itemized_end[EventItemizer::BAT_HOUR][$ey][$em]['d' . $ed]; $itemized[EventItemizer::BAT_MINUTE][$ey][$em]['d' . $ed] = $itemized_end[EventItemizer::BAT_MINUTE][$ey][$em]['d' . $ed]; @@ -259,28 +259,31 @@ private function createDayGranural($itemized = array()) { * @param \DateTime $start_date * @param \DateTime $end_date * @param \DateInterval $interval - * @param \DateTime $period_start * @return array */ - public function createHourlyGranular(\DateTime $start_date, \DateTime $end_date, \DateInterval $interval, \DateTime $period_start) { + public function createHourlyGranular(\DateTime $start_date, \DateTime $end_date, \DateInterval $interval) { $period = new \DatePeriod($start_date, $interval, $end_date); $itemized = array(); - $start_minute = (int)$period_start->format('i'); - $init = TRUE; + $start_minute = (int)$start_date->format('i'); $event_value = $this->event->getValue(); + $year = $start_date->format('Y'); + $month = $start_date->format('n'); + $day = $start_date->format('j'); + $hour = $start_date->format('G'); + $min = $start_date->format('i'); + foreach ($period as $minute) { - if ($init) { + // Re-calculate if we're at a day boundary. + if ($hour == 24) { $year = $minute->format('Y'); $month = $minute->format('n'); $day = $minute->format('j'); $hour = $minute->format('G'); $min = $minute->format('i'); - - $init = FALSE; } // Doing minutes so set the values in the minute array @@ -304,11 +307,6 @@ public function createHourlyGranular(\DateTime $start_date, \DateTime $end_date, $start_minute = 0; } - if ($hour == 24) { - // Re-calculate if we're at a day boundary. - $init = TRUE; - } - $min = str_pad($min, 2, 0, STR_PAD_LEFT); } diff --git a/src/Store/DrupalDBStore.php b/src/Store/DrupalDBStore.php index 01ea8f1..1d29116 100644 --- a/src/Store/DrupalDBStore.php +++ b/src/Store/DrupalDBStore.php @@ -7,6 +7,7 @@ namespace Roomify\Bat\Store; +use Roomify\Bat\Event\EventInterface; use Roomify\Bat\Event\Event; use Roomify\Bat\Event\EventItemizer; use Roomify\Bat\Store\SqlDBStore; @@ -70,12 +71,12 @@ public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_i } /** - * @param \Roomify\Bat\Event\Event $event + * @param \Roomify\Bat\Event\EventInterface $event * @param $granularity * * @return bool */ - public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) { + public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOURLY) { $stored = TRUE; $transaction = db_transaction(); @@ -118,7 +119,7 @@ public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) { } } - //If we have minutes write minutes + // If we have minutes write minutes foreach ($itemized[Event::BAT_MINUTE] as $year => $months) { foreach ($months as $month => $days) { foreach ($days as $day => $hours) { diff --git a/src/Store/SqlDBStore.php b/src/Store/SqlDBStore.php index 1b37f3e..c788460 100644 --- a/src/Store/SqlDBStore.php +++ b/src/Store/SqlDBStore.php @@ -113,7 +113,7 @@ public function buildQueries(\DateTime $start_date, \DateTime $end_date, $unit_i $queries[Event::BAT_MINUTE] = 'SELECT * FROM ' . $this->minute_table . ' WHERE '; // Create a mock event which we will use to determine how to query the database - $mock_event = new Event($start_date, $end_date, new Unit(0, 0, NULL), -10); + $mock_event = new Event($start_date, $end_date, new Unit(0, 0, array()), -10); // We don't need a granular event even if we are retrieving granular data - since we don't // know what the event break-down is going to be we need to get the full range of data from // days, hours and minutes. diff --git a/src/Store/SqlLiteDBStore.php b/src/Store/SqlLiteDBStore.php index a9ceace..b796626 100644 --- a/src/Store/SqlLiteDBStore.php +++ b/src/Store/SqlLiteDBStore.php @@ -7,6 +7,7 @@ namespace Roomify\Bat\Store; +use Roomify\Bat\Event\EventInterface; use Roomify\Bat\Event\Event; use Roomify\Bat\Event\EventItemizer; use Roomify\Bat\Store\SqlDBStore; @@ -78,12 +79,12 @@ public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_i } /** - * @param \Roomify\Bat\Event\Event $event + * @param \Roomify\Bat\Event\EventInterface $event * @param $granularity * * @return bool */ - public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) { + public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOURLY) { $stored = TRUE; // Get existing event data from db diff --git a/src/Store/StoreInterface.php b/src/Store/StoreInterface.php index 2cbc90a..3742b07 100644 --- a/src/Store/StoreInterface.php +++ b/src/Store/StoreInterface.php @@ -7,7 +7,7 @@ namespace Roomify\Bat\Store; -use Roomify\Bat\Event\Event; +use Roomify\Bat\Event\EventInterface; /** * A store is a place where event data is held. The purpose of separating these @@ -35,5 +35,6 @@ public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_i * * @return boolean */ - public function storeEvent(Event $event, $granularity); + public function storeEvent(EventInterface $event, $granularity); + } diff --git a/src/Valuator/AbstractValuator.php b/src/Valuator/AbstractValuator.php index 92c5ba5..91461be 100644 --- a/src/Valuator/AbstractValuator.php +++ b/src/Valuator/AbstractValuator.php @@ -76,10 +76,9 @@ public function setEndDate(\DateTime $end_date) { } /** - * @param \DateTime $end_date * @return \DateTime */ - public function getEndDate(\DateTime $end_date) { + public function getEndDate() { return $this->end_date; } diff --git a/tests/EventTest.php b/tests/EventTest.php index ef39cad..000326a 100644 --- a/tests/EventTest.php +++ b/tests/EventTest.php @@ -554,4 +554,24 @@ public function testEventItemizeEventOneDay() { $this->assertEquals($itemized[Event::BAT_DAY]['2016']['3']['d1'], '5'); } + public function testCreateHourlyGranular() { + $event_state = 5; + $start_date = new \DateTime('2016-01-30 12:12'); + $end_date = new \DateTime('2016-02-02 12:12'); + $unit = new Unit(1, 2, array()); + + $event = new Event($start_date, $end_date, $unit, $event_state); + + $itemizer = new EventItemizer($event); + $itemized = $itemizer->createHourlyGranular($start_date, $end_date, new \DateInterval('PT1M')); + + $this->assertEquals($itemized[Event::BAT_HOUR]['2016']['1']['d30']['h12'], '-1'); + + $this->assertEquals($itemized[Event::BAT_MINUTE]['2016']['1']['d30']['h12']['m12'], '5'); + + $this->assertEquals($itemized[Event::BAT_HOUR]['2016']['2']['d2']['h12'], '-1'); + + $this->assertEquals($itemized[Event::BAT_MINUTE]['2016']['2']['d2']['h12']['m11'], '5'); + } + }