|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SimpleSoftwareIO\QrCode\DataTypes; |
| 4 | + |
| 5 | +use DateTime; |
| 6 | +use BaconQrCode\Exception\InvalidArgumentException; |
| 7 | + |
| 8 | +class Calendar implements DataTypeInterface |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The prefix of the QrCode. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $prefix = "BEGIN:VEVENT\n"; |
| 16 | + |
| 17 | + /** |
| 18 | + * The suffix of the QrCode. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $suffix = "END:VEVENT"; |
| 23 | + |
| 24 | + /** |
| 25 | + * The separator between the variables. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $separator = "\n"; |
| 30 | + |
| 31 | + /** |
| 32 | + * The summary of the event. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + protected $summary; |
| 37 | + |
| 38 | + /** |
| 39 | + * The location of the event. |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + protected $location; |
| 44 | + |
| 45 | + /** |
| 46 | + * The url of the event. |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + protected $url; |
| 51 | + |
| 52 | + /** |
| 53 | + * The start time of the event. |
| 54 | + * |
| 55 | + * @var DateTime |
| 56 | + */ |
| 57 | + protected $startDateTime; |
| 58 | + |
| 59 | + /** |
| 60 | + * The end time of the event. |
| 61 | + * |
| 62 | + * @var DateTime |
| 63 | + */ |
| 64 | + protected $endDateTime; |
| 65 | + |
| 66 | + /** |
| 67 | + * The standard format for the [$statDateTime] and [$endDateTime]. |
| 68 | + * |
| 69 | + * @var string |
| 70 | + */ |
| 71 | + protected $dateTimeFormat = "Y-m-d H:i"; |
| 72 | + |
| 73 | + /** |
| 74 | + * Generates the DataType Object and sets all of its properties. |
| 75 | + * |
| 76 | + * @param $arguments |
| 77 | + */ |
| 78 | + public function create(array $arguments) |
| 79 | + { |
| 80 | + $this->setProperties($arguments); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the correct QrCode format. |
| 85 | + * |
| 86 | + * @return string |
| 87 | + */ |
| 88 | + public function __toString() |
| 89 | + { |
| 90 | + return $this->buildCalendarString(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Builds the WiFi string. |
| 95 | + * |
| 96 | + * @return string |
| 97 | + */ |
| 98 | + protected function buildCalendarString() |
| 99 | + { |
| 100 | + $calendar = $this->prefix; |
| 101 | + |
| 102 | + if (isset($this->summary)) { |
| 103 | + $calendar .= "SUMMARY:".$this->summary.$this->separator; |
| 104 | + } |
| 105 | + if (isset($this->location)) { |
| 106 | + $calendar .= 'LOCATION:'.$this->location.$this->separator; |
| 107 | + } |
| 108 | + if (isset($this->url)) { |
| 109 | + $calendar .= 'URL:'.$this->url.$this->separator; |
| 110 | + } |
| 111 | + if (isset($this->startDateTime)) { |
| 112 | + $calendar .= 'DTSTART:'.$this->convertEventDateTimeToString($this->startDateTime).$this->separator; |
| 113 | + } |
| 114 | + if (isset($this->endDateTime)) { |
| 115 | + $calendar .= 'DTEND:'.$this->convertEventDateTimeToString($this->endDateTime).$this->separator; |
| 116 | + } |
| 117 | + |
| 118 | + $calendar .= $this->suffix; |
| 119 | + |
| 120 | + return $calendar; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Sets the Calendar properties. |
| 125 | + * |
| 126 | + * @param $arguments |
| 127 | + */ |
| 128 | + protected function setProperties(array $arguments) |
| 129 | + { |
| 130 | + $arguments = $arguments[0]; |
| 131 | + if (isset($arguments['summary'])) { |
| 132 | + $this->summary = $arguments['summary']; |
| 133 | + } |
| 134 | + if (isset($arguments['location'])) { |
| 135 | + $this->location = $arguments['location']; |
| 136 | + } |
| 137 | + if (isset($arguments['url'])) { |
| 138 | + $this->setUrl($arguments['url']); |
| 139 | + } |
| 140 | + if (isset($arguments['start_date_time'])) { |
| 141 | + $this->setEventDateTime($arguments['start_date_time'], "start"); |
| 142 | + } |
| 143 | + if (isset($arguments['end_date_time'])) { |
| 144 | + $this->setEventDateTime($arguments['start_date_time'], "end"); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Sets the url property. |
| 150 | + * |
| 151 | + * @param $url |
| 152 | + */ |
| 153 | + protected function setUrl($url) |
| 154 | + { |
| 155 | + if ($this->isValidUrl($url)) { |
| 156 | + $this->url = $url; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Sets the datetime property. |
| 162 | + * |
| 163 | + * @param DateTime $eventDateTime |
| 164 | + * @param string $type |
| 165 | + * |
| 166 | + */ |
| 167 | + protected function setEventDateTime($eventDateTime, $type = "") |
| 168 | + { |
| 169 | + if ($this->isValidDateTime($eventDateTime)) { |
| 170 | + if ($type == "start") { |
| 171 | + $this->startDateTime = $eventDateTime->format("yyyyMMddTHHmmss"); |
| 172 | + } |
| 173 | + if ($type == "end") { |
| 174 | + $this->endDateTime = $eventDateTime->format("yyyyMMddTHHmmss"); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Ensures url is valid. |
| 181 | + * |
| 182 | + * @param string $url |
| 183 | + * |
| 184 | + * @return bool |
| 185 | + */ |
| 186 | + protected function isValidUrl($url) |
| 187 | + { |
| 188 | + if (! filter_var($url, FILTER_VALIDATE_EMAIL)) { |
| 189 | + throw new InvalidArgumentException('Invalid url provided'); |
| 190 | + } |
| 191 | + |
| 192 | + return true; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Ensures datetime is valid. |
| 197 | + * |
| 198 | + * @param DateTime $dateTime |
| 199 | + * @param string $type |
| 200 | + * |
| 201 | + * @return bool |
| 202 | + */ |
| 203 | + protected function isValidDateTime($dateTime, $type = "") |
| 204 | + { |
| 205 | + $newDate = DateTime::createFromFormat($this->dateTimeFormat, $dateTime); |
| 206 | + if (! $newDate && $newDate->format($this->dateTimeFormat) == $dateTime) { |
| 207 | + if ($type == "start") { |
| 208 | + throw new InvalidArgumentException('Invalid start date provided'); |
| 209 | + } |
| 210 | + if ($type == "end") { |
| 211 | + throw new InvalidArgumentException('Invalid end date provided'); |
| 212 | + } |
| 213 | + throw new InvalidArgumentException('Invalid date provided'); |
| 214 | + } |
| 215 | + |
| 216 | + return true; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Returns date time to string |
| 221 | + * |
| 222 | + * @param DateTime $dateTime |
| 223 | + * |
| 224 | + * @return string |
| 225 | + */ |
| 226 | + protected function convertEventDateTimeToString($dateTime) |
| 227 | + { |
| 228 | + return $dateTime->format($this->dateTimeFormat); |
| 229 | + } |
| 230 | +} |
0 commit comments