From 28d5971d23f96b1308ac27f8daffecfe290f0f14 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Fri, 26 Jan 2024 15:01:10 +0530 Subject: [PATCH] Add consts, improve examples --- README.md | 4 +++- src/Generators/Ics.php | 9 ++++++--- src/Link.php | 2 +- tests/Generators/IcsGeneratorTest.php | 10 +++++----- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 37067c7..5df1984 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,10 @@ echo $link->webOutlook(); // Generate a link to create an event on outlook.office.com calendar echo $link->webOffice(); -// Generate a data uri for an ics file (for iCal & Outlook) +// 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 using arbitrary generator: echo $link->formatWith(new \Your\Generator()); diff --git a/src/Generators/Ics.php b/src/Generators/Ics.php index a6a8b2e..f4b33de 100644 --- a/src/Generators/Ics.php +++ b/src/Generators/Ics.php @@ -10,6 +10,9 @@ */ class Ics implements Generator { + public const FORMAT_HTML = 'html'; + public const FORMAT_FILE = 'file'; + /** @var string {@see https://www.php.net/manual/en/function.date.php} */ protected $dateFormat = 'Ymd'; /** @var string */ @@ -18,12 +21,12 @@ class Ics implements Generator /** @var array */ protected $options = []; - /** @var array{format?: string} */ + /** @var array{format?: self::FORMAT_*} */ protected $presentationOptions = []; /** * @param array $options Optional ICS properties and components - * @param array{format?: string} $presentationOptions + * @param array{format?: self::FORMAT_*} $presentationOptions */ public function __construct(array $options = [], array $presentationOptions = []) { @@ -69,7 +72,7 @@ public function generate(Link $link): string $url[] = 'END:VEVENT'; $url[] = 'END:VCALENDAR'; - $format = $this->presentationOptions['format'] ?? 'html'; + $format = $this->presentationOptions['format'] ?? self::FORMAT_HTML; return match ($format) { 'file' => $this->buildFile($url), diff --git a/src/Link.php b/src/Link.php index 60e9dee..103b81e 100644 --- a/src/Link.php +++ b/src/Link.php @@ -116,7 +116,7 @@ public function google(): string /** * @param array $options ICS specific properties and components - * @param array{format?: string} $presentationOptions + * @param array{format?: \Spatie\CalendarLinks\Generators\Ics::FORMAT_*} $presentationOptions * @return string */ public function ics(array $options = [], array $presentationOptions = []): string diff --git a/tests/Generators/IcsGeneratorTest.php b/tests/Generators/IcsGeneratorTest.php index 3e025db..0c43cad 100644 --- a/tests/Generators/IcsGeneratorTest.php +++ b/tests/Generators/IcsGeneratorTest.php @@ -16,7 +16,7 @@ class IcsGeneratorTest extends TestCase */ protected function generator(array $options = [], array $presentationOptions = []): Generator { - $presentationOptions['format'] ??= 'file'; + $presentationOptions['format'] ??= Ics::FORMAT_FILE; return new Ics($options, $presentationOptions); } @@ -30,7 +30,7 @@ protected function linkMethodName(): string public function it_can_generate_an_ics_link_with_custom_uid(): void { $this->assertMatchesSnapshot( - $this->generator(['UID' => 'random-uid', ['format' => 'file']])->generate($this->createShortEventLink()) + $this->generator(['UID' => 'random-uid', ['format' => Ics::FORMAT_FILE]])->generate($this->createShortEventLink()) ); } @@ -38,7 +38,7 @@ public function it_can_generate_an_ics_link_with_custom_uid(): void public function it_has_a_product_id(): void { $this->assertMatchesSnapshot( - $this->generator(['PRODID' => 'Spatie calendar-links'], ['format' => 'file'])->generate($this->createShortEventLink()) + $this->generator(['PRODID' => 'Spatie calendar-links'], ['format' => Ics::FORMAT_FILE])->generate($this->createShortEventLink()) ); } @@ -46,7 +46,7 @@ public function it_has_a_product_id(): void public function it_has_a_product_dtstamp(): void { $this->assertMatchesSnapshot( - $this->generator(['DTSTAMP' => '20180201T090000Z'], ['format' => 'file'])->generate($this->createShortEventLink()) + $this->generator(['DTSTAMP' => '20180201T090000Z'], ['format' => Ics::FORMAT_FILE])->generate($this->createShortEventLink()) ); } @@ -54,7 +54,7 @@ public function it_has_a_product_dtstamp(): void public function it_generates_base64_encoded_link_for_html(): void { $this->assertMatchesSnapshot( - $this->generator([], ['format' => 'html'])->generate($this->createShortEventLink()) + $this->generator([], ['format' => Ics::FORMAT_FILE])->generate($this->createShortEventLink()) ); } }