Skip to content

Commit

Permalink
fix: fix error "date(): Argument #2 ($timestamp) must be of type ?int…
Browse files Browse the repository at this point in the history
…, string given"

fixes #19
  • Loading branch information
cgoIT committed Jul 15, 2024
1 parent 0c69189 commit 4a69df7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
31 changes: 31 additions & 0 deletions src/Classes/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,37 @@ public static function appendToArrayKey(&$arr, mixed $key, mixed $val): void
}
}

/**
* @param array<mixed> $arrFixedDates
*
* @return array<mixed>
*/
public static function sanitizeFixedDates(array $arrFixedDates): array
{
foreach ($arrFixedDates as $pos => &$fixedDate) {
foreach (['new_repeat', 'new_start', 'new_end'] as $col) {
$val = $fixedDate[$col];
if (!empty($val)) {
if (!\is_int($val)) {
$intVal = strtotime((string) $val);
if (false !== $intVal) {
$fixedDate[$col] = $intVal;
} else {
$fixedDate[$col] = 0;
}
}
} else {
if ('new_repeat' === $col) {
unset($arrFixedDates[$pos]);
continue 2;
}
}
}
}

return $arrFixedDates;
}

/**
* Implodes an array of fixed dates into a string.
*
Expand Down
32 changes: 2 additions & 30 deletions src/EventListener/DataContainer/CalendarEventsCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Cgoit\CalendarExtendedBundle\EventListener\DataContainer;

use Cgoit\CalendarExtendedBundle\Classes\Utils;
use Cgoit\CalendarExtendedBundle\Exception\CalendarExtendedException;
use Contao\Backend;
use Contao\CalendarEventsModel;
Expand Down Expand Up @@ -261,35 +262,6 @@ public function defaultRepeatEachExt(mixed $value, DataContainer $dc): mixed
return $value;
}

/**
* @param array<mixed> $arrFixedDates
*
* @return array<mixed>
*/
private function sanitizeFixedDates(array $arrFixedDates): array
{
foreach ($arrFixedDates as $pos => &$fixedDate) {
foreach (['new_repeat', 'new_start', 'new_end'] as $col) {
$val = $fixedDate[$col];
if (!empty($val)) {
if (!\is_int($val)) {
$intVal = strtotime((string) $val);
if (false !== $intVal) {
$fixedDate[$col] = $intVal;
}
}
} else {
if ('new_repeat' === $col) {
unset($arrFixedDates[$pos]);
continue 2;
}
}
}
}

return $arrFixedDates;
}

/**
* @param array<mixed> $arrAllRecurrences
* @param array<mixed> $maxRepeatEnd
Expand All @@ -308,7 +280,7 @@ private function getFixedDates(Result|\stdClass $activeRecord, array $arrAllRecu
return [null, $arrAllRecurrences, $maxRepeatEnd];
}

$arrFixedDates = $this->sanitizeFixedDates($arrFixedDates);
$arrFixedDates = Utils::sanitizeFixedDates($arrFixedDates);
usort(
$arrFixedDates,
static function ($a, $b) {
Expand Down
2 changes: 2 additions & 0 deletions src/Hook/GetAllEventsHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ private function handleFixedDatesRecurrences(array $arrEvents, array $arrCalenda
$arrFixedDates = StringUtil::deserialize($objEvent->repeatFixedDates);

if (!empty($arrFixedDates) && \is_array($arrFixedDates)) {
$arrFixedDates = Utils::sanitizeFixedDates($arrFixedDates);

foreach ($arrFixedDates as $date) {
$intStart = $date['new_repeat'];
$intEnd = $intStart;
Expand Down

0 comments on commit 4a69df7

Please sign in to comment.