- draw(date('Y-9-1'), 'pink'); ?>
+ render(['startDate'=>date('Y-9-1' ),'color'=>'pink']); ?>
- draw(date('Y-10-1'), 'blue'); ?>
+ render(['startDate'=>date('Y-10-1' ),'color'=>'blue']); ?>
- draw(date('Y-11-1'), 'orange'); ?>
+ render(['startDate'=>date('Y-11-1'), 'color'=>'orange']); ?>
- draw(date('Y-12-1'), 'purple'); ?>
+ render(['startDate'=>date('Y-12-1'), 'color'=>'purple']); ?>
@@ -176,7 +176,12 @@
- useWeekView()->draw(date('Y-1-1'), 'blue'); ?>
+ useWeekView()->render([
+ 'startDate'=>date('Y-1-1'),
+ 'color'=>'blue',
+ 'startTime' => '10:00',
+ 'endTime' => '20:30'
+ ]); ?>
@@ -188,7 +193,10 @@
- useWeekView()->hideTuesdays()->setLocale('fr')->draw(date('Y-12-25'), 'green'); ?>
+ useWeekView()->hideTuesdays()->setLocale('fr')->render([
+ 'startDate' => date('Y-12-25'),
+ 'color' => 'green',
+ ]); ?>
diff --git a/src/Calendar.php b/src/Calendar.php
index c4edf31..a37d585 100644
--- a/src/Calendar.php
+++ b/src/Calendar.php
@@ -219,30 +219,37 @@ public function getEvents(): array
/**
* Returns the calendar as a month view.
+ *
+ * @param array{ color?:string, startDate?: string|DateTimeInterface } $options
*/
- public function asMonthView(DateTimeInterface|string|null $startDate = null, string $color = ''): string
+ public function asMonthView(array $options): string
{
- return (new Month($this->config, $this))->render($startDate, $color);
+ return (new Month($this->config, $this))->render($options);
}
/**
* Returns the calendar as a month view.
+ *
+ * @param array{ color?:string, startDate?: string|DateTimeInterface, timeInterval?:int,endTime?:string, startTime?:string } $options
*/
- public function asWeekView(DateTimeInterface|string|null $startDate = null, string $color = ''): string
+ public function asWeekView(array $options): string
{
- return (new Week($this->config, $this))->render($startDate, $color);
+ return (new Week($this->config, $this))->render($options);
}
/**
* Draw the calendar and return HTML output.
*
- * @param string|DateTimeInterface|null $date the date of this calendar
- *
* @return string The calendar
*/
public function draw(DateTimeInterface|string|null $date = null, string $color = ''): string
{
- return 'week' === $this->config->type ? $this->asWeekView($date, $color) : $this->asMonthView($date, $color);
+ $options = [
+ 'color' => $color,
+ 'startDate' => $date,
+ ];
+
+ return 'week' === $this->config->type ? $this->asWeekView($options) : $this->asMonthView($options);
}
/**
@@ -264,10 +271,12 @@ public function stylesheet(bool $print = true): ?string
/**
* Shortcut helper to print the calendar output.
+ *
+ * @param array{color?: string, startDate?: (string|DateTimeInterface), timeInterval?: int, endTime?: string, startTime?: string} $options
*/
- public function display(string|DateTimeInterface|null $date = null, string $color = ''): void
+ public function render(array $options): void
{
echo $this->stylesheet();
- echo $this->draw($date, $color);
+ echo 'week' === $this->config->type ? $this->asWeekView($options) : $this->asMonthView($options);
}
}
diff --git a/src/Config.php b/src/Config.php
index d44598a..f92169d 100644
--- a/src/Config.php
+++ b/src/Config.php
@@ -16,24 +16,6 @@ class Config
*/
public string $type = 'month';
- /**
- * Time Interval used in the week view.
- * Default is set to 30 minutes.
- */
- public int $time_interval = 30;
-
- /**
- * The Week View Starting Time.
- * Leave at 00:00 for a full 24-hour calendar.
- */
- public string $start_time = '00:00';
-
- /**
- * The Week View end time.
- * Leave at 00:00 for a full 24-hour calendar.
- */
- public string $end_time = '00:00';
-
/**
* The Day Format.
*/
diff --git a/src/Views/Month.php b/src/Views/Month.php
index 6cba354..d8cc470 100644
--- a/src/Views/Month.php
+++ b/src/Views/Month.php
@@ -11,6 +11,14 @@
class Month extends View
{
+ /**
+ * @var array{color: string, startDate: (string|CarbonInterface)}
+ */
+ private array $options = [
+ 'color' => '',
+ 'startDate' => '',
+ ];
+
protected function findEvents(CarbonInterface $start, CarbonInterface $end): array
{
// Extracting and comparing only the dates (Y-m-d) to avoid time-based exclusion
@@ -22,14 +30,17 @@ protected function findEvents(CarbonInterface $start, CarbonInterface $end): arr
/**
* Returns the calendar as a month view.
+ *
+ * @param array{color?: string, startDate?: (string|DateTimeInterface)} $options
*/
- public function render(DateTimeInterface|string|null $startDate = null, string $color = ''): string
+ public function render(array $options): string
{
+ $this->options = $this->initializeOptions($options);
$calendar = $this->makeHiddenStyles();
- $startDate = Carbon::parse($startDate)->firstOfMonth();
+ $startDate = $this->options['startDate'];
- $calendar .= sprintf('
', $color, $this->config->table_classes);
+ $calendar .= sprintf('', $this->options['color'], $this->config->table_classes);
$calendar .= $this->getHeader($startDate);
@@ -65,7 +76,7 @@ public function makeHiddenStyles(): string
return [] !== $hiddenDays ? sprintf('', $style) : '';
}
- protected function getHeader(Carbon $startDate): string
+ protected function getHeader(CarbonInterface $startDate): string
{
$string = '';
@@ -176,4 +187,17 @@ protected function renderDay(CarbonInterface $runningDay): string
return $string.$dayRender;
}
+
+ /**
+ * @param array{color?: string, startDate?: (string|DateTimeInterface)} $options
+ *
+ * @return array{color: string, startDate: (string|CarbonInterface)}
+ */
+ public function initializeOptions(array $options): array
+ {
+ return [
+ 'color' => $options['color'] ?? '',
+ 'startDate' => Carbon::parse($options['startDate'] ?? null)->firstOfMonth(),
+ ];
+ }
}
diff --git a/src/Views/View.php b/src/Views/View.php
index 1deab27..3cac4c2 100644
--- a/src/Views/View.php
+++ b/src/Views/View.php
@@ -27,6 +27,8 @@ abstract protected function findEvents(CarbonInterface $start, CarbonInterface $
/**
* Returns the calendar as a month view.
+ *
+ * @param array{color?: string, startDate?: (string|DateTimeInterface)} $options
*/
- abstract public function render(DateTimeInterface|string|null $startDate = null, string $color = ''): string;
+ abstract public function render(array $options): string;
}
diff --git a/src/Views/Week.php b/src/Views/Week.php
index 7689c65..2447034 100644
--- a/src/Views/Week.php
+++ b/src/Views/Week.php
@@ -18,6 +18,17 @@ class Week extends View
*/
protected array $usedEvents = [];
+ /**
+ * @var array{color: string, startDate: (string|CarbonInterface), timeInterval: int, endTime: string, startTime: string}
+ */
+ private array $options = [
+ 'color' => '',
+ 'startDate' => '',
+ 'timeInterval' => 0,
+ 'startTime' => '',
+ 'endTime' => '',
+ ];
+
protected function findEvents(CarbonInterface $start, CarbonInterface $end): array
{
$callback = fn (Event $event): bool => $event->start->betweenIncluded($start, $end)
@@ -29,14 +40,18 @@ protected function findEvents(CarbonInterface $start, CarbonInterface $end): arr
/**
* Returns the calendar output as a week view.
+ *
+ * @param array{color?: string, startDate?: (string|DateTimeInterface), timeInterval?: int, endTime?: string, startTime?: string} $options
*/
- public function render(DateTimeInterface|string|null $startDate = null, string $color = ''): string
+ public function render(array $options): string
{
- $startDate = $this->sanitizeStartDate(Carbon::parse($startDate));
+ $this->options = $this->initializeOptions($options);
+
+ $startDate = $this->sanitizeStartDate($this->options['startDate']);
$carbonPeriod = $startDate->locale($this->config->locale)->toPeriod(7);
$calendar = [
'',
- '
',
+ '',
$this->makeHeader($carbonPeriod),
'',
$this->renderBlocks($carbonPeriod),
@@ -55,14 +70,14 @@ public function render(DateTimeInterface|string|null $startDate = null, string $
*/
protected function getTimes(): array
{
- $start_time = Carbon::createFromFormat('H:i', $this->config->start_time);
- $end_time = Carbon::createFromFormat('H:i', $this->config->end_time);
+ $start_time = Carbon::createFromFormat('H:i', $this->options['startTime']);
+ $end_time = Carbon::createFromFormat('H:i', $this->options['endTime']);
if ($start_time->equalTo($end_time)) {
$end_time->addDay();
}
- $carbonPeriod = CarbonInterval::minutes($this->config->time_interval)->toPeriod($this->config->start_time,
- $end_time);
+ $carbonPeriod = CarbonInterval::minutes($this->options['timeInterval'])
+ ->toPeriod($this->options['startTime'], $end_time);
$times = [];
foreach ($carbonPeriod->toArray() as $carbon) {
@@ -119,7 +134,7 @@ protected function renderBlocks(CarbonPeriod $carbonPeriod): string
$content .= '';
$start_time = $time;
- $end_time = date('H:i', strtotime($time.' + '.$this->config->time_interval.' minutes'));
+ $end_time = date('H:i', strtotime($time.' + '.$this->options['timeInterval'].' minutes'));
$content .= ''.$start_time.' - '.$end_time.' | ';
@@ -130,7 +145,7 @@ protected function renderBlocks(CarbonPeriod $carbonPeriod): string
$datetime = $carbon->setTimeFrom($time);
- $events = $this->findEvents($datetime, $datetime->clone()->addMinutes($this->config->time_interval));
+ $events = $this->findEvents($datetime, $datetime->clone()->addMinutes($this->options['timeInterval']));
$today_class = $carbon->isSameHour($today) ? ' today' : '';
@@ -179,4 +194,20 @@ protected function renderEvent(Event $event, CarbonInterface $dateTime): string
return ''.$eventSummary.'
';
}
+
+ /**
+ * @param array{color?: string, startDate?: (string|DateTimeInterface), timeInterval?: int, endTime?: string, startTime?: string} $options
+ *
+ * @return array{color: string, startDate: (string|CarbonInterface), timeInterval: int, endTime: string, startTime: string}
+ */
+ public function initializeOptions(array $options): array
+ {
+ return [
+ 'color' => $options['color'] ?? '',
+ 'startDate' => $this->sanitizeStartDate(Carbon::parse($options['startDate'] ?? null)),
+ 'timeInterval' => $options['timeInterval'] ?? 30,
+ 'startTime' => $options['startTime'] ?? '00:00',
+ 'endTime' => $options['endTime'] ?? '00:00',
+ ];
+ }
}