From 99b9f28337aad6e65532353f7c9edb13e67d89c4 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 28 Jan 2024 14:03:46 +0530 Subject: [PATCH 1/2] Allow URL customization by providing custom parameters --- src/Generators/BaseOutlook.php | 14 +++++++++++ src/Generators/Google.php | 14 +++++++++++ src/Generators/Ics.php | 2 +- src/Generators/Yahoo.php | 14 +++++++++++ src/Link.php | 23 ++++++++++++------- tests/Generators/GoogleGeneratorTest.php | 8 +++++++ tests/Generators/WebOfficeGeneratorTest.php | 8 +++++++ tests/Generators/WebOutlookGeneratorTest.php | 8 +++++++ tests/Generators/YahooGeneratorTest.php | 8 +++++++ ...erate_an_url_with_custom_parameters__1.txt | 1 + ...erate_an_url_with_custom_parameters__1.txt | 1 + ...erate_an_url_with_custom_parameters__1.txt | 1 + ...erate_an_url_with_custom_parameters__1.txt | 1 + 13 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 tests/Generators/__snapshots__/GoogleGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt create mode 100644 tests/Generators/__snapshots__/WebOfficeGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt create mode 100644 tests/Generators/__snapshots__/WebOutlookGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt create mode 100644 tests/Generators/__snapshots__/YahooGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt diff --git a/src/Generators/BaseOutlook.php b/src/Generators/BaseOutlook.php index 8f0fc82..3ac7dca 100644 --- a/src/Generators/BaseOutlook.php +++ b/src/Generators/BaseOutlook.php @@ -8,6 +8,7 @@ /** * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/outlook-web.md + * @psalm-type OutlookOptions = array */ abstract class BaseOutlook implements Generator { @@ -17,6 +18,15 @@ abstract class BaseOutlook implements Generator /** @var string {@see https://www.php.net/manual/en/function.date.php} */ protected $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; + /** @psalm-var OutlookOptions */ + protected array $options = []; + + /** @psalm-param OutlookOptions $options */ + public function __construct(array $options = []) + { + $this->options = $options; + } + /** Get base URL for links. */ abstract public function baseUrl(): string; @@ -47,6 +57,10 @@ public function generate(Link $link): string $url .= '&location='.$this->sanitizeString($link->address); } + foreach ($this->options as $key => $value) { + $url .= '&'.urlencode($key).(in_array($value, [null, ''], true) ? '' : '='.$this->sanitizeString((string) $value)); + } + return $url; } diff --git a/src/Generators/Google.php b/src/Generators/Google.php index 5ff4fe6..c502632 100644 --- a/src/Generators/Google.php +++ b/src/Generators/Google.php @@ -8,6 +8,7 @@ /** * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/google.md + * @psalm-type GoogleOptions = array */ class Google implements Generator { @@ -16,6 +17,15 @@ class Google implements Generator /** @var string */ protected $dateTimeFormat = 'Ymd\THis\Z'; + /** @psalm-var GoogleOptions */ + protected array $options = []; + + /** @psalm-param GoogleOptions $options */ + public function __construct(array $options = []) + { + $this->options = $options; + } + /** {@inheritDoc} */ public function generate(Link $link): string { @@ -44,6 +54,10 @@ public function generate(Link $link): string $url .= '&location='.urlencode($link->address); } + foreach ($this->options as $key => $value) { + $url .= '&'.urlencode($key).(in_array($value, [null, ''], true) ? '' : '='.urlencode((string) $value)); + } + return $url; } } diff --git a/src/Generators/Ics.php b/src/Generators/Ics.php index 248b674..67fcc85 100644 --- a/src/Generators/Ics.php +++ b/src/Generators/Ics.php @@ -20,7 +20,7 @@ class Ics implements Generator /** @var string */ protected $dateTimeFormat = 'Ymd\THis\Z'; - /** @var IcsOptions */ + /** @psalm-var IcsOptions */ protected $options = []; /** @var array{format?: self::FORMAT_*} */ diff --git a/src/Generators/Yahoo.php b/src/Generators/Yahoo.php index c6a18db..06f4953 100644 --- a/src/Generators/Yahoo.php +++ b/src/Generators/Yahoo.php @@ -8,6 +8,7 @@ /** * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/yahoo.md + * @psalm-type YahooOptions = array */ class Yahoo implements Generator { @@ -16,6 +17,15 @@ class Yahoo implements Generator /** @var string */ protected $dateTimeFormat = 'Ymd\THis\Z'; + /** @psalm-var YahooOptions */ + protected array $options = []; + + /** @psalm-param YahooOptions $options */ + public function __construct(array $options = []) + { + $this->options = $options; + } + /** {@inheritDoc} */ public function generate(Link $link): string { @@ -44,6 +54,10 @@ public function generate(Link $link): string $url .= '&in_loc='.$this->sanitizeText($link->address); } + foreach ($this->options as $key => $value) { + $url .= '&'.urlencode($key).(in_array($value, [null, ''], true) ? '' : '='.$this->sanitizeText((string) $value)); + } + return $url; } diff --git a/src/Link.php b/src/Link.php index 9230480..e8b4116 100644 --- a/src/Link.php +++ b/src/Link.php @@ -17,6 +17,9 @@ * @property-read string $address * @property-read bool $allDay * @psalm-import-type IcsOptions from \Spatie\CalendarLinks\Generators\Ics + * @psalm-import-type GoogleOptions from \Spatie\CalendarLinks\Generators\Google + * @psalm-import-type YahooOptions from \Spatie\CalendarLinks\Generators\Yahoo + * @psalm-import-type OutlookOptions from \Spatie\CalendarLinks\Generators\BaseOutlook */ class Link { @@ -123,9 +126,10 @@ public function formatWith(Generator $generator): string return $generator->generate($this); } - public function google(): string + /** @psalm-param GoogleOptions $options */ + public function google(array $options = []): string { - return $this->formatWith(new Google()); + return $this->formatWith(new Google($options)); } /** @@ -139,19 +143,22 @@ public function ics(array $options = [], array $presentationOptions = []): strin return $this->formatWith(new Ics($options, $presentationOptions)); } - public function yahoo(): string + /** @psalm-param YahooOptions $options */ + public function yahoo(array $options = []): string { - return $this->formatWith(new Yahoo()); + return $this->formatWith(new Yahoo($options)); } - public function webOutlook(): string + /** @psalm-param OutlookOptions $options */ + public function webOutlook(array $options = []): string { - return $this->formatWith(new WebOutlook()); + return $this->formatWith(new WebOutlook($options)); } - public function webOffice(): string + /** @psalm-param OutlookOptions $options */ + public function webOffice(array $options = []): string { - return $this->formatWith(new WebOffice()); + return $this->formatWith(new WebOffice($options)); } public function __get($property) diff --git a/tests/Generators/GoogleGeneratorTest.php b/tests/Generators/GoogleGeneratorTest.php index 079e15b..f068640 100644 --- a/tests/Generators/GoogleGeneratorTest.php +++ b/tests/Generators/GoogleGeneratorTest.php @@ -35,4 +35,12 @@ public function it_correctly_generates_all_day_events_by_dates(): void $this->generator()->generate($this->createEventMultipleDaysViaStartEndWithTimezoneLink()) ); } + + /** @test */ + public function it_can_generate_an_url_with_custom_parameters(): void + { + $link = $this->createShortEventLink(); + + $this->assertMatchesSnapshot($link->google(['recur' => 'RRULE:FREQ=DAILY'])); + } } diff --git a/tests/Generators/WebOfficeGeneratorTest.php b/tests/Generators/WebOfficeGeneratorTest.php index 9a6ec3d..badfe9a 100644 --- a/tests/Generators/WebOfficeGeneratorTest.php +++ b/tests/Generators/WebOfficeGeneratorTest.php @@ -19,4 +19,12 @@ protected function linkMethodName(): string { return 'webOffice'; } + + /** @test */ + public function it_can_generate_an_url_with_custom_parameters(): void + { + $link = $this->createShortEventLink(); + + $this->assertMatchesSnapshot($link->webOffice(['online' => 1])); + } } diff --git a/tests/Generators/WebOutlookGeneratorTest.php b/tests/Generators/WebOutlookGeneratorTest.php index 3b2bebf..03ba90c 100644 --- a/tests/Generators/WebOutlookGeneratorTest.php +++ b/tests/Generators/WebOutlookGeneratorTest.php @@ -19,4 +19,12 @@ protected function linkMethodName(): string { return 'webOutlook'; } + + /** @test */ + public function it_can_generate_an_url_with_custom_parameters(): void + { + $link = $this->createShortEventLink(); + + $this->assertMatchesSnapshot($link->webOutlook(['online' => 1])); + } } diff --git a/tests/Generators/YahooGeneratorTest.php b/tests/Generators/YahooGeneratorTest.php index e77ac05..5b1017f 100644 --- a/tests/Generators/YahooGeneratorTest.php +++ b/tests/Generators/YahooGeneratorTest.php @@ -34,4 +34,12 @@ public function it_can_generate_a_yahoo_link_for_long_multiple_days_event(): voi $this->assertMatchesSnapshot($link->yahoo()); } + + /** @test */ + public function it_can_generate_an_url_with_custom_parameters(): void + { + $link = $this->createShortEventLink(); + + $this->assertMatchesSnapshot($link->yahoo(['uid' => '750e0c92aa33a7382460a280c2dfb8e6', 'msngr' => null])); + } } diff --git a/tests/Generators/__snapshots__/GoogleGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt b/tests/Generators/__snapshots__/GoogleGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt new file mode 100644 index 0000000..1f254ff --- /dev/null +++ b/tests/Generators/__snapshots__/GoogleGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt @@ -0,0 +1 @@ +https://calendar.google.com/calendar/render?action=TEMPLATE&dates=20180201T090000Z/20180201T180000Z&ctz=UTC&text=Birthday&details=With+balloons%2C+clowns+and+stuff%0ABring+a+dog%2C+bring+a+frog&location=Party+Lane+1A%2C+1337+Funtown&recur=RRULE%3AFREQ%3DDAILY \ No newline at end of file diff --git a/tests/Generators/__snapshots__/WebOfficeGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt b/tests/Generators/__snapshots__/WebOfficeGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt new file mode 100644 index 0000000..f2ee332 --- /dev/null +++ b/tests/Generators/__snapshots__/WebOfficeGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt @@ -0,0 +1 @@ +https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown \ No newline at end of file diff --git a/tests/Generators/__snapshots__/WebOutlookGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt b/tests/Generators/__snapshots__/WebOutlookGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt new file mode 100644 index 0000000..079416d --- /dev/null +++ b/tests/Generators/__snapshots__/WebOutlookGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt @@ -0,0 +1 @@ +https://outlook.live.com/calendar/action/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown \ No newline at end of file diff --git a/tests/Generators/__snapshots__/YahooGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt b/tests/Generators/__snapshots__/YahooGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt new file mode 100644 index 0000000..bd2dc7f --- /dev/null +++ b/tests/Generators/__snapshots__/YahooGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt @@ -0,0 +1 @@ +https://calendar.yahoo.com/?v=60&view=d&type=20&ST=20180201T090000Z&ET=20180201T180000Z&TITLE=Birthday&DESC=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&in_loc=Party%20Lane%201A%2C%201337%20Funtown&uid=750e0c92aa33a7382460a280c2dfb8e6&msngr \ No newline at end of file From e3f779b570aa9995a28de3e1ee8467a9da6156b3 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sun, 28 Jan 2024 14:22:56 +0530 Subject: [PATCH 2/2] Rename parameters --- src/Generators/BaseOutlook.php | 14 ++++----- src/Generators/Google.php | 14 ++++----- src/Generators/Yahoo.php | 15 +++++----- src/Link.php | 30 +++++++++---------- ...erate_an_url_with_custom_parameters__1.txt | 2 +- ...erate_an_url_with_custom_parameters__1.txt | 2 +- 6 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/Generators/BaseOutlook.php b/src/Generators/BaseOutlook.php index 3ac7dca..3fd19e5 100644 --- a/src/Generators/BaseOutlook.php +++ b/src/Generators/BaseOutlook.php @@ -8,7 +8,7 @@ /** * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/outlook-web.md - * @psalm-type OutlookOptions = array + * @psalm-type OutlookUrlParameters = array */ abstract class BaseOutlook implements Generator { @@ -18,13 +18,13 @@ abstract class BaseOutlook implements Generator /** @var string {@see https://www.php.net/manual/en/function.date.php} */ protected $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; - /** @psalm-var OutlookOptions */ - protected array $options = []; + /** @psalm-var OutlookUrlParameters */ + protected array $urlParameters = []; - /** @psalm-param OutlookOptions $options */ - public function __construct(array $options = []) + /** @psalm-param OutlookUrlParameters $urlParameters */ + public function __construct(array $urlParameters = []) { - $this->options = $options; + $this->urlParameters = $urlParameters; } /** Get base URL for links. */ @@ -57,7 +57,7 @@ public function generate(Link $link): string $url .= '&location='.$this->sanitizeString($link->address); } - foreach ($this->options as $key => $value) { + foreach ($this->urlParameters as $key => $value) { $url .= '&'.urlencode($key).(in_array($value, [null, ''], true) ? '' : '='.$this->sanitizeString((string) $value)); } diff --git a/src/Generators/Google.php b/src/Generators/Google.php index c502632..728c6f9 100644 --- a/src/Generators/Google.php +++ b/src/Generators/Google.php @@ -8,7 +8,7 @@ /** * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/google.md - * @psalm-type GoogleOptions = array + * @psalm-type GoogleUrlParameters = array */ class Google implements Generator { @@ -17,13 +17,13 @@ class Google implements Generator /** @var string */ protected $dateTimeFormat = 'Ymd\THis\Z'; - /** @psalm-var GoogleOptions */ - protected array $options = []; + /** @psalm-var GoogleUrlParameters */ + protected array $urlParameters = []; - /** @psalm-param GoogleOptions $options */ - public function __construct(array $options = []) + /** @psalm-param GoogleUrlParameters $urlParameters */ + public function __construct(array $urlParameters = []) { - $this->options = $options; + $this->urlParameters = $urlParameters; } /** {@inheritDoc} */ @@ -54,7 +54,7 @@ public function generate(Link $link): string $url .= '&location='.urlencode($link->address); } - foreach ($this->options as $key => $value) { + foreach ($this->urlParameters as $key => $value) { $url .= '&'.urlencode($key).(in_array($value, [null, ''], true) ? '' : '='.urlencode((string) $value)); } diff --git a/src/Generators/Yahoo.php b/src/Generators/Yahoo.php index 06f4953..99e9900 100644 --- a/src/Generators/Yahoo.php +++ b/src/Generators/Yahoo.php @@ -8,22 +8,23 @@ /** * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/yahoo.md - * @psalm-type YahooOptions = array + * @psalm-type YahooUrlParameters = array */ class Yahoo implements Generator { /** @var string {@see https://www.php.net/manual/en/function.date.php} */ protected $dateFormat = 'Ymd'; + /** @var string */ protected $dateTimeFormat = 'Ymd\THis\Z'; - /** @psalm-var YahooOptions */ - protected array $options = []; + /** @psalm-var YahooUrlParameters */ + protected array $urlParameters = []; - /** @psalm-param YahooOptions $options */ - public function __construct(array $options = []) + /** @psalm-param YahooUrlParameters $urlParameters */ + public function __construct(array $urlParameters = []) { - $this->options = $options; + $this->urlParameters = $urlParameters; } /** {@inheritDoc} */ @@ -54,7 +55,7 @@ public function generate(Link $link): string $url .= '&in_loc='.$this->sanitizeText($link->address); } - foreach ($this->options as $key => $value) { + foreach ($this->urlParameters as $key => $value) { $url .= '&'.urlencode($key).(in_array($value, [null, ''], true) ? '' : '='.$this->sanitizeText((string) $value)); } diff --git a/src/Link.php b/src/Link.php index e8b4116..54c9ab3 100644 --- a/src/Link.php +++ b/src/Link.php @@ -17,9 +17,9 @@ * @property-read string $address * @property-read bool $allDay * @psalm-import-type IcsOptions from \Spatie\CalendarLinks\Generators\Ics - * @psalm-import-type GoogleOptions from \Spatie\CalendarLinks\Generators\Google - * @psalm-import-type YahooOptions from \Spatie\CalendarLinks\Generators\Yahoo - * @psalm-import-type OutlookOptions from \Spatie\CalendarLinks\Generators\BaseOutlook + * @psalm-import-type GoogleUrlParameters from \Spatie\CalendarLinks\Generators\Google + * @psalm-import-type YahooUrlParameters from \Spatie\CalendarLinks\Generators\Yahoo + * @psalm-import-type OutlookUrlParameters from \Spatie\CalendarLinks\Generators\BaseOutlook */ class Link { @@ -126,10 +126,10 @@ public function formatWith(Generator $generator): string return $generator->generate($this); } - /** @psalm-param GoogleOptions $options */ - public function google(array $options = []): string + /** @psalm-param GoogleUrlParameters $urlParameters */ + public function google(array $urlParameters = []): string { - return $this->formatWith(new Google($options)); + return $this->formatWith(new Google($urlParameters)); } /** @@ -143,22 +143,22 @@ public function ics(array $options = [], array $presentationOptions = []): strin return $this->formatWith(new Ics($options, $presentationOptions)); } - /** @psalm-param YahooOptions $options */ - public function yahoo(array $options = []): string + /** @psalm-param YahooUrlParameters $urlParameters */ + public function yahoo(array $urlParameters = []): string { - return $this->formatWith(new Yahoo($options)); + return $this->formatWith(new Yahoo($urlParameters)); } - /** @psalm-param OutlookOptions $options */ - public function webOutlook(array $options = []): string + /** @psalm-param OutlookUrlParameters $urlParameters */ + public function webOutlook(array $urlParameters = []): string { - return $this->formatWith(new WebOutlook($options)); + return $this->formatWith(new WebOutlook($urlParameters)); } - /** @psalm-param OutlookOptions $options */ - public function webOffice(array $options = []): string + /** @psalm-param OutlookUrlParameters $urlParameters */ + public function webOffice(array $urlParameters = []): string { - return $this->formatWith(new WebOffice($options)); + return $this->formatWith(new WebOffice($urlParameters)); } public function __get($property) diff --git a/tests/Generators/__snapshots__/WebOfficeGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt b/tests/Generators/__snapshots__/WebOfficeGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt index f2ee332..099f961 100644 --- a/tests/Generators/__snapshots__/WebOfficeGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt +++ b/tests/Generators/__snapshots__/WebOfficeGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt @@ -1 +1 @@ -https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown \ No newline at end of file +https://outlook.office.com/calendar/deeplink/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown&online=1 \ No newline at end of file diff --git a/tests/Generators/__snapshots__/WebOutlookGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt b/tests/Generators/__snapshots__/WebOutlookGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt index 079416d..fd60bac 100644 --- a/tests/Generators/__snapshots__/WebOutlookGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt +++ b/tests/Generators/__snapshots__/WebOutlookGeneratorTest__it_can_generate_an_url_with_custom_parameters__1.txt @@ -1 +1 @@ -https://outlook.live.com/calendar/action/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown \ No newline at end of file +https://outlook.live.com/calendar/action/compose?path=/calendar/action/compose&rru=addevent&startdt=2018-02-01T09:00:00Z&enddt=2018-02-01T18:00:00Z&subject=Birthday&body=With%20balloons%2C%20clowns%20and%20stuff%0ABring%20a%20dog%2C%20bring%20a%20frog&location=Party%20Lane%201A%2C%201337%20Funtown&online=1 \ No newline at end of file