| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Document; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\IntOrFloat; |
| 7 |
|
| 8 |
class Properties |
| 9 |
{ |
| 10 |
/** constants */ |
| 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 = [ |
| 19 |
self::PROPERTY_TYPE_BOOLEAN, |
| 20 |
self::PROPERTY_TYPE_INTEGER, |
| 21 |
self::PROPERTY_TYPE_FLOAT, |
| 22 |
self::PROPERTY_TYPE_DATE, |
| 23 |
self::PROPERTY_TYPE_STRING, |
| 24 |
]; |
| 25 |
|
| 26 |
/** |
| 27 |
* Creator. |
| 28 |
*/ |
| 29 |
private string $creator = 'Unknown Creator'; |
| 30 |
|
| 31 |
/** |
| 32 |
* LastModifiedBy. |
| 33 |
*/ |
| 34 |
private string $lastModifiedBy; |
| 35 |
|
| 36 |
/** |
| 37 |
* Created. |
| 38 |
* @var float|int |
| 39 |
*/ |
| 40 |
private $created; |
| 41 |
|
| 42 |
/** |
| 43 |
* Modified. |
| 44 |
* @var float|int |
| 45 |
*/ |
| 46 |
private $modified; |
| 47 |
|
| 48 |
/** |
| 49 |
* Title. |
| 50 |
*/ |
| 51 |
private string $title = 'Untitled Spreadsheet'; |
| 52 |
|
| 53 |
/** |
| 54 |
* Description. |
| 55 |
*/ |
| 56 |
private string $description = ''; |
| 57 |
|
| 58 |
/** |
| 59 |
* Subject. |
| 60 |
*/ |
| 61 |
private string $subject = ''; |
| 62 |
|
| 63 |
/** |
| 64 |
* Keywords. |
| 65 |
*/ |
| 66 |
private string $keywords = ''; |
| 67 |
|
| 68 |
/** |
| 69 |
* Category. |
| 70 |
*/ |
| 71 |
private string $category = ''; |
| 72 |
|
| 73 |
/** |
| 74 |
* Manager. |
| 75 |
*/ |
| 76 |
private string $manager = ''; |
| 77 |
|
| 78 |
/** |
| 79 |
* Company. |
| 80 |
*/ |
| 81 |
private string $company = ''; |
| 82 |
|
| 83 |
/** |
| 84 |
* Custom Properties. |
| 85 |
* |
| 86 |
* @var array{value: null|bool|float|int|string, type: string}[] |
| 87 |
*/ |
| 88 |
private array $customProperties = []; |
| 89 |
|
| 90 |
private string $hyperlinkBase = ''; |
| 91 |
|
| 92 |
private string $viewport = ''; |
| 93 |
|
| 94 |
/** |
| 95 |
* Create a new Document Properties instance. |
| 96 |
*/ |
| 97 |
public function __construct() |
| 98 |
{ |
| 99 |
// Initialise values |
| 100 |
$this->lastModifiedBy = $this->creator; |
| 101 |
$this->created = self::intOrFloatTimestamp(null); |
| 102 |
$this->modified = $this->created; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get Creator. |
| 107 |
*/ |
| 108 |
public function getCreator(): string |
| 109 |
{ |
| 110 |
return $this->creator; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Set Creator. |
| 115 |
* |
| 116 |
* @return $this |
| 117 |
*/ |
| 118 |
public function setCreator(string $creator): self |
| 119 |
{ |
| 120 |
$this->creator = $creator; |
| 121 |
|
| 122 |
return $this; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get Last Modified By. |
| 127 |
*/ |
| 128 |
public function getLastModifiedBy(): string |
| 129 |
{ |
| 130 |
return $this->lastModifiedBy; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Set Last Modified By. |
| 135 |
* |
| 136 |
* @return $this |
| 137 |
*/ |
| 138 |
public function setLastModifiedBy(string $modifiedBy): self |
| 139 |
{ |
| 140 |
$this->lastModifiedBy = $modifiedBy; |
| 141 |
|
| 142 |
return $this; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @param null|bool|float|int|string $timestamp |
| 147 |
* @return float|int |
| 148 |
*/ |
| 149 |
private static function intOrFloatTimestamp($timestamp) |
| 150 |
{ |
| 151 |
if ($timestamp === null || is_bool($timestamp)) { |
| 152 |
$timestamp = (float) (new DateTime())->format('U'); |
| 153 |
} elseif (is_string($timestamp)) { |
| 154 |
if (is_numeric($timestamp)) { |
| 155 |
$timestamp = (float) $timestamp; |
| 156 |
} else { |
| 157 |
$timestamp = (string) preg_replace('/[.][0-9]*$/', '', $timestamp); |
| 158 |
$timestamp = (string) preg_replace('/^(\d{4})- (\d)/', '$1-0$2', $timestamp); |
| 159 |
$timestamp = (string) preg_replace('/^(\d{4}-\d{2})- (\d)/', '$1-0$2', $timestamp); |
| 160 |
$timestamp = (float) (new DateTime($timestamp))->format('U'); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return IntOrFloat::evaluate($timestamp); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get Created. |
| 169 |
* @return float|int |
| 170 |
*/ |
| 171 |
public function getCreated() |
| 172 |
{ |
| 173 |
return $this->created; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Set Created. |
| 178 |
* |
| 179 |
* @return $this |
| 180 |
* @param null|float|int|string $timestamp |
| 181 |
*/ |
| 182 |
public function setCreated($timestamp): self |
| 183 |
{ |
| 184 |
$this->created = self::intOrFloatTimestamp($timestamp); |
| 185 |
|
| 186 |
return $this; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get Modified. |
| 191 |
* @return float|int |
| 192 |
*/ |
| 193 |
public function getModified() |
| 194 |
{ |
| 195 |
return $this->modified; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Set Modified. |
| 200 |
* |
| 201 |
* @return $this |
| 202 |
* @param null|float|int|string $timestamp |
| 203 |
*/ |
| 204 |
public function setModified($timestamp): self |
| 205 |
{ |
| 206 |
$this->modified = self::intOrFloatTimestamp($timestamp); |
| 207 |
|
| 208 |
return $this; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get Title. |
| 213 |
*/ |
| 214 |
public function getTitle(): string |
| 215 |
{ |
| 216 |
return $this->title; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Set Title. |
| 221 |
* |
| 222 |
* @return $this |
| 223 |
*/ |
| 224 |
public function setTitle(string $title): self |
| 225 |
{ |
| 226 |
$this->title = $title; |
| 227 |
|
| 228 |
return $this; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get Description. |
| 233 |
*/ |
| 234 |
public function getDescription(): string |
| 235 |
{ |
| 236 |
return $this->description; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Set Description. |
| 241 |
* |
| 242 |
* @return $this |
| 243 |
*/ |
| 244 |
public function setDescription(string $description): self |
| 245 |
{ |
| 246 |
$this->description = $description; |
| 247 |
|
| 248 |
return $this; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get Subject. |
| 253 |
*/ |
| 254 |
public function getSubject(): string |
| 255 |
{ |
| 256 |
return $this->subject; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Set Subject. |
| 261 |
* |
| 262 |
* @return $this |
| 263 |
*/ |
| 264 |
public function setSubject(string $subject): self |
| 265 |
{ |
| 266 |
$this->subject = $subject; |
| 267 |
|
| 268 |
return $this; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Get Keywords. |
| 273 |
*/ |
| 274 |
public function getKeywords(): string |
| 275 |
{ |
| 276 |
return $this->keywords; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Set Keywords. |
| 281 |
* |
| 282 |
* @return $this |
| 283 |
*/ |
| 284 |
public function setKeywords(string $keywords): self |
| 285 |
{ |
| 286 |
$this->keywords = $keywords; |
| 287 |
|
| 288 |
return $this; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Get Category. |
| 293 |
*/ |
| 294 |
public function getCategory(): string |
| 295 |
{ |
| 296 |
return $this->category; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Set Category. |
| 301 |
* |
| 302 |
* @return $this |
| 303 |
*/ |
| 304 |
public function setCategory(string $category): self |
| 305 |
{ |
| 306 |
$this->category = $category; |
| 307 |
|
| 308 |
return $this; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Get Company. |
| 313 |
*/ |
| 314 |
public function getCompany(): string |
| 315 |
{ |
| 316 |
return $this->company; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Set Company. |
| 321 |
* |
| 322 |
* @return $this |
| 323 |
*/ |
| 324 |
public function setCompany(string $company): self |
| 325 |
{ |
| 326 |
$this->company = $company; |
| 327 |
|
| 328 |
return $this; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Get Manager. |
| 333 |
*/ |
| 334 |
public function getManager(): string |
| 335 |
{ |
| 336 |
return $this->manager; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Set Manager. |
| 341 |
* |
| 342 |
* @return $this |
| 343 |
*/ |
| 344 |
public function setManager(string $manager): self |
| 345 |
{ |
| 346 |
$this->manager = $manager; |
| 347 |
|
| 348 |
return $this; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Get a List of Custom Property Names. |
| 353 |
* |
| 354 |
* @return string[] |
| 355 |
*/ |
| 356 |
public function getCustomProperties(): array |
| 357 |
{ |
| 358 |
return array_keys($this->customProperties); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Check if a Custom Property is defined. |
| 363 |
*/ |
| 364 |
public function isCustomPropertySet(string $propertyName): bool |
| 365 |
{ |
| 366 |
return array_key_exists($propertyName, $this->customProperties); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Get a Custom Property Value. |
| 371 |
* @return bool|int|float|string|null |
| 372 |
*/ |
| 373 |
public function getCustomPropertyValue(string $propertyName) |
| 374 |
{ |
| 375 |
if (isset($this->customProperties[$propertyName])) { |
| 376 |
return $this->customProperties[$propertyName]['value']; |
| 377 |
} |
| 378 |
|
| 379 |
return null; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Get a Custom Property Type. |
| 384 |
*/ |
| 385 |
public function getCustomPropertyType(string $propertyName): ?string |
| 386 |
{ |
| 387 |
return $this->customProperties[$propertyName]['type'] ?? null; |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* @param bool|int|float|string|null $propertyValue |
| 392 |
*/ |
| 393 |
private function identifyPropertyType($propertyValue): string |
| 394 |
{ |
| 395 |
if (is_float($propertyValue)) { |
| 396 |
return self::PROPERTY_TYPE_FLOAT; |
| 397 |
} |
| 398 |
if (is_int($propertyValue)) { |
| 399 |
return self::PROPERTY_TYPE_INTEGER; |
| 400 |
} |
| 401 |
if (is_bool($propertyValue)) { |
| 402 |
return self::PROPERTY_TYPE_BOOLEAN; |
| 403 |
} |
| 404 |
|
| 405 |
return self::PROPERTY_TYPE_STRING; |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Set a Custom Property. |
| 410 |
* |
| 411 |
* @param ?string $propertyType see `self::VALID_PROPERTY_TYPE_LIST` |
| 412 |
* |
| 413 |
* @return $this |
| 414 |
* @param bool|int|float|string|null $propertyValue |
| 415 |
*/ |
| 416 |
public function setCustomProperty(string $propertyName, $propertyValue = '', ?string $propertyType = null): self |
| 417 |
{ |
| 418 |
if (($propertyType === null) || (!in_array($propertyType, self::VALID_PROPERTY_TYPE_LIST))) { |
| 419 |
$propertyType = $this->identifyPropertyType($propertyValue); |
| 420 |
} |
| 421 |
|
| 422 |
$this->customProperties[$propertyName] = [ |
| 423 |
'value' => self::convertProperty($propertyValue, $propertyType), |
| 424 |
'type' => $propertyType, |
| 425 |
]; |
| 426 |
|
| 427 |
return $this; |
| 428 |
} |
| 429 |
|
| 430 |
private const PROPERTY_TYPE_ARRAY = [ |
| 431 |
'i' => self::PROPERTY_TYPE_INTEGER, // Integer |
| 432 |
'i1' => self::PROPERTY_TYPE_INTEGER, // 1-Byte Signed Integer |
| 433 |
'i2' => self::PROPERTY_TYPE_INTEGER, // 2-Byte Signed Integer |
| 434 |
'i4' => self::PROPERTY_TYPE_INTEGER, // 4-Byte Signed Integer |
| 435 |
'i8' => self::PROPERTY_TYPE_INTEGER, // 8-Byte Signed Integer |
| 436 |
'int' => self::PROPERTY_TYPE_INTEGER, // Integer |
| 437 |
'ui1' => self::PROPERTY_TYPE_INTEGER, // 1-Byte Unsigned Integer |
| 438 |
'ui2' => self::PROPERTY_TYPE_INTEGER, // 2-Byte Unsigned Integer |
| 439 |
'ui4' => self::PROPERTY_TYPE_INTEGER, // 4-Byte Unsigned Integer |
| 440 |
'ui8' => self::PROPERTY_TYPE_INTEGER, // 8-Byte Unsigned Integer |
| 441 |
'uint' => self::PROPERTY_TYPE_INTEGER, // Unsigned Integer |
| 442 |
'f' => self::PROPERTY_TYPE_FLOAT, // Real Number |
| 443 |
'r4' => self::PROPERTY_TYPE_FLOAT, // 4-Byte Real Number |
| 444 |
'r8' => self::PROPERTY_TYPE_FLOAT, // 8-Byte Real Number |
| 445 |
'decimal' => self::PROPERTY_TYPE_FLOAT, // Decimal |
| 446 |
's' => self::PROPERTY_TYPE_STRING, // String |
| 447 |
'empty' => self::PROPERTY_TYPE_STRING, // Empty |
| 448 |
'null' => self::PROPERTY_TYPE_STRING, // Null |
| 449 |
'lpstr' => self::PROPERTY_TYPE_STRING, // LPSTR |
| 450 |
'lpwstr' => self::PROPERTY_TYPE_STRING, // LPWSTR |
| 451 |
'bstr' => self::PROPERTY_TYPE_STRING, // Basic String |
| 452 |
'd' => self::PROPERTY_TYPE_DATE, // Date and Time |
| 453 |
'date' => self::PROPERTY_TYPE_DATE, // Date and Time |
| 454 |
'filetime' => self::PROPERTY_TYPE_DATE, // File Time |
| 455 |
'b' => self::PROPERTY_TYPE_BOOLEAN, // Boolean |
| 456 |
'bool' => self::PROPERTY_TYPE_BOOLEAN, // Boolean |
| 457 |
]; |
| 458 |
|
| 459 |
private const SPECIAL_TYPES = [ |
| 460 |
'empty' => '', |
| 461 |
'null' => null, |
| 462 |
]; |
| 463 |
|
| 464 |
/** |
| 465 |
* Convert property to form desired by Excel. |
| 466 |
* @param bool|int|float|string|null $propertyValue |
| 467 |
* @return bool|int|float|string|null |
| 468 |
*/ |
| 469 |
public static function convertProperty($propertyValue, string $propertyType) |
| 470 |
{ |
| 471 |
return self::SPECIAL_TYPES[$propertyType] ?? self::convertProperty2($propertyValue, $propertyType); |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Convert property to form desired by Excel. |
| 476 |
* @param bool|int|float|string|null $propertyValue |
| 477 |
* @return bool|int|float|string|null |
| 478 |
*/ |
| 479 |
private static function convertProperty2($propertyValue, string $type) |
| 480 |
{ |
| 481 |
$propertyType = self::convertPropertyType($type); |
| 482 |
switch ($propertyType) { |
| 483 |
case self::PROPERTY_TYPE_INTEGER: |
| 484 |
$intValue = (int) $propertyValue; |
| 485 |
|
| 486 |
return ($type[0] === 'u') ? abs($intValue) : $intValue; |
| 487 |
case self::PROPERTY_TYPE_FLOAT: |
| 488 |
return (float) $propertyValue; |
| 489 |
case self::PROPERTY_TYPE_DATE: |
| 490 |
return self::intOrFloatTimestamp($propertyValue); |
| 491 |
case self::PROPERTY_TYPE_BOOLEAN: |
| 492 |
return is_bool($propertyValue) ? $propertyValue : ($propertyValue === 'true'); |
| 493 |
default: // includes string |
| 494 |
return $propertyValue; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
public static function convertPropertyType(string $propertyType): string |
| 499 |
{ |
| 500 |
return self::PROPERTY_TYPE_ARRAY[$propertyType] ?? self::PROPERTY_TYPE_UNKNOWN; |
| 501 |
} |
| 502 |
|
| 503 |
public function getHyperlinkBase(): string |
| 504 |
{ |
| 505 |
return $this->hyperlinkBase; |
| 506 |
} |
| 507 |
|
| 508 |
public function setHyperlinkBase(string $hyperlinkBase): self |
| 509 |
{ |
| 510 |
$this->hyperlinkBase = $hyperlinkBase; |
| 511 |
|
| 512 |
return $this; |
| 513 |
} |
| 514 |
|
| 515 |
public function getViewport(): string |
| 516 |
{ |
| 517 |
return $this->viewport; |
| 518 |
} |
| 519 |
|
| 520 |
public const SUGGESTED_VIEWPORT = 'width=device-width, initial-scale=1'; |
| 521 |
|
| 522 |
public function setViewport(string $viewport): self |
| 523 |
{ |
| 524 |
$this->viewport = $viewport; |
| 525 |
|
| 526 |
return $this; |
| 527 |
} |
| 528 |
} |
| 529 |
|