year = $year; $this->month = $month; $this->fillDays($posts); } /** * @param $posts * @throws \Exception */ protected function fillDays($posts) { $start = DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2))); $interval = new DateInterval('P1D'); $end = clone $start; $end->add(new DateInterval('P1M')); $period = new DatePeriod($start, $interval, $end); foreach ($period as $dt) { $post = null; if (count($posts) > 0 && $posts->first()->date_post->format('Ymd') === $dt->format('Ymd')) { $post = $posts->shift(); } $this->days[] = new Day($dt, $post); } } /** * @return string */ public function getDate() { return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2))) ->format('F Y'); } /** * @return Day[] */ public function getDays(): array { return $this->days; } /** * @return DateTime|false */ public function getPrevious() { return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2))) ->modify('-1 month'); } /** * @return DateTime|false */ public function getNext() { return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2))) ->modify('+1 month'); } /** * @return string */ public function render(): string { $content = ''; return $content; } /** * @param Day $day * @return string */ private function _showDay(Day $day): string { $currentDate = $day->getDate()->format('Ymd'); $day_of_week = $day->getDate()->format('N'); $day_number = $day->getDate()->format('d'); $post = $day->getPost(); $classes = ['day']; if ($currentDate === date('Ymd')) { $classes[] = 'today'; } if ($day_of_week % 7 === 1) { $classes[] = 'start'; } if ($day_of_week % 7 === 0) { $classes[] = 'end'; } if ($post instanceof Post) { $classes[] = 'has-post'; } return '
  • ' . $day_number . '
  • '; } }