Completed
Push — master ( 219e60...f62607 )
by Alies
01:39 queued 37s
created

Link::formatWith()   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;
4
5
use DateTime;
6
use Spatie\CalendarLinks\Exceptions\InvalidLink;
7
use Spatie\CalendarLinks\Generators\Google;
8
use Spatie\CalendarLinks\Generators\Ics;
9
use Spatie\CalendarLinks\Generators\WebOutlook;
10
use Spatie\CalendarLinks\Generators\Yahoo;
11
12
/**
13
 * @property-read string $title
14
 * @property-read \DateTime $from
15
 * @property-read \DateTime $to
16
 * @property-read string $description
17
 * @property-read string $address
18
 * @property-read bool $allDay
19
 */
20
class Link
21
{
22
    /** @var string */
23
    protected $title;
24
25
    /** @var \DateTime */
26
    protected $from;
27
28
    /** @var \DateTime */
29
    protected $to;
30
31
    /** @var string */
32
    protected $description;
33
34
    /** @var bool */
35
    protected $allDay;
36
37
    /** @var string */
38
    protected $address;
39
40
    public function __construct(string $title, DateTime $from, DateTime $to, bool $allDay = false)
41
    {
42
        $this->title = $title;
43
        $this->allDay = $allDay;
44
45
        if ($to < $from) {
46
            throw InvalidLink::invalidDateRange($from, $to);
47
        }
48
49
        $this->from = clone $from;
50
        $this->to = clone $to;
51
    }
52
53
    /**
54
     * @param string $title
55
     * @param \DateTime $from
56
     * @param \DateTime $to
57
     * @param bool $allDay
58
     *
59
     * @return static
60
     * @throws InvalidLink
61
     */
62
    public static function create(string $title, DateTime $from, DateTime $to, bool $allDay = false)
63
    {
64
        return new static($title, $from, $to, $allDay);
65
    }
66
67
    /**
68
     * @param string   $title
69
     * @param DateTime $fromDate
70
     * @param int      $numberOfDays
71
     *
72
     * @return Link
73
     * @throws InvalidLink
74
     */
75
    public static function createAllDay(string $title, DateTime $fromDate, int $numberOfDays = 1): self
76
    {
77
        $from = (clone $fromDate)->modify('midnight');
78
        $to = (clone $from)->modify("+$numberOfDays days");
79
80
        return new self($title, $from, $to, true);
81
    }
82
83
    /**
84
     * @param string $description
85
     *
86
     * @return $this
87
     */
88
    public function description(string $description)
89
    {
90
        $this->description = $description;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $address
97
     *
98
     * @return $this
99
     */
100
    public function address(string $address)
101
    {
102
        $this->address = $address;
103
104
        return $this;
105
    }
106
107
    public function formatWith(Generator $generator): string
108
    {
109
        return $generator->generate($this);
110
    }
111
112
    public function google(): string
113
    {
114
        return $this->formatWith(new Google());
115
    }
116
117
    public function ics(): string
118
    {
119
        return $this->formatWith(new Ics());
120
    }
121
122
    public function yahoo(): string
123
    {
124
        return $this->formatWith(new Yahoo());
125
    }
126
127
    public function webOutlook(): string
128
    {
129
        return $this->formatWith(new WebOutlook());
130
    }
131
132
    public function __get($property)
133
    {
134
        return $this->$property;
135
    }
136
}
137