Event::setOptions()   A
last analyzed

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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CalendarBundle\Entity;
6
7
use DateTimeInterface;
8
9
class Event
10
{
11
    const DATE_FORMAT = 'Y-m-d\\TH:i:s.u\\Z';
12
13
    /**
14
     * @var string
15
     */
16
    protected $title;
17
18
    /**
19
     * @var DateTimeInterface
20
     */
21
    protected $start;
22
23
    /**
24
     * @var DateTimeInterface|null
25
     */
26
    protected $end;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $allDay = true;
32
33
    /**
34
     * @var array
35
     */
36
    protected $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 2
    public function getTitle(): ?string
51
    {
52 2
        return $this->title;
53
    }
54
55 2
    public function setTitle(string $title): void
56
    {
57 2
        $this->title = $title;
58 2
    }
59
60 2
    public function getStart(): ?DateTimeInterface
61
    {
62 2
        return $this->start;
63
    }
64
65 2
    public function setStart(DateTimeInterface $start): void
66
    {
67 2
        $this->start = $start;
68 2
    }
69
70 2
    public function getEnd(): ?DateTimeInterface
71
    {
72 2
        return $this->end;
73
    }
74
75 2
    public function setEnd(?DateTimeInterface $end): void
76
    {
77 2
        if (null !== $end) {
78 2
            $this->allDay = false;
79
        }
80 2
        $this->end = $end;
81 2
    }
82
83 1
    public function isAllDay(): bool
84
    {
85 1
        return $this->allDay;
86
    }
87
88 1
    public function setAllDay(bool $allDay): void
89
    {
90 1
        $this->allDay = $allDay;
91 1
    }
92
93 2
    public function getOptions(): array
94
    {
95 2
        return $this->options;
96
    }
97
98 2
    public function setOptions(array $options): void
99
    {
100 2
        $this->options = $options;
101 2
    }
102
103
    /**
104
     * @param string|int $name
105
     */
106 1
    public function getOption($name)
107
    {
108 1
        return $this->options[$name];
109
    }
110
111
    /**
112
     * @param string|int $name
113
     */
114 1
    public function addOption($name, $value): void
115
    {
116 1
        $this->options[$name] = $value;
117 1
    }
118
119
    /**
120
     * @param string|int $name
121
     *
122
     * @return mixed|null
123
     */
124 1
    public function removeOption($name)
125
    {
126 1
        if (!isset($this->options[$name]) && !\array_key_exists($name, $this->options)) {
127 1
            return null;
128
        }
129
130 1
        $removed = $this->options[$name];
131 1
        unset($this->options[$name]);
132
133 1
        return $removed;
134
    }
135
136 1
    public function toArray(): array
137
    {
138
        $event = [
139 1
            'title' => $this->getTitle(),
140 1
            'start' => $this->getStart()->format(self::DATE_FORMAT),
141 1
            'allDay' => $this->isAllDay(),
142
        ];
143
144 1
        if (null !== $this->getEnd()) {
145 1
            $event['end'] = $this->getEnd()->format(self::DATE_FORMAT);
146
        }
147
148 1
        return array_merge($event, $this->getOptions());
149
    }
150
}
151