1 | <?php |
||
2 | |||
3 | namespace PhpOffice\PhpSpreadsheet\Document; |
||
4 | |||
5 | class Properties |
||
6 | { |
||
7 | /** constants */ |
||
8 | const PROPERTY_TYPE_BOOLEAN = 'b'; |
||
9 | const PROPERTY_TYPE_INTEGER = 'i'; |
||
10 | const PROPERTY_TYPE_FLOAT = 'f'; |
||
11 | const PROPERTY_TYPE_DATE = 'd'; |
||
12 | const PROPERTY_TYPE_STRING = 's'; |
||
13 | const PROPERTY_TYPE_UNKNOWN = 'u'; |
||
14 | |||
15 | /** |
||
16 | * Creator. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | private $creator = 'Unknown Creator'; |
||
21 | |||
22 | /** |
||
23 | * LastModifiedBy. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $lastModifiedBy; |
||
28 | |||
29 | /** |
||
30 | * Created. |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | private $created; |
||
35 | |||
36 | /** |
||
37 | * Modified. |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | private $modified; |
||
42 | |||
43 | /** |
||
44 | * Title. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $title = 'Untitled Spreadsheet'; |
||
49 | |||
50 | /** |
||
51 | * Description. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $description = ''; |
||
56 | |||
57 | /** |
||
58 | * Subject. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $subject = ''; |
||
63 | |||
64 | /** |
||
65 | * Keywords. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | private $keywords = ''; |
||
70 | |||
71 | /** |
||
72 | * Category. |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | private $category = ''; |
||
77 | |||
78 | /** |
||
79 | * Manager. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | private $manager = ''; |
||
84 | |||
85 | /** |
||
86 | * Company. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | private $company = 'Microsoft Corporation'; |
||
91 | |||
92 | /** |
||
93 | * Custom Properties. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $customProperties = []; |
||
98 | |||
99 | /** |
||
100 | * Create a new Document Properties instance. |
||
101 | */ |
||
102 | 208 | public function __construct() |
|
103 | { |
||
104 | // Initialise values |
||
105 | 208 | $this->lastModifiedBy = $this->creator; |
|
106 | 208 | $this->created = time(); |
|
107 | 208 | $this->modified = time(); |
|
108 | 208 | } |
|
109 | |||
110 | /** |
||
111 | * Get Creator. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | 106 | public function getCreator() |
|
116 | { |
||
117 | 106 | return $this->creator; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Set Creator. |
||
122 | * |
||
123 | * @param string $creator |
||
124 | * |
||
125 | * @return Properties |
||
126 | */ |
||
127 | 99 | public function setCreator($creator) |
|
128 | { |
||
129 | 99 | $this->creator = $creator; |
|
130 | |||
131 | 99 | return $this; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get Last Modified By. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 90 | public function getLastModifiedBy() |
|
140 | { |
||
141 | 90 | return $this->lastModifiedBy; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Set Last Modified By. |
||
146 | * |
||
147 | * @param string $pValue |
||
148 | * |
||
149 | * @return Properties |
||
150 | */ |
||
151 | 95 | public function setLastModifiedBy($pValue) |
|
152 | { |
||
153 | 95 | $this->lastModifiedBy = $pValue; |
|
154 | |||
155 | 95 | return $this; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get Created. |
||
160 | * |
||
161 | * @return int |
||
162 | */ |
||
163 | 95 | public function getCreated() |
|
164 | { |
||
165 | 95 | return $this->created; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Set Created. |
||
170 | * |
||
171 | * @param int|string $time |
||
172 | * |
||
173 | * @return Properties |
||
174 | */ |
||
175 | 72 | public function setCreated($time) |
|
176 | { |
||
177 | 72 | if ($time === null) { |
|
178 | $time = time(); |
||
179 | 72 | } elseif (is_string($time)) { |
|
180 | if (is_numeric($time)) { |
||
181 | $time = (int) $time; |
||
182 | } else { |
||
183 | $time = strtotime($time); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | 72 | $this->created = $time; |
|
188 | |||
189 | 72 | return $this; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Get Modified. |
||
194 | * |
||
195 | * @return int |
||
196 | */ |
||
197 | 90 | public function getModified() |
|
198 | { |
||
199 | 90 | return $this->modified; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Set Modified. |
||
204 | * |
||
205 | * @param int|string $time |
||
206 | * |
||
207 | * @return Properties |
||
208 | */ |
||
209 | 72 | public function setModified($time) |
|
210 | { |
||
211 | 72 | if ($time === null) { |
|
212 | $time = time(); |
||
213 | 72 | } elseif (is_string($time)) { |
|
214 | if (is_numeric($time)) { |
||
215 | $time = (int) $time; |
||
216 | } else { |
||
217 | $time = strtotime($time); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | 72 | $this->modified = $time; |
|
222 | |||
223 | 72 | return $this; |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Get Title. |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | 106 | public function getTitle() |
|
232 | { |
||
233 | 106 | return $this->title; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Set Title. |
||
238 | * |
||
239 | * @param string $title |
||
240 | * |
||
241 | * @return Properties |
||
242 | */ |
||
243 | 87 | public function setTitle($title) |
|
244 | { |
||
245 | 87 | $this->title = $title; |
|
246 | |||
247 | 87 | return $this; |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Get Description. |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 106 | public function getDescription() |
|
256 | { |
||
257 | 106 | return $this->description; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Set Description. |
||
262 | * |
||
263 | * @param string $description |
||
264 | * |
||
265 | * @return Properties |
||
266 | */ |
||
267 | 83 | public function setDescription($description) |
|
268 | { |
||
269 | 83 | $this->description = $description; |
|
270 | |||
271 | 83 | return $this; |
|
272 | } |
||
273 | |||
274 | /** |
||
275 | * Get Subject. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 106 | public function getSubject() |
|
280 | { |
||
281 | 106 | return $this->subject; |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * Set Subject. |
||
286 | * |
||
287 | * @param string $subject |
||
288 | * |
||
289 | * @return Properties |
||
290 | */ |
||
291 | 83 | public function setSubject($subject) |
|
292 | { |
||
293 | 83 | $this->subject = $subject; |
|
294 | |||
295 | 83 | return $this; |
|
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get Keywords. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | 106 | public function getKeywords() |
|
304 | { |
||
305 | 106 | return $this->keywords; |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * Set Keywords. |
||
310 | * |
||
311 | * @param string $keywords |
||
312 | * |
||
313 | * @return Properties |
||
314 | */ |
||
315 | 83 | public function setKeywords($keywords) |
|
316 | { |
||
317 | 83 | $this->keywords = $keywords; |
|
318 | |||
319 | 83 | return $this; |
|
320 | } |
||
321 | |||
322 | /** |
||
323 | * Get Category. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | 106 | public function getCategory() |
|
328 | { |
||
329 | 106 | return $this->category; |
|
330 | } |
||
331 | |||
332 | /** |
||
333 | * Set Category. |
||
334 | * |
||
335 | * @param string $category |
||
336 | * |
||
337 | * @return Properties |
||
338 | */ |
||
339 | 73 | public function setCategory($category) |
|
340 | { |
||
341 | 73 | $this->category = $category; |
|
342 | |||
343 | 73 | return $this; |
|
344 | } |
||
345 | |||
346 | /** |
||
347 | * Get Company. |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | 101 | public function getCompany() |
|
352 | { |
||
353 | 101 | return $this->company; |
|
354 | } |
||
355 | |||
356 | /** |
||
357 | * Set Company. |
||
358 | * |
||
359 | * @param string $company |
||
360 | * |
||
361 | * @return Properties |
||
362 | */ |
||
363 | 50 | public function setCompany($company) |
|
364 | { |
||
365 | 50 | $this->company = $company; |
|
366 | |||
367 | 50 | return $this; |
|
368 | } |
||
369 | |||
370 | /** |
||
371 | * Get Manager. |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | 96 | public function getManager() |
|
376 | { |
||
377 | 96 | return $this->manager; |
|
378 | } |
||
379 | |||
380 | /** |
||
381 | * Set Manager. |
||
382 | * |
||
383 | * @param string $manager |
||
384 | * |
||
385 | * @return Properties |
||
386 | */ |
||
387 | 35 | public function setManager($manager) |
|
388 | { |
||
389 | 35 | $this->manager = $manager; |
|
390 | |||
391 | 35 | return $this; |
|
392 | } |
||
393 | |||
394 | /** |
||
395 | * Get a List of Custom Property Names. |
||
396 | * |
||
397 | * @return array of string |
||
398 | */ |
||
399 | 86 | public function getCustomProperties() |
|
400 | { |
||
401 | 86 | return array_keys($this->customProperties); |
|
402 | } |
||
403 | |||
404 | /** |
||
405 | * Check if a Custom Property is defined. |
||
406 | * |
||
407 | * @param string $propertyName |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function isCustomPropertySet($propertyName) |
||
412 | { |
||
413 | return isset($this->customProperties[$propertyName]); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Get a Custom Property Value. |
||
418 | * |
||
419 | * @param string $propertyName |
||
420 | * |
||
421 | * @return string |
||
422 | */ |
||
423 | 3 | public function getCustomPropertyValue($propertyName) |
|
424 | { |
||
425 | 3 | if (isset($this->customProperties[$propertyName])) { |
|
426 | 3 | return $this->customProperties[$propertyName]['value']; |
|
427 | } |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * Get a Custom Property Type. |
||
432 | * |
||
433 | * @param string $propertyName |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | 3 | public function getCustomPropertyType($propertyName) |
|
438 | { |
||
439 | 3 | if (isset($this->customProperties[$propertyName])) { |
|
440 | 3 | return $this->customProperties[$propertyName]['type']; |
|
441 | } |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Set a Custom Property. |
||
446 | * |
||
447 | * @param string $propertyName |
||
448 | * @param mixed $propertyValue |
||
449 | * @param string $propertyType |
||
450 | * 'i' : Integer |
||
451 | * 'f' : Floating Point |
||
452 | * 's' : String |
||
453 | * 'd' : Date/Time |
||
454 | * 'b' : Boolean |
||
455 | * |
||
456 | * @return Properties |
||
457 | */ |
||
458 | 13 | public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null) |
|
459 | { |
||
460 | 13 | if (($propertyType === null) || (!in_array($propertyType, [self::PROPERTY_TYPE_INTEGER, |
|
461 | 13 | self::PROPERTY_TYPE_FLOAT, |
|
462 | 13 | self::PROPERTY_TYPE_STRING, |
|
463 | 13 | self::PROPERTY_TYPE_DATE, |
|
464 | 13 | self::PROPERTY_TYPE_BOOLEAN, ]))) { |
|
465 | if ($propertyValue === null) { |
||
466 | $propertyType = self::PROPERTY_TYPE_STRING; |
||
467 | } elseif (is_float($propertyValue)) { |
||
468 | $propertyType = self::PROPERTY_TYPE_FLOAT; |
||
469 | } elseif (is_int($propertyValue)) { |
||
470 | $propertyType = self::PROPERTY_TYPE_INTEGER; |
||
471 | } elseif (is_bool($propertyValue)) { |
||
472 | $propertyType = self::PROPERTY_TYPE_BOOLEAN; |
||
473 | } else { |
||
474 | $propertyType = self::PROPERTY_TYPE_STRING; |
||
475 | } |
||
476 | } |
||
477 | |||
478 | 13 | $this->customProperties[$propertyName] = [ |
|
479 | 13 | 'value' => $propertyValue, |
|
480 | 13 | 'type' => $propertyType, |
|
481 | ]; |
||
482 | |||
483 | 13 | return $this; |
|
484 | } |
||
485 | |||
486 | /** |
||
487 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
488 | */ |
||
489 | public function __clone() |
||
490 | { |
||
491 | $vars = get_object_vars($this); |
||
492 | foreach ($vars as $key => $value) { |
||
493 | if (is_object($value)) { |
||
494 | $this->$key = clone $value; |
||
495 | } else { |
||
496 | $this->$key = $value; |
||
497 | } |
||
498 | } |
||
499 | } |
||
500 | |||
501 | 3 | public static function convertProperty($propertyValue, $propertyType) |
|
502 | { |
||
503 | 3 | switch ($propertyType) { |
|
504 | 3 | case 'empty': // Empty |
|
505 | return ''; |
||
506 | |||
507 | break; |
||
0 ignored issues
–
show
|
|||
508 | 3 | case 'null': // Null |
|
509 | return null; |
||
510 | |||
511 | break; |
||
512 | 3 | case 'i1': // 1-Byte Signed Integer |
|
513 | 3 | case 'i2': // 2-Byte Signed Integer |
|
514 | 3 | case 'i4': // 4-Byte Signed Integer |
|
515 | 3 | case 'i8': // 8-Byte Signed Integer |
|
516 | 3 | case 'int': // Integer |
|
517 | 2 | return (int) $propertyValue; |
|
518 | |||
519 | break; |
||
520 | 3 | case 'ui1': // 1-Byte Unsigned Integer |
|
521 | 3 | case 'ui2': // 2-Byte Unsigned Integer |
|
522 | 3 | case 'ui4': // 4-Byte Unsigned Integer |
|
523 | 3 | case 'ui8': // 8-Byte Unsigned Integer |
|
524 | 3 | case 'uint': // Unsigned Integer |
|
525 | return abs((int) $propertyValue); |
||
526 | |||
527 | break; |
||
528 | 3 | case 'r4': // 4-Byte Real Number |
|
529 | 3 | case 'r8': // 8-Byte Real Number |
|
530 | 3 | case 'decimal': // Decimal |
|
531 | 3 | return (float) $propertyValue; |
|
532 | |||
533 | break; |
||
534 | 3 | case 'lpstr': // LPSTR |
|
535 | 3 | case 'lpwstr': // LPWSTR |
|
536 | 3 | case 'bstr': // Basic String |
|
537 | 3 | return $propertyValue; |
|
538 | |||
539 | break; |
||
540 | 3 | case 'date': // Date and Time |
|
541 | 3 | case 'filetime': // File Time |
|
542 | 3 | return strtotime($propertyValue); |
|
543 | |||
544 | break; |
||
545 | 3 | case 'bool': // Boolean |
|
546 | 3 | return $propertyValue == 'true'; |
|
547 | |||
548 | break; |
||
549 | case 'cy': // Currency |
||
550 | case 'error': // Error Status Code |
||
551 | case 'vector': // Vector |
||
552 | case 'array': // Array |
||
553 | case 'blob': // Binary Blob |
||
554 | case 'oblob': // Binary Blob Object |
||
555 | case 'stream': // Binary Stream |
||
556 | case 'ostream': // Binary Stream Object |
||
557 | case 'storage': // Binary Storage |
||
558 | case 'ostorage': // Binary Storage Object |
||
559 | case 'vstream': // Binary Versioned Stream |
||
560 | case 'clsid': // Class ID |
||
561 | case 'cf': // Clipboard Data |
||
562 | return $propertyValue; |
||
563 | |||
564 | break; |
||
565 | } |
||
566 | |||
567 | return $propertyValue; |
||
568 | } |
||
569 | |||
570 | 3 | public static function convertPropertyType($propertyType) |
|
571 | { |
||
572 | 3 | switch ($propertyType) { |
|
573 | 3 | case 'i1': // 1-Byte Signed Integer |
|
574 | 3 | case 'i2': // 2-Byte Signed Integer |
|
575 | 3 | case 'i4': // 4-Byte Signed Integer |
|
576 | 3 | case 'i8': // 8-Byte Signed Integer |
|
577 | 3 | case 'int': // Integer |
|
578 | 3 | case 'ui1': // 1-Byte Unsigned Integer |
|
579 | 3 | case 'ui2': // 2-Byte Unsigned Integer |
|
580 | 3 | case 'ui4': // 4-Byte Unsigned Integer |
|
581 | 3 | case 'ui8': // 8-Byte Unsigned Integer |
|
582 | 3 | case 'uint': // Unsigned Integer |
|
583 | 2 | return self::PROPERTY_TYPE_INTEGER; |
|
584 | |||
585 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other
Loading history...
|
|||
586 | 3 | case 'r4': // 4-Byte Real Number |
|
587 | 3 | case 'r8': // 8-Byte Real Number |
|
588 | 3 | case 'decimal': // Decimal |
|
589 | 3 | return self::PROPERTY_TYPE_FLOAT; |
|
590 | |||
591 | break; |
||
592 | 3 | case 'empty': // Empty |
|
593 | 3 | case 'null': // Null |
|
594 | 3 | case 'lpstr': // LPSTR |
|
595 | 3 | case 'lpwstr': // LPWSTR |
|
596 | 3 | case 'bstr': // Basic String |
|
597 | 3 | return self::PROPERTY_TYPE_STRING; |
|
598 | |||
599 | break; |
||
600 | 3 | case 'date': // Date and Time |
|
601 | 3 | case 'filetime': // File Time |
|
602 | 3 | return self::PROPERTY_TYPE_DATE; |
|
603 | |||
604 | break; |
||
605 | 3 | case 'bool': // Boolean |
|
606 | 3 | return self::PROPERTY_TYPE_BOOLEAN; |
|
607 | |||
608 | break; |
||
609 | case 'cy': // Currency |
||
610 | case 'error': // Error Status Code |
||
611 | case 'vector': // Vector |
||
612 | case 'array': // Array |
||
613 | case 'blob': // Binary Blob |
||
614 | case 'oblob': // Binary Blob Object |
||
615 | case 'stream': // Binary Stream |
||
616 | case 'ostream': // Binary Stream Object |
||
617 | case 'storage': // Binary Storage |
||
618 | case 'ostorage': // Binary Storage Object |
||
619 | case 'vstream': // Binary Versioned Stream |
||
620 | case 'clsid': // Class ID |
||
621 | case 'cf': // Clipboard Data |
||
622 | return self::PROPERTY_TYPE_UNKNOWN; |
||
623 | |||
624 | break; |
||
625 | } |
||
626 | |||
627 | return self::PROPERTY_TYPE_UNKNOWN; |
||
628 | } |
||
629 | } |
||
630 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.