Passed
Push — master ( 961468...052950 )
by Theo
01:43
created

Event::addOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CalendarBundle\Entity;
4
5
class Event
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $title;
11
12
    /**
13
     * @var \DateTimeInterface
14
     */
15
    protected $start;
16
17
    /**
18
     * @var \DateTimeInterface
19
     */
20
    protected $end = null;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $allDay = true;
26
27
    /**
28
     * @var array
29
     */
30
    protected $options = [];
31
32
    /**
33
     * @param string             $title
34
     * @param \DateTimeInterface $start
35
     * @param \DateTimeInterface $end
36
     * @param array              $options
37
     */
38 2
    public function __construct(
39
        string $title,
40
        \DateTimeInterface $start,
41
        \DateTimeInterface $end = null,
42
        array $options = []
43
    ) {
44 2
        $this->setTitle($title);
45 2
        $this->setStart($start);
46 2
        $this->setEnd($end);
47 2
        $this->setOptions($options);
48 2
    }
49
50
    /**
51
     * @return string
52
     */
53 2
    public function getTitle(): ?string
54
    {
55 2
        return $this->title;
56
    }
57
58
    /**
59
     * @param string $title
60
     */
61 2
    public function setTitle(string $title): void
62
    {
63 2
        $this->title = $title;
64 2
    }
65
66
    /**
67
     * @return \DateTimeInterface
68
     */
69 2
    public function getStart(): ?\DateTimeInterface
70
    {
71 2
        return $this->start;
72
    }
73
74
    /**
75
     * @param \DateTimeInterface $start
76
     */
77 2
    public function setStart(\DateTimeInterface $start): void
78
    {
79 2
        $this->start = $start;
80 2
    }
81
82
    /**
83
     * @return \DateTimeInterface|null
84
     */
85 2
    public function getEnd(): ?\DateTimeInterface
86
    {
87 2
        return $this->end;
88
    }
89
90
    /**
91
     * @param \DateTimeInterface $end
92
     */
93 2
    public function setEnd(?\DateTimeInterface $end): void
94
    {
95 2
        if (null !== $end) {
96 2
            $this->allDay = false;
97
        }
98 2
        $this->end = $end;
99 2
    }
100
101
    /**
102
     * @return bool
103
     */
104 1
    public function isAllDay(): bool
105
    {
106 1
        return $this->allDay;
107
    }
108
109
    /**
110
     * @param bool $allDay
111
     */
112 1
    public function setAllDay(bool $allDay): void
113
    {
114 1
        $this->allDay = $allDay;
115 1
    }
116
117
    /**
118
     * @return array
119
     */
120 2
    public function getOptions(): array
121
    {
122 2
        return $this->options;
123
    }
124
125
    /**
126
     * @param array $options
127
     */
128 2
    public function setOptions(array $options): void
129
    {
130 2
        $this->options = $options;
131 2
    }
132
133
    /**
134
     * @param string $name
135
     *
136
     * @return mixed
137
     */
138 1
    public function getOption(string $name)
139
    {
140 1
        return $this->options[$name];
141
    }
142
143
    /**
144
     * @param string $name
145
     * @param mixed  $value
146
     */
147 1
    public function addOption(string $name, $value): void
148
    {
149 1
        $this->options[$name] = $value;
150 1
    }
151
152
    /**
153
     * @param string $name
154
     *
155
     * @return mixed
156
     */
157 1
    public function removeOption(string $name)
158
    {
159 1
        if (!isset($this->options[$name]) && !array_key_exists($name, $this->options)) {
160 1
            return null;
161
        }
162
163 1
        $removed = $this->options[$name];
164 1
        unset($this->options[$name]);
165
166 1
        return $removed;
167
    }
168
169
    /**
170
     * @return array
171
     */
172 1
    public function toArray(): array
173
    {
174
        $event = [
175 1
            'title' => $this->getTitle(),
176 1
            'start' => $this->getStart()->format('Y-m-d\\TH:i:sP'),
177 1
            'allDay' => $this->isAllDay(),
178
        ];
179
180 1
        if (null !== $this->getEnd()) {
181 1
            $event['end'] = $this->getEnd()->format('Y-m-d\\TH:i:sP');
182
        }
183
184 1
        return array_merge($event, $this->getOptions());
185
    }
186
}
187