<?php
 class vB_Entity_Styleschedule extends vB_Entity { use vB_Trait_NoSerialize; protected vB_Entity_Datetime $_vbstartdate; public string $startdate; public string $startdate_tzoffset; protected vB_Entity_Datetime $_vbenddate; public string $enddate; public string $enddate_tzoffset; public function __construct( public int $styleid, public bool $enabled, string $startdate, string $startdate_tzoffset, string $enddate, string $enddate_tzoffset, public bool $useyear, public int $priority, public bool $overridechannelcustom, public bool $overrideusercustom, public string $title, public ?int $scheduleid = null ) { try { $this->_vbstartdate = new vB_Entity_Datetime($startdate, $startdate_tzoffset, '00:00:00'); $this->startdate = $this->_vbstartdate->datetime_mysql; $this->startdate_tzoffset = $this->_vbstartdate->tzoffset_raw; } catch(Throwable $e) { throw new vB_Exception_Api('required_field_x_missing_or_invalid', 'startdate'); } try { $this->_vbenddate = new vB_Entity_Datetime($enddate, $enddate_tzoffset, '23:59:59'); $this->enddate = $this->_vbenddate->datetime_mysql; $this->enddate_tzoffset = $this->_vbenddate->tzoffset_raw; } catch(Throwable $e) { throw new vB_Exception_Api('required_field_x_missing_or_invalid', 'enddate'); } if ($this->useyear) { if ($this->_vbenddate->getUnixTimestamp(false) <= $this->_vbstartdate->getUnixTimestamp(true) ) { throw new vB_Exception_Api('required_field_x_missing_or_invalid', 'enddate'); } } else { } } protected function vbstartdate($value = null) { if (func_num_args() == 1) { $this->_vbstartdate = $value; } else { return $this->_vbstartdate; } } protected function vbenddate($value = null) { if (func_num_args() == 1) { $this->_vbenddate = $value; } else { return $this->_vbenddate; } } public static function createFromArray(array $dbrecord) : static { return new vB_Entity_Styleschedule( $dbrecord['styleid'], $dbrecord['enabled'], $dbrecord['startdate'], $dbrecord['startdate_tzoffset'], $dbrecord['enddate'], $dbrecord['enddate_tzoffset'], $dbrecord['useyear'], $dbrecord['priority'], $dbrecord['overridechannelcustom'], $dbrecord['overrideusercustom'], $dbrecord['title'], $dbrecord['scheduleid'] ?? null ); } public function getNearestStartAndEndUnixtimes(int $attimestamp) : array { $startVbDatetime = $this->_vbstartdate; $endVbDatetime = $this->_vbenddate; if ($this->useyear) { return [ 'startunixtime' => $startVbDatetime->getUnixTimestamp(true), 'endunixtime' => $endVbDatetime->getUnixTimestamp(false), ]; } $compareVbDatetime = new vB_Entity_Datetime('@' . $attimestamp, $this->startdate_tzoffset); $compareYear = $compareVbDatetime->year; $compareunixtime = $compareVbDatetime->getUnixTimestamp(true); $startIsLeap = $startVbDatetime->isLeapDateIgnoreYear(); $endIsLeap = $endVbDatetime->isLeapDateIgnoreYear(); $startIsLeap = $startVbDatetime->isLeapDateIgnoreYear(); $endIsLeap = $endVbDatetime->isLeapDateIgnoreYear(); if ($startIsLeap AND $endIsLeap) { if (!vB_Entity_Datetime::isLeapYear($compareYear)) { $nextYear = vB_Entity_Datetime::calculateNextLeapYear($compareYear); $range = $this->_getRange($nextYear, $startVbDatetime, $endVbDatetime); } else { $range = $this->_getRange($compareYear, $startVbDatetime, $endVbDatetime); $compare = $this->_compareRange($compareunixtime, $range); if ($compare === 1) { $nextYear = vB_Entity_Datetime::calculateNextLeapYear($compareYear); $range = $this->_getRange($nextYear, $startVbDatetime, $endVbDatetime); } } return [ 'startunixtime' => $range[0], 'endunixtime' => $range[1], ]; } $thisRange = $this->_getRange($compareYear, $startVbDatetime, $endVbDatetime); $compare = $this->_compareRange($compareunixtime, $thisRange); if ($compare === 0) { return [ 'startunixtime' => $thisRange[0], 'endunixtime' => $thisRange[1], ]; } else if ($compare === -1) { $prevRange = $this->_getRange($compareYear - 1, $startVbDatetime, $endVbDatetime); if ($this->_compareRange($compareunixtime, $prevRange) === 0) { return [ 'startunixtime' => $prevRange[0], 'endunixtime' => $prevRange[1], ]; } else { return [ 'startunixtime' => $thisRange[0], 'endunixtime' => $thisRange[1], ]; } } else { $nextRange = $this->_getRange($compareYear + 1, $startVbDatetime, $endVbDatetime); return [ 'startunixtime' => $nextRange[0], 'endunixtime' => $nextRange[1], ]; } } private function _compareRange($timestamp, $range) : int { if (count($range) != 2) { throw new vB_Exception_Api('invalid_data'); } $start = $range[0]; $end = $range[1]; if ($timestamp < $start) { return -1; } else if ($end < $timestamp) { return 1; } else { return 0; } } private function _getRange($year, $startVbDatetime, $endVbDatetime) : array { $startunixtime = $startVbDatetime->getUnixTimestampForYear($year, true); $endunixtime = $endVbDatetime->getUnixTimestampForYear($year, false); if ($endunixtime <= $startunixtime) { $endunixtime = $endVbDatetime->getUnixTimestampForYear($year + 1, false); } return [ $startunixtime, $endunixtime, ]; } } 