diff options
author | AlexKlimenkov <shurick.klimenkov@gmail.com> | 2016-03-14 16:41:20 +0300 |
---|---|---|
committer | AlexKlimenkov <shurick.klimenkov@gmail.com> | 2016-03-14 16:41:20 +0300 |
commit | 0bcea2320e4235e6cfc0bcf65b917e45845bd5fe (patch) | |
tree | 6f567231ec547e3a9987b45ab7352587766457b5 | |
parent | 0b2d678903c2cf2ae375e968850a5e741036bd95 (diff) | |
parent | 888ede15fc86db7001fa4d28bc2a686ed41315a3 (diff) | |
download | scheduler-helper-php-0bcea2320e4235e6cfc0bcf65b917e45845bd5fe.zip scheduler-helper-php-0bcea2320e4235e6cfc0bcf65b917e45845bd5fe.tar.gz scheduler-helper-php-0bcea2320e4235e6cfc0bcf65b917e45845bd5fe.tar.bz2 |
Merge pull request #18 from mperednya/master
Performance tweaks.
-rw-r--r-- | RecurringType.php | 11 | ||||
-rwxr-xr-x | SchedulerHelper.php | 54 | ||||
-rwxr-xr-x | SchedulerHelperDate.php | 48 |
3 files changed, 54 insertions, 59 deletions
diff --git a/RecurringType.php b/RecurringType.php index 9ad8ca5..96b062b 100644 --- a/RecurringType.php +++ b/RecurringType.php @@ -311,7 +311,8 @@ class RecurringType { //If recurring type has list of days, then get those days. $recurringWeekDays = $this->getWeekDaysListValue(); if($recurringWeekDays) { - for($i = 0; $i < count($recurringWeekDays); $i++) { + $daysCount = count($recurringWeekDays); + for($i = 0; $i < $daysCount; $i++) { $dayStep = $this->_getRecurringDayStep($dateStamp, $recurringWeekDays[$i]); array_push($recurringDays, SchedulerHelperDate::addDays($dateStamp, $dayStep)); } @@ -338,7 +339,10 @@ class RecurringType { */ public function getRecurringDates($intervalStartDateStamp, $intervalEndDateStamp, $countDates = NULL) { - if(!($this->getRecurringTypeStepValue() && $this->getRecurringTypeValue())) + $recurringTypeStep = $this->getRecurringTypeStepValue(); + $recType = $this->getRecurringTypeValue(); + + if(!($recType && $recType)) return false; //Correct interval by recurring interval. @@ -361,8 +365,7 @@ class RecurringType { $recurringDays = $this->_getRecurringDays($currentRecurringStartDateStamp); $recurringDates = array_merge($recurringDates, $recurringDays); - $recurringTypeStep = $this->getRecurringTypeStepValue(); - switch($this->getRecurringTypeValue()) { + switch($recType) { case self::REC_TYPE_DAY: $currentRecurringStartDateStamp = SchedulerHelperDate::addDays($currentRecurringStartDateStamp, $recurringTypeStep); break; diff --git a/SchedulerHelper.php b/SchedulerHelper.php index f3cfcf6..ef1dcec 100755 --- a/SchedulerHelper.php +++ b/SchedulerHelper.php @@ -243,7 +243,8 @@ class Helper extends DHelper implements IHelper private function _prepareSimpleEvents($events) { $resultData = array(); - for($i = 0; $i < count($events); $i++) + $evCount = count($events); + for($i = 0; $i < $evCount; $i++) { array_push($resultData, $this->_filterEventDataToResponse($events[$i])); } @@ -295,22 +296,11 @@ class Helper extends DHelper implements IHelper { $filteredEventData = array(); $fullEventData = array(); - foreach($eventDataArray as $dataKey => $dataValue) { - $mappedFieldsValues = array_flip($this->_mapped_fields); $fullEventData[$dataKey] = $dataValue; - - if( - ($this->_use_only_mapped_fields && array_key_exists($dataKey, $mappedFieldsValues)) - || !$this->_use_only_mapped_fields - ) { + if (!$this->_use_only_mapped_fields || array_key_exists($dataKey, array_flip($this->_mapped_fields))) $filteredEventData[$dataKey] = $dataValue; - } } - -// $this->_use_only_mapped_fields - -// var_dump($filteredEventData);exit; return array("filtered_event_data" => $filteredEventData, "full_event_data" => $fullEventData); } @@ -329,13 +319,17 @@ class Helper extends DHelper implements IHelper if(isset($recurringEventExceptionsData[$recurringEventData[$this->getIdFieldName()]])) $parentRecurringExceptions = $recurringEventExceptionsData[$recurringEventData[$this->getIdFieldName()]]; - for($i = 0; $i < count($recurringDatesStamps); $i++) { + $startField = $this->getStartDateFieldName(); + $lengthField = $this->getLengthFieldName(); + $endField = $this->getEndDateFieldName(); + $stampsCount = count($recurringDatesStamps); + for($i = 0; $i < $stampsCount; $i++) { $preparedEventData = $recurringEventData; $eventStartDateStamp = $recurringDatesStamps[$i]; - $preparedEventData[$this->getStartDateFieldName()] = date(SchedulerHelperDate::FORMAT_DEFAULT, $eventStartDateStamp); + $preparedEventData[$startField] = date(SchedulerHelperDate::FORMAT_DEFAULT, $eventStartDateStamp); - $eventEndDateStamp = $eventStartDateStamp + $recurringEventData[$this->getLengthFieldName()]; - $preparedEventData[$this->getEndDateFieldName()] = date(SchedulerHelperDate::FORMAT_DEFAULT, $eventEndDateStamp); + $eventEndDateStamp = $eventStartDateStamp + $recurringEventData[$lengthField]; + $preparedEventData[$endField] = date(SchedulerHelperDate::FORMAT_DEFAULT, $eventEndDateStamp); if(isset($parentRecurringExceptions[$eventStartDateStamp])) { $eventExceptionData = $parentRecurringExceptions[$eventStartDateStamp]; @@ -367,13 +361,18 @@ class Helper extends DHelper implements IHelper $intervalStartDateStamp = $this->getDateTimestamp($startDate); $intervalEndDateStamp = $this->getDateTimestamp($endDate); - for($i = 0; $i < count($recurringEvents); $i++) { + $recField = $this->getRecurringTypeFieldName(); + $startField = $this->getStartDateFieldName(); + $endField = $this->getEndDateFieldName(); + + $recCount = count($recurringEvents); + for($i = 0; $i < $recCount; $i++) { $eventData = $recurringEvents[$i]; //Parse recurring data format. - $recurringTypeData = $eventData[$this->getRecurringTypeFieldName()]; - $recurringStartDateStamp = $this->getDateTimestamp($eventData[$this->getStartDateFieldName()]); - $recurringEndDateStamp = $this->getDateTimestamp($eventData[$this->getEndDateFieldName()]); + $recurringTypeData = $eventData[$recField]; + $recurringStartDateStamp = $this->getDateTimestamp($eventData[$startField]); + $recurringEndDateStamp = $this->getDateTimestamp($eventData[$endField]); $recurringTypeObj = new RecurringType($recurringTypeData, $recurringStartDateStamp, $recurringEndDateStamp); //Get recurring dates by parsed format. @@ -391,18 +390,15 @@ class Helper extends DHelper implements IHelper //Leave events that belongs to interval. $resultData = array(); - for($i = 0; $i < count($eventsData); $i++) { + $evCount = count($eventsData); + for($i = 0; $i < $evCount; $i++) { $eventData = $eventsData[$i]; $fullEventData = $eventData["full_event_data"]; - $recurringStartDateStamp = $this->getDateTimestamp($fullEventData[$this->getStartDateFieldName()]); - $recurringEndDateStamp = $this->getDateTimestamp($fullEventData[$this->getEndDateFieldName()]); - - if( - ($recurringStartDateStamp <= $intervalEndDateStamp) && ($recurringEndDateStamp >= $intervalStartDateStamp) - ) { + $recurringStartDateStamp = $this->getDateTimestamp($fullEventData[$startField]); + $recurringEndDateStamp = $this->getDateTimestamp($fullEventData[$endField]); + if($recurringStartDateStamp <= $intervalEndDateStamp && $recurringEndDateStamp >= $intervalStartDateStamp) { array_push($resultData, $eventData["filtered_event_data"]); } - } return $resultData; diff --git a/SchedulerHelperDate.php b/SchedulerHelperDate.php index 3993738..89308cb 100755 --- a/SchedulerHelperDate.php +++ b/SchedulerHelperDate.php @@ -20,6 +20,15 @@ class SchedulerHelperDate "year" => "Y" ); + static $INTERVAL_UNITS = array( + "hour" => array("type" => "T", "val" => "H"), + "minute" => array("type" => "T", "val" => "M"), + "second" => array("type" => "T", "val" => "S"), + "month" => array("type" => "", "val" => "M"), + "day" => array("type" => "", "val" => "D"), + "year" => array("type" => "", "val" => "Y") + ); + static public function differenceBetweenDates($firstDateStamp, $secondDateStamp) { $firstDate = new DateTime(date(self::FORMAT_DEFAULT, $firstDateStamp)); $secondDate = new DateTime(date(self::FORMAT_DEFAULT, $secondDateStamp)); @@ -72,44 +81,31 @@ class SchedulerHelperDate } static public function addDate($timestamp, $unit, $count) { - $dateUnits = self::$DATE_UNITS; - $units = array( - $dateUnits["hour"], - $dateUnits["minute"], - $dateUnits["second"], - $dateUnits["month"], - $dateUnits["day"], - $dateUnits["year"] - ); - $args = array(); - - for($i = 0; $i < count($units); $i++){ - $time_part = $units[$i]; - $param = date($time_part, $timestamp); - if($unit == $time_part) - $param += $count; - - array_push($args, $param); - - } - - return call_user_func_array("mktime", $args); + $date = new DateTime(); + $date->setTimestamp($timestamp); + $absCount = $count >= 0 ? $count : abs($count); + $interval = new \DateInterval("P" . $unit["type"] . $absCount . $unit["val"]); + if ($count >= 0) + $date->add($interval); + else + $date->sub($interval); + return $date->getTimestamp(); } static public function addDays($timestamp, $count) { - return self::addDate($timestamp, self::$DATE_UNITS["day"], $count); + return self::addDate($timestamp, self::$INTERVAL_UNITS["day"], $count); } static public function addWeeks($timestamp, $count) { - return self::addDate($timestamp, self::$DATE_UNITS["day"], ($count * self::DAYS_IN_WEEK)); + return self::addDate($timestamp, self::$INTERVAL_UNITS["day"], ($count * self::DAYS_IN_WEEK)); } static public function addMonths($timestamp, $count) { - return self::addDate($timestamp, self::$DATE_UNITS["month"], $count); + return self::addDate($timestamp, self::$INTERVAL_UNITS["month"], $count); } static public function addYears($timestamp, $count) { - return self::addDate($timestamp, self::$DATE_UNITS["year"], $count); + return self::addDate($timestamp, self::$INTERVAL_UNITS["year"], $count); } }
\ No newline at end of file |