* @since 1.0.0 */ public $events = []; /** * Constructor. * * @since 1.0.0 */ public function __construct() { $this->createdAt = new \DateTimeImmutable('now'); $this->date = new SmartDateTime('now'); } /** * @param Event $event Calendar event * * @return int * * @since 1.0.0 */ public function addEvent(Event $event) : int { $this->events[] = $event; \end($this->events); $key = \key($this->events); \reset($this->events); return $key; } /** * @return Event[] * * @since 1.0.0 */ public function getEvents() : array { return $this->events; } /** * @param int $id Event id * * @return bool * * @since 1.0.0 */ public function removeEvent(int $id) : bool { if (isset($this->events[$id])) { unset($this->events[$id]); return true; } return false; } /** * @param int $id Event id * * @return Event * * @since 1.0.0 */ public function getEvent(int $id) : Event { return $this->events[$id] ?? new NullEvent(); } /** * Get event by date * * @param \DateTime $date Date of the event * * @return Event[] * * @since 1.0.0 */ public function getEventsOnDate(\DateTime $date) : array { $events = []; foreach ($this->events as $event) { if ($event->schedule->start !== null && $event->schedule->start->format('Y-m-d') === $date->format('Y-m-d') ) { $events[] = $event; } } return $events; } /** * Has event on date * * @param \DateTime $date Date of the event * * @return bool * * @since 1.0.0 */ public function hasEventOnDate(\DateTime $date) : bool { foreach ($this->events as $event) { if ($event->schedule->start !== null && $event->schedule->start->format('Y-m-d') === $date->format('Y-m-d') ) { return true; } } return false; } /** * {@inheritdoc} */ public function toArray() : array { return [ 'id' => $this->id, 'name' => $this->name, 'description' => $this->description, 'createdAt' => $this->createdAt, ]; } /** * {@inheritdoc} */ public function jsonSerialize() : mixed { return $this->toArray(); } }