<?php
 class vB_Api_Styleschedule extends vB_Api { private $library; protected function __construct() { $this->library = vB_Library::instance('styleschedule'); } public function getSchedule(int $scheduleid) : array { return [ 'schedule' => $this->library->getStyleSchedule($scheduleid) ]; } public function getAllSchedules() : array { return [ 'schedules' => $this->library->getStyleSchedules(), ]; } public function deleteSchedule(int $scheduleid) : array { $this->checkHasAdminPermission('canadminstyles'); $this->library->deleteSchedule($scheduleid); return ['success' => true]; } public function saveSchedule(mixed $data) : array { $return = $this->saveSchedulesBulk([$data]); return [ 'success' => $return['success'], 'scheduleid' => reset($return['scheduleids']), ]; } public function saveSchedulesBulk(array $schedules) : array { $this->checkHasAdminPermission('canadminstyles'); $scheduleInstances = []; foreach ($schedules AS $__key => $__data) { if (is_array($__data)) { $this->validateSingleScheduleArrayData($__data); $scheduleInstances[$__key] = vB_Entity_Styleschedule::createFromArray($__data); } else if (is_a($__data, 'vB_Entity_Styleschedule')) { $scheduleInstances[$__key] = $__data; } else { throw new vB_Exception_Api('invalid_data'); } } $scheduleids = $this->library->saveStyleSchedulesBulk($scheduleInstances); $notfalsecount = array_reduce($scheduleids, fn(mixed $carry, mixed $item) => $carry + ($item ? 1 : 0), 0); $success = (count($schedules) == $notfalsecount); return [ 'success' => $success, 'scheduleids' => $scheduleids, ]; } private function validateSingleScheduleArrayData($data) { $required = [ 'styleid', 'enabled', 'startdate', 'startdate_tzoffset', 'enddate', 'enddate_tzoffset', 'useyear', 'priority', 'overridechannelcustom', 'overrideusercustom', 'title', ]; foreach ($required AS $__key) { if (!isset($data[$__key])) { throw new vB_Exception_Api('required_field_x_missing_or_invalid', $__key); } } } } 