Completed
Push — master ( 912d34...2051d5 )
by
unknown
24s
created

Ics::escapeString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\CalendarLinks\Generators;
4
5
use Spatie\CalendarLinks\Link;
6
use Spatie\CalendarLinks\Generator;
7
8
/**
9
 * @see https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/
10
 */
11
class Ics implements Generator
12
{
13
    public function generate(Link $link): string
14
    {
15
        $url = ['data:text/calendar;charset=utf8,',
16
      'BEGIN:VCALENDAR',
17
      'VERSION:2.0',
18
      'BEGIN:VEVENT',
19
      'SUMMARY:'.$link->title, ];
20
21
        if ($link->allDay) {
22
            $dateTimeFormat = 'Ymd';
23
            $url[] = 'DTSTART:'.$link->from->format($dateTimeFormat);
24
            $url[] = 'DTEND:'.$link->to->format($dateTimeFormat);
25
        } else {
26
            $dateTimeFormat = "e:Ymd\THis";
27
            $url[] = 'DTSTART;TZID='.$link->from->format($dateTimeFormat);
28
            $url[] = 'DTEND;TZID='.$link->to->format($dateTimeFormat);
29
        }
30
31
        if ($link->description) {
32
            $url[] = 'DESCRIPTION:'.$this->escapeString($link->description);
33
        }
34
        if ($link->address) {
35
            $url[] = 'LOCATION:'.$this->escapeString($link->address);
36
        }
37
38
        $url[] = 'END:VEVENT';
39
        $url[] = 'END:VCALENDAR';
40
        $redirectLink = implode('%0A', $url);
41
42
        return $redirectLink;
43
    }
44
45
    protected function escapeString(string $field): string
46
    {
47
        return addcslashes($field, "\n,");
48
    }
49
}
50