Skip to content

Commit 9861342

Browse files
committed
Refactor: Simplify DocumentProperties
1 parent 4a3400c commit 9861342

File tree

1 file changed

+39
-85
lines changed

1 file changed

+39
-85
lines changed

src/PhpWord/DocumentProperties.php

Lines changed: 39 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -473,50 +473,33 @@ public function setCustomProperty($propertyName, $propertyValue = '', $propertyT
473473
*/
474474
public static function convertProperty($propertyValue, $propertyType)
475475
{
476-
switch ($propertyType) {
477-
case 'empty': // Empty
478-
return '';
479-
case 'null': // Null
480-
return null;
481-
case 'i1': // 1-Byte Signed Integer
482-
case 'i2': // 2-Byte Signed Integer
483-
case 'i4': // 4-Byte Signed Integer
484-
case 'i8': // 8-Byte Signed Integer
485-
case 'int': // Integer
486-
return (int) $propertyValue;
487-
case 'ui1': // 1-Byte Unsigned Integer
488-
case 'ui2': // 2-Byte Unsigned Integer
489-
case 'ui4': // 4-Byte Unsigned Integer
490-
case 'ui8': // 8-Byte Unsigned Integer
491-
case 'uint': // Unsigned Integer
492-
return abs((int) $propertyValue);
493-
case 'r4': // 4-Byte Real Number
494-
case 'r8': // 8-Byte Real Number
495-
case 'decimal': // Decimal
496-
return (float) $propertyValue;
497-
case 'lpstr': // LPSTR
498-
case 'lpwstr': // LPWSTR
499-
case 'bstr': // Basic String
500-
return $propertyValue;
501-
case 'date': // Date and Time
502-
case 'filetime': // File Time
503-
return strtotime($propertyValue);
504-
case 'bool': // Boolean
505-
return ($propertyValue == 'true') ? true : false;
506-
case 'cy': // Currency
507-
case 'error': // Error Status Code
508-
case 'vector': // Vector
509-
case 'array': // Array
510-
case 'blob': // Binary Blob
511-
case 'oblob': // Binary Blob Object
512-
case 'stream': // Binary Stream
513-
case 'ostream': // Binary Stream Object
514-
case 'storage': // Binary Storage
515-
case 'ostorage': // Binary Storage Object
516-
case 'vstream': // Binary Versioned Stream
517-
case 'clsid': // Class ID
518-
case 'cf': // Clipboard Data
519-
return $propertyValue;
476+
$typeGroups = array(
477+
'empty' => array('empty'),
478+
'null' => array('null'),
479+
'int' => array('i1', 'i2', 'i4', 'i8', 'int'),
480+
'abs' => array('ui1', 'ui2', 'ui4', 'ui8', 'uint'),
481+
'float' => array('r4', 'r8', 'decimal'),
482+
'date' => array('date', 'filetime'),
483+
'bool' => array('bool'),
484+
);
485+
foreach ($typeGroups as $groupId => $groupMembers) {
486+
if (in_array($propertyType, $groupMembers)) {
487+
if ($groupId == 'null') {
488+
return null;
489+
} elseif ($groupId == 'int') {
490+
return (int) $propertyValue;
491+
} elseif ($groupId == 'abs') {
492+
return abs((int) $propertyValue);
493+
} elseif ($groupId == 'float') {
494+
return (float) $propertyValue;
495+
} elseif ($groupId == 'date') {
496+
return strtotime($propertyValue);
497+
} elseif ($groupId == 'bool') {
498+
return ($propertyValue == 'true') ? true : false;
499+
} else {
500+
return '';
501+
}
502+
}
520503
}
521504

522505
return $propertyValue;
@@ -530,48 +513,19 @@ public static function convertProperty($propertyValue, $propertyType)
530513
*/
531514
public static function convertPropertyType($propertyType)
532515
{
533-
switch ($propertyType) {
534-
case 'i1': // 1-Byte Signed Integer
535-
case 'i2': // 2-Byte Signed Integer
536-
case 'i4': // 4-Byte Signed Integer
537-
case 'i8': // 8-Byte Signed Integer
538-
case 'int': // Integer
539-
case 'ui1': // 1-Byte Unsigned Integer
540-
case 'ui2': // 2-Byte Unsigned Integer
541-
case 'ui4': // 4-Byte Unsigned Integer
542-
case 'ui8': // 8-Byte Unsigned Integer
543-
case 'uint': // Unsigned Integer
544-
return self::PROPERTY_TYPE_INTEGER;
545-
case 'r4': // 4-Byte Real Number
546-
case 'r8': // 8-Byte Real Number
547-
case 'decimal': // Decimal
548-
return self::PROPERTY_TYPE_FLOAT;
549-
case 'empty': // Empty
550-
case 'null': // Null
551-
case 'lpstr': // LPSTR
552-
case 'lpwstr': // LPWSTR
553-
case 'bstr': // Basic String
554-
return self::PROPERTY_TYPE_STRING;
555-
case 'date': // Date and Time
556-
case 'filetime': // File Time
557-
return self::PROPERTY_TYPE_DATE;
558-
case 'bool': // Boolean
559-
return self::PROPERTY_TYPE_BOOLEAN;
560-
case 'cy': // Currency
561-
case 'error': // Error Status Code
562-
case 'vector': // Vector
563-
case 'array': // Array
564-
case 'blob': // Binary Blob
565-
case 'oblob': // Binary Blob Object
566-
case 'stream': // Binary Stream
567-
case 'ostream': // Binary Stream Object
568-
case 'storage': // Binary Storage
569-
case 'ostorage': // Binary Storage Object
570-
case 'vstream': // Binary Versioned Stream
571-
case 'clsid': // Class ID
572-
case 'cf': // Clipboard Data
573-
return self::PROPERTY_TYPE_UNKNOWN;
516+
$typeGroups = array(
517+
self::PROPERTY_TYPE_INTEGER => array('i1', 'i2', 'i4', 'i8', 'int', 'ui1', 'ui2', 'ui4', 'ui8', 'uint'),
518+
self::PROPERTY_TYPE_FLOAT => array('r4', 'r8', 'decimal'),
519+
self::PROPERTY_TYPE_STRING => array('empty', 'null', 'lpstr', 'lpwstr', 'bstr'),
520+
self::PROPERTY_TYPE_DATE => array('date', 'filetime'),
521+
self::PROPERTY_TYPE_BOOLEAN => array('bool'),
522+
);
523+
foreach ($typeGroups as $groupId => $groupMembers) {
524+
if (in_array($propertyType, $groupMembers)) {
525+
return $groupId;
526+
}
574527
}
528+
575529
return self::PROPERTY_TYPE_UNKNOWN;
576530
}
577531
}

0 commit comments

Comments
 (0)