ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Properties.php
Go to the documentation of this file.
1<?php
2
4
5use DateTime;
7
9{
11 public const PROPERTY_TYPE_BOOLEAN = 'b';
12 public const PROPERTY_TYPE_INTEGER = 'i';
13 public const PROPERTY_TYPE_FLOAT = 'f';
14 public const PROPERTY_TYPE_DATE = 'd';
15 public const PROPERTY_TYPE_STRING = 's';
16 public const PROPERTY_TYPE_UNKNOWN = 'u';
17
18 private const VALID_PROPERTY_TYPE_LIST = [
24 ];
25
31 private $creator = 'Unknown Creator';
32
39
45 private $created;
46
52 private $modified;
53
59 private $title = 'Untitled Spreadsheet';
60
66 private $description = '';
67
73 private $subject = '';
74
80 private $keywords = '';
81
87 private $category = '';
88
94 private $manager = '';
95
101 private $company = '';
102
108 private $customProperties = [];
109
113 public function __construct()
114 {
115 // Initialise values
116 $this->lastModifiedBy = $this->creator;
117 $this->created = self::intOrFloatTimestamp(null);
118 $this->modified = self::intOrFloatTimestamp(null);
119 }
120
124 public function getCreator(): string
125 {
126 return $this->creator;
127 }
128
134 public function setCreator(string $creator): self
135 {
136 $this->creator = $creator;
137
138 return $this;
139 }
140
144 public function getLastModifiedBy(): string
145 {
147 }
148
154 public function setLastModifiedBy(string $modifier): self
155 {
156 $this->lastModifiedBy = $modifier;
157
158 return $this;
159 }
160
166 private static function intOrFloatTimestamp($timestamp)
167 {
168 if ($timestamp === null) {
169 $timestamp = (float) (new DateTime())->format('U');
170 } elseif (is_string($timestamp)) {
171 if (is_numeric($timestamp)) {
172 $timestamp = (float) $timestamp;
173 } else {
174 $timestamp = preg_replace('/[.][0-9]*$/', '', $timestamp) ?? '';
175 $timestamp = (float) (new DateTime($timestamp))->format('U');
176 }
177 }
178
180 }
181
187 public function getCreated()
188 {
189 return $this->created;
190 }
191
199 public function setCreated($timestamp): self
200 {
201 $this->created = self::intOrFloatTimestamp($timestamp);
202
203 return $this;
204 }
205
211 public function getModified()
212 {
213 return $this->modified;
214 }
215
223 public function setModified($timestamp): self
224 {
225 $this->modified = self::intOrFloatTimestamp($timestamp);
226
227 return $this;
228 }
229
233 public function getTitle(): string
234 {
235 return $this->title;
236 }
237
243 public function setTitle(string $title): self
244 {
245 $this->title = $title;
246
247 return $this;
248 }
249
253 public function getDescription(): string
254 {
255 return $this->description;
256 }
257
263 public function setDescription(string $description): self
264 {
265 $this->description = $description;
266
267 return $this;
268 }
269
273 public function getSubject(): string
274 {
275 return $this->subject;
276 }
277
283 public function setSubject(string $subject): self
284 {
285 $this->subject = $subject;
286
287 return $this;
288 }
289
293 public function getKeywords(): string
294 {
295 return $this->keywords;
296 }
297
303 public function setKeywords(string $keywords): self
304 {
305 $this->keywords = $keywords;
306
307 return $this;
308 }
309
313 public function getCategory(): string
314 {
315 return $this->category;
316 }
317
323 public function setCategory(string $category): self
324 {
325 $this->category = $category;
326
327 return $this;
328 }
329
333 public function getCompany(): string
334 {
335 return $this->company;
336 }
337
343 public function setCompany(string $company): self
344 {
345 $this->company = $company;
346
347 return $this;
348 }
349
353 public function getManager(): string
354 {
355 return $this->manager;
356 }
357
363 public function setManager(string $manager): self
364 {
365 $this->manager = $manager;
366
367 return $this;
368 }
369
375 public function getCustomProperties(): array
376 {
377 return array_keys($this->customProperties);
378 }
379
383 public function isCustomPropertySet(string $propertyName): bool
384 {
385 return array_key_exists($propertyName, $this->customProperties);
386 }
387
393 public function getCustomPropertyValue(string $propertyName)
394 {
395 if (isset($this->customProperties[$propertyName])) {
396 return $this->customProperties[$propertyName]['value'];
397 }
398
399 return null;
400 }
401
407 public function getCustomPropertyType(string $propertyName)
408 {
409 return $this->customProperties[$propertyName]['type'] ?? null;
410 }
411
415 private function identifyPropertyType($propertyValue): string
416 {
417 if (is_float($propertyValue)) {
419 }
420 if (is_int($propertyValue)) {
422 }
423 if (is_bool($propertyValue)) {
425 }
426
428 }
429
443 public function setCustomProperty(string $propertyName, $propertyValue = '', $propertyType = null): self
444 {
445 if (($propertyType === null) || (!in_array($propertyType, self::VALID_PROPERTY_TYPE_LIST))) {
446 $propertyType = $this->identifyPropertyType($propertyValue);
447 }
448
449 if (!is_object($propertyValue)) {
450 $this->customProperties[$propertyName] = [
451 'value' => self::convertProperty($propertyValue, $propertyType),
452 'type' => $propertyType,
453 ];
454 }
455
456 return $this;
457 }
458
459 private const PROPERTY_TYPE_ARRAY = [
460 'i' => self::PROPERTY_TYPE_INTEGER, // Integer
461 'i1' => self::PROPERTY_TYPE_INTEGER, // 1-Byte Signed Integer
462 'i2' => self::PROPERTY_TYPE_INTEGER, // 2-Byte Signed Integer
463 'i4' => self::PROPERTY_TYPE_INTEGER, // 4-Byte Signed Integer
464 'i8' => self::PROPERTY_TYPE_INTEGER, // 8-Byte Signed Integer
465 'int' => self::PROPERTY_TYPE_INTEGER, // Integer
466 'ui1' => self::PROPERTY_TYPE_INTEGER, // 1-Byte Unsigned Integer
467 'ui2' => self::PROPERTY_TYPE_INTEGER, // 2-Byte Unsigned Integer
468 'ui4' => self::PROPERTY_TYPE_INTEGER, // 4-Byte Unsigned Integer
469 'ui8' => self::PROPERTY_TYPE_INTEGER, // 8-Byte Unsigned Integer
470 'uint' => self::PROPERTY_TYPE_INTEGER, // Unsigned Integer
471 'f' => self::PROPERTY_TYPE_FLOAT, // Real Number
472 'r4' => self::PROPERTY_TYPE_FLOAT, // 4-Byte Real Number
473 'r8' => self::PROPERTY_TYPE_FLOAT, // 8-Byte Real Number
474 'decimal' => self::PROPERTY_TYPE_FLOAT, // Decimal
475 's' => self::PROPERTY_TYPE_STRING, // String
476 'empty' => self::PROPERTY_TYPE_STRING, // Empty
477 'null' => self::PROPERTY_TYPE_STRING, // Null
478 'lpstr' => self::PROPERTY_TYPE_STRING, // LPSTR
479 'lpwstr' => self::PROPERTY_TYPE_STRING, // LPWSTR
480 'bstr' => self::PROPERTY_TYPE_STRING, // Basic String
481 'd' => self::PROPERTY_TYPE_DATE, // Date and Time
482 'date' => self::PROPERTY_TYPE_DATE, // Date and Time
483 'filetime' => self::PROPERTY_TYPE_DATE, // File Time
484 'b' => self::PROPERTY_TYPE_BOOLEAN, // Boolean
485 'bool' => self::PROPERTY_TYPE_BOOLEAN, // Boolean
486 ];
487
488 private const SPECIAL_TYPES = [
489 'empty' => '',
490 'null' => null,
491 ];
492
500 public static function convertProperty($propertyValue, string $propertyType)
501 {
502 return self::SPECIAL_TYPES[$propertyType] ?? self::convertProperty2($propertyValue, $propertyType);
503 }
504
512 private static function convertProperty2($propertyValue, string $type)
513 {
514 $propertyType = self::convertPropertyType($type);
515 switch ($propertyType) {
517 $intValue = (int) $propertyValue;
518
519 return ($type[0] === 'u') ? abs($intValue) : $intValue;
521 return (float) $propertyValue;
523 return self::intOrFloatTimestamp($propertyValue);
525 return is_bool($propertyValue) ? $propertyValue : ($propertyValue === 'true');
526 default: // includes string
527 return $propertyValue;
528 }
529 }
530
531 public static function convertPropertyType(string $propertyType): string
532 {
533 return self::PROPERTY_TYPE_ARRAY[$propertyType] ?? self::PROPERTY_TYPE_UNKNOWN;
534 }
535}
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
An exception for terminatinating execution or to throw for unit testing.
setCustomProperty(string $propertyName, $propertyValue='', $propertyType=null)
Set a Custom Property.
Definition: Properties.php:443
setCreator(string $creator)
Set Creator.
Definition: Properties.php:134
getCustomPropertyType(string $propertyName)
Get a Custom Property Type.
Definition: Properties.php:407
static convertProperty($propertyValue, string $propertyType)
Convert property to form desired by Excel.
Definition: Properties.php:500
getCustomPropertyValue(string $propertyName)
Get a Custom Property Value.
Definition: Properties.php:393
setLastModifiedBy(string $modifier)
Set Last Modified By.
Definition: Properties.php:154
static convertProperty2($propertyValue, string $type)
Convert property to form desired by Excel.
Definition: Properties.php:512
isCustomPropertySet(string $propertyName)
Check if a Custom Property is defined.
Definition: Properties.php:383
setSubject(string $subject)
Set Subject.
Definition: Properties.php:283
__construct()
Create a new Document Properties instance.
Definition: Properties.php:113
setCompany(string $company)
Set Company.
Definition: Properties.php:343
setDescription(string $description)
Set Description.
Definition: Properties.php:263
setManager(string $manager)
Set Manager.
Definition: Properties.php:363
getCustomProperties()
Get a List of Custom Property Names.
Definition: Properties.php:375
setCategory(string $category)
Set Category.
Definition: Properties.php:323
setKeywords(string $keywords)
Set Keywords.
Definition: Properties.php:303
static convertPropertyType(string $propertyType)
Definition: Properties.php:531
static evaluate($value)
Help some functions with large results operate correctly on 32-bit, by returning result as int when p...
Definition: IntOrFloat.php:15
$type