Skip to content

Commit

Permalink
Merge pull request #29 from GeoSot/ref/change-given-options
Browse files Browse the repository at this point in the history
ref: change input argument on render methods
  • Loading branch information
benhall14 authored Oct 30, 2024
2 parents 0c43ecb + b61b8dc commit 7c0ad13
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 55 deletions.
36 changes: 22 additions & 14 deletions html/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,90 +77,90 @@

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->draw(date('Y-1-1'), ''); ?>
<?php echo $calendar->draw(date('Y-1-1' )); ?>

<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->setLocale('it')->draw(date('Y-2-1'), 'pink'); ?>
<?php echo $calendar->setLocale('it')->render(['startDate'=>date('Y-2-1' ),'color'=>'pink']); ?>

<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->setLocale('fr')->draw(date('Y-3-1'), 'blue'); ?>
<?php echo $calendar->setLocale('fr')->render(['startDate'=>date('Y-3-1' ),'color'=>'blue']); ?>

<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->setLocale('es')->draw(date('Y-4-1'), 'orange'); ?>
<?php echo $calendar->setLocale('es')->render(['startDate'=>date('Y-4-1'), 'color'=>'orange']); ?>

<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->setLocale('el')->draw(date('Y-5-1'), 'purple'); ?>
<?php echo $calendar->setLocale('el')->render(['startDate'=>date('Y-5-1'), 'color'=>'purple']); ?>
<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->setLocale('en')->draw(date('Y-6-1'), 'yellow'); ?>
<?php echo $calendar->setLocale('en')->render(['startDate'=>date('Y-6-1'), 'color'=>'yellow']); ?>

<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->draw(date('Y-7-1'), 'green'); ?>
<?php echo $calendar->render(['startDate'=>date('Y-7-1') ,'color'=>'green']); ?>

<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->draw(date('Y-8-1'), 'grey'); ?>
<?php echo $calendar->render(['startDate'=>date('Y-8-1' ),'color'=>'grey']); ?>

<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->draw(date('Y-9-1'), 'pink'); ?>
<?php echo $calendar->render(['startDate'=>date('Y-9-1' ),'color'=>'pink']); ?>

<hr />

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->draw(date('Y-10-1'), 'blue'); ?>
<?php echo $calendar->render(['startDate'=>date('Y-10-1' ),'color'=>'blue']); ?>

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->draw(date('Y-11-1'), 'orange'); ?>
<?php echo $calendar->render(['startDate'=>date('Y-11-1'), 'color'=>'orange']); ?>

</div>

<div class="col-xs-12 col-sm-6 col-md-4">

<?php echo $calendar->draw(date('Y-12-1'), 'purple'); ?>
<?php echo $calendar->render(['startDate'=>date('Y-12-1'), 'color'=>'purple']); ?>

<hr />

Expand All @@ -176,7 +176,12 @@

<div class="col-xs-12">

<?php echo $calendar->useWeekView()->draw(date('Y-1-1'), 'blue'); ?>
<?php echo $calendar->useWeekView()->render([
'startDate'=>date('Y-1-1'),
'color'=>'blue',
'startTime' => '10:00',
'endTime' => '20:30'
]); ?>

</div>

Expand All @@ -188,7 +193,10 @@

<div class="col-xs-12">

<?php echo $calendar->useWeekView()->hideTuesdays()->setLocale('fr')->draw(date('Y-12-25'), 'green'); ?>
<?php echo $calendar->useWeekView()->hideTuesdays()->setLocale('fr')->render([
'startDate' => date('Y-12-25'),
'color' => 'green',
]); ?>

</div>

Expand Down
27 changes: 18 additions & 9 deletions src/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}
}
18 changes: 0 additions & 18 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
32 changes: 28 additions & 4 deletions src/Views/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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('<table class="calendar %s %s ">', $color, $this->config->table_classes);
$calendar .= sprintf('<table class="calendar %s %s ">', $this->options['color'], $this->config->table_classes);

$calendar .= $this->getHeader($startDate);

Expand Down Expand Up @@ -65,7 +76,7 @@ public function makeHiddenStyles(): string
return [] !== $hiddenDays ? sprintf('<style>%s</style>', $style) : '';
}

protected function getHeader(Carbon $startDate): string
protected function getHeader(CarbonInterface $startDate): string
{
$string = '<thead>';

Expand Down Expand Up @@ -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(),
];
}
}
4 changes: 3 additions & 1 deletion src/Views/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
49 changes: 40 additions & 9 deletions src/Views/Week.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = [
'<div class="weekly-calendar-container">',
'<table class="weekly-calendar calendar '.$color.' '.$this->config->table_classes.'">',
'<table class="weekly-calendar calendar '.$this->options['color'].' '.$this->config->table_classes.'">',
$this->makeHeader($carbonPeriod),
'<tbody>',
$this->renderBlocks($carbonPeriod),
Expand All @@ -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) {
Expand Down Expand Up @@ -119,7 +134,7 @@ protected function renderBlocks(CarbonPeriod $carbonPeriod): string
$content .= '<tr>';

$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 .= '<td class="cal-weekview-time-th"><div>'.$start_time.' - '.$end_time.'</div></td>';

Expand All @@ -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' : '';

Expand Down Expand Up @@ -179,4 +194,20 @@ protected function renderEvent(Event $event, CarbonInterface $dateTime): string

return '<div class="cal-weekview-event '.$classes.'">'.$eventSummary.'</div>';
}

/**
* @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',
];
}
}

0 comments on commit 7c0ad13

Please sign in to comment.