Skip to content

Commit

Permalink
Use method extraction, fix issues reported by Psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Jan 26, 2024
1 parent 33cbd2f commit 1ffc15a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ echo $link->webOffice();
// Generate a data URI for an ics file (for iCal & Outlook)
echo $link->ics();
echo $link->ics(['URL' => 'https://my-page.com', 'UID' => 'custom-id']); //
echo $link->ics([], ['format' => 'file']); // e.g. to attach ics as a file to an email.

// Generate a data uri for an ics file with default reminder (for iCal & Outlook)
// Generate a data uri for an ics file with the default reminder (for iCal & Outlook)
echo $link->ics(['REMINDER' => []]);

// Generate a data uri for an ics file with a custom reminder (for iCal & Outlook)
echo $link->ics(['REMINDER' => ['DESCRIPTION' => 'Remind me', 'TIME' => DateTime::createFromFormat('Y-m-d H:i', '2018-02-01 08:15', new DateTimeZone('UTC'))]]);
echo $link->ics(['REMINDER' => ['DESCRIPTION' => 'Remind me', 'TIME' => 1706264894]]);
echo $link->ics([], ['format' => 'file']); // e.g. to attach ics as a file to an email.

// Generate a data URI using arbitrary generator:
echo $link->formatWith(new \Your\Generator());
Expand Down
50 changes: 32 additions & 18 deletions src/Generators/Ics.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @see https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/
* @psalm-type IcsOptions = array{UID?: string, URL?: string, REMINDER?: array{DESCRIPTION?: string, TIME?: \DateTimeInterface}}
*/
class Ics implements Generator
{
Expand All @@ -15,17 +16,18 @@ class Ics implements Generator

/** @var string {@see https://www.php.net/manual/en/function.date.php} */
protected $dateFormat = 'Ymd';

/** @var string */
protected $dateTimeFormat = 'Ymd\THis\Z';

/** @var array<non-empty-string, non-empty-string> */
/** @var IcsOptions */
protected $options = [];

/** @var array{format?: self::FORMAT_*} */
protected $presentationOptions = [];

/**
* @param array<non-empty-string, non-empty-string> $options Optional ICS properties and components
* @param IcsOptions $options Optional ICS properties and components
* @param array{format?: self::FORMAT_*} $presentationOptions
*/
public function __construct(array $options = [], array $presentationOptions = [])
Expand Down Expand Up @@ -69,22 +71,8 @@ public function generate(Link $link): string
$url[] = 'URL;VALUE=URI:'.$this->options['URL'];
}

if (isset($this->options['REMINDER'])) {
$description = 'Reminder: '.$this->escapeString($link->title);
if (isset($this->options['REMINDER']['DESCRIPTION'])) {
$description = $this->escapeString($this->options['REMINDER']['DESCRIPTION']);
}

$trigger = '-PT15M';
if (isset($this->options[ 'REMINDER'][ 'TIME'])) {
$trigger = 'VALUE=DATE-TIME:'.gmdate($dateTimeFormat, $this->options[ 'REMINDER'][ 'TIME']->getTimestamp());
}

$url[] = 'BEGIN:VALARM';
$url[] = 'ACTION:DISPLAY';
$url[] = 'DESCRIPTION:'.$description;
$url[] = 'TRIGGER:'.$trigger;
$url[] = 'END:VALARM';
if (is_array($this->options['REMINDER'] ?? null)) {
$url = [...$url, ...$this->generateAlertComponent($link)];
}

$url[] = 'END:VEVENT';
Expand Down Expand Up @@ -125,4 +113,30 @@ protected function generateEventUid(Link $link): string
$link->address
));
}

/**
* @param \Spatie\CalendarLinks\Link $link
* @return list<string>
*/
private function generateAlertComponent(Link $link): array
{
$description = $this->options['REMINDER']['DESCRIPTION'] ?? null;
if (! is_string($description)) {
$description = 'Reminder: '.$this->escapeString($link->title);
}

$trigger = '-PT15M';
if (($reminderTime = $this->options['REMINDER']['TIME'] ?? null) instanceof \DateTimeInterface) {
$trigger = 'VALUE=DATE-TIME:'.gmdate($this->dateTimeFormat, $reminderTime->getTimestamp());
}

$alarmComponent = [];
$alarmComponent[] = 'BEGIN:VALARM';
$alarmComponent[] = 'ACTION:DISPLAY';
$alarmComponent[] = 'DESCRIPTION:'.$description;
$alarmComponent[] = 'TRIGGER:'.$trigger;
$alarmComponent[] = 'END:VALARM';

return $alarmComponent;
}
}
2 changes: 2 additions & 0 deletions src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @property-read string $description
* @property-read string $address
* @property-read bool $allDay
* @psalm-import-type IcsOptions from \Spatie\CalendarLinks\Generators\Ics
*/
class Link
{
Expand Down Expand Up @@ -128,6 +129,7 @@ public function google(): string
}

/**
* @psalm-param IcsOptions $options ICS specific properties and components
* @param array<non-empty-string, non-empty-string> $options ICS specific properties and components
* @param array{format?: \Spatie\CalendarLinks\Generators\Ics::FORMAT_*} $presentationOptions
* @return string
Expand Down

0 comments on commit 1ffc15a

Please sign in to comment.