diff --git a/src/Generators/BaseOutlook.php b/src/Generators/BaseOutlook.php index 8f0fc82..3fd19e5 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 OutlookUrlParameters = 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 OutlookUrlParameters */ + protected array $urlParameters = []; + + /** @psalm-param OutlookUrlParameters $urlParameters */ + public function __construct(array $urlParameters = []) + { + $this->urlParameters = $urlParameters; + } + /** 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->urlParameters 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..728c6f9 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 GoogleUrlParameters = array */ class Google implements Generator { @@ -16,6 +17,15 @@ class Google implements Generator /** @var string */ protected $dateTimeFormat = 'Ymd\THis\Z'; + /** @psalm-var GoogleUrlParameters */ + protected array $urlParameters = []; + + /** @psalm-param GoogleUrlParameters $urlParameters */ + public function __construct(array $urlParameters = []) + { + $this->urlParameters = $urlParameters; + } + /** {@inheritDoc} */ public function generate(Link $link): string { @@ -44,6 +54,10 @@ public function generate(Link $link): string $url .= '&location='.urlencode($link->address); } + foreach ($this->urlParameters 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..99e9900 100644 --- a/src/Generators/Yahoo.php +++ b/src/Generators/Yahoo.php @@ -8,14 +8,25 @@ /** * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/yahoo.md + * @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 YahooUrlParameters */ + protected array $urlParameters = []; + + /** @psalm-param YahooUrlParameters $urlParameters */ + public function __construct(array $urlParameters = []) + { + $this->urlParameters = $urlParameters; + } + /** {@inheritDoc} */ public function generate(Link $link): string { @@ -44,6 +55,10 @@ public function generate(Link $link): string $url .= '&in_loc='.$this->sanitizeText($link->address); } + foreach ($this->urlParameters 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..54c9ab3 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 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 { @@ -123,9 +126,10 @@ public function formatWith(Generator $generator): string return $generator->generate($this); } - public function google(): string + /** @psalm-param GoogleUrlParameters $urlParameters */ + public function google(array $urlParameters = []): string { - return $this->formatWith(new Google()); + return $this->formatWith(new Google($urlParameters)); } /** @@ -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 YahooUrlParameters $urlParameters */ + public function yahoo(array $urlParameters = []): string { - return $this->formatWith(new Yahoo()); + return $this->formatWith(new Yahoo($urlParameters)); } - public function webOutlook(): string + /** @psalm-param OutlookUrlParameters $urlParameters */ + public function webOutlook(array $urlParameters = []): string { - return $this->formatWith(new WebOutlook()); + return $this->formatWith(new WebOutlook($urlParameters)); } - public function webOffice(): string + /** @psalm-param OutlookUrlParameters $urlParameters */ + public function webOffice(array $urlParameters = []): string { - return $this->formatWith(new WebOffice()); + return $this->formatWith(new WebOffice($urlParameters)); } 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..099f961 --- /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&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 new file mode 100644 index 0000000..fd60bac --- /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&online=1 \ 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