Passed
Pull Request — master (#14)
by Theo
03:38
created

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