checkData($data); $this->id = isset($data[self::ID]) ? $data[self::ID] : 1; $this->status = $data[self::STATUS]; if (isset($data[self::START_TIME])) { $this->startTime = $this->getDateTime($data[self::START_TIME]); } else { $this->startTime = new \DateTime(); } if (isset($data[self::END_TIME])) { $this->endTime = $this->getDateTime($data[self::END_TIME]); } } /** * Return the Maintenance state as array, Datetime are converted to timestamp * * @return array */ public function toArray() { $data = [ self::ID => $this->id, self::STATUS => $this->status, self::START_TIME => $this->startTime->getTimestamp(), ]; if (! is_null($this->endTime)) { $data[self::END_TIME] = $this->endTime->getTimestamp(); } return $data; } /** * @return int|mixed */ public function getId() { return $this->id; } /** * @param $id */ public function setId($id) { $this->id = $id; } /** * @param $endTime */ public function setEndTime($endTime) { $this->endTime = $this->getDateTime($endTime); } /** * @return \DateTime */ public function getStartTime() { return $this->startTime; } /** * @return bool */ public function getBooleanStatus() { return $this->status === self::LIVE_MODE ? true : false; } /** * @return \DateInterval */ public function getDuration() { $endTime = $this->endTime ?: new \DateTime(); return $this->startTime->diff($endTime); } /** * Transform a string|Datetime to Datetime * * @param $dateTime * @return \DateTime * @throws \common_Exception */ protected function getDateTime($dateTime) { if ($dateTime instanceof \DateTime) { return $dateTime; } if ((is_string($dateTime) && (int) $dateTime > 0) || is_numeric($dateTime)) { return (new \DateTime())->setTimestamp($dateTime); } throw new \common_Exception(__('A date has to be a Datetime or timestamp')); } /** * Check data of constructor input * * @param array $data * @throws \common_Exception */ protected function checkData(array $data) { if (! isset($data[self::STATUS]) || ! in_array($data[self::STATUS], self::$availableStatus)) { throw new \common_Exception( __('A maintenance status must have a STATUS: "%s" or "%s"', self::LIVE_MODE, self::OFFLINE_MODE) ); } } }