Skip to content

Commit 4e5bbb9

Browse files
committed
Refactor: Increase DocumentProperties cohesion
1 parent 9861342 commit 4e5bbb9

File tree

2 files changed

+106
-69
lines changed

2 files changed

+106
-69
lines changed

src/PhpWord/DocumentProperties.php

Lines changed: 105 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ public function getCreator()
137137
/**
138138
* Set Creator
139139
*
140-
* @param string $pValue
140+
* @param string $value
141141
* @return self
142142
*/
143-
public function setCreator($pValue = '')
143+
public function setCreator($value = '')
144144
{
145-
$this->creator = $pValue;
145+
$this->creator = $this->setValue($value, '');
146+
146147
return $this;
147148
}
148149

@@ -159,12 +160,13 @@ public function getLastModifiedBy()
159160
/**
160161
* Set Last Modified By
161162
*
162-
* @param string $pValue
163+
* @param string $value
163164
* @return self
164165
*/
165-
public function setLastModifiedBy($pValue = '')
166+
public function setLastModifiedBy($value = '')
166167
{
167-
$this->lastModifiedBy = $pValue;
168+
$this->lastModifiedBy = $this->setValue($value, '');
169+
168170
return $this;
169171
}
170172

@@ -181,15 +183,13 @@ public function getCreated()
181183
/**
182184
* Set Created
183185
*
184-
* @param int $pValue
186+
* @param int $value
185187
* @return self
186188
*/
187-
public function setCreated($pValue = null)
189+
public function setCreated($value = null)
188190
{
189-
if (is_null($pValue)) {
190-
$pValue = time();
191-
}
192-
$this->created = $pValue;
191+
$this->created = $this->setValue($value, time());
192+
193193
return $this;
194194
}
195195

@@ -206,15 +206,13 @@ public function getModified()
206206
/**
207207
* Set Modified
208208
*
209-
* @param int $pValue
209+
* @param int $value
210210
* @return self
211211
*/
212-
public function setModified($pValue = null)
212+
public function setModified($value = null)
213213
{
214-
if (is_null($pValue)) {
215-
$pValue = time();
216-
}
217-
$this->modified = $pValue;
214+
$this->modified = $this->setValue($value, time());
215+
218216
return $this;
219217
}
220218

@@ -231,12 +229,13 @@ public function getTitle()
231229
/**
232230
* Set Title
233231
*
234-
* @param string $pValue
232+
* @param string $value
235233
* @return self
236234
*/
237-
public function setTitle($pValue = '')
235+
public function setTitle($value = '')
238236
{
239-
$this->title = $pValue;
237+
$this->title = $this->setValue($value, '');
238+
240239
return $this;
241240
}
242241

@@ -253,12 +252,13 @@ public function getDescription()
253252
/**
254253
* Set Description
255254
*
256-
* @param string $pValue
255+
* @param string $value
257256
* @return self
258257
*/
259-
public function setDescription($pValue = '')
258+
public function setDescription($value = '')
260259
{
261-
$this->description = $pValue;
260+
$this->description = $this->setValue($value, '');
261+
262262
return $this;
263263
}
264264

@@ -275,12 +275,13 @@ public function getSubject()
275275
/**
276276
* Set Subject
277277
*
278-
* @param string $pValue
278+
* @param string $value
279279
* @return self
280280
*/
281-
public function setSubject($pValue = '')
281+
public function setSubject($value = '')
282282
{
283-
$this->subject = $pValue;
283+
$this->subject = $this->setValue($value, '');
284+
284285
return $this;
285286
}
286287

@@ -297,12 +298,13 @@ public function getKeywords()
297298
/**
298299
* Set Keywords
299300
*
300-
* @param string $pValue
301+
* @param string $value
301302
* @return self
302303
*/
303-
public function setKeywords($pValue = '')
304+
public function setKeywords($value = '')
304305
{
305-
$this->keywords = $pValue;
306+
$this->keywords = $this->setValue($value, '');
307+
306308
return $this;
307309
}
308310

@@ -319,12 +321,13 @@ public function getCategory()
319321
/**
320322
* Set Category
321323
*
322-
* @param string $pValue
324+
* @param string $value
323325
* @return self
324326
*/
325-
public function setCategory($pValue = '')
327+
public function setCategory($value = '')
326328
{
327-
$this->category = $pValue;
329+
$this->category = $this->setValue($value, '');
330+
328331
return $this;
329332
}
330333

@@ -341,12 +344,13 @@ public function getCompany()
341344
/**
342345
* Set Company
343346
*
344-
* @param string $pValue
347+
* @param string $value
345348
* @return self
346349
*/
347-
public function setCompany($pValue = '')
350+
public function setCompany($value = '')
348351
{
349-
$this->company = $pValue;
352+
$this->company = $this->setValue($value, '');
353+
350354
return $this;
351355
}
352356

@@ -363,12 +367,13 @@ public function getManager()
363367
/**
364368
* Set Manager
365369
*
366-
* @param string $pValue
370+
* @param string $value
367371
* @return self
368372
*/
369-
public function setManager($pValue = '')
373+
public function setManager($value = '')
370374
{
371-
$this->manager = $pValue;
375+
$this->manager = $this->setValue($value, '');
376+
372377
return $this;
373378
}
374379

@@ -401,7 +406,7 @@ public function isCustomPropertySet($propertyName)
401406
*/
402407
public function getCustomPropertyValue($propertyName)
403408
{
404-
if (isset($this->customProperties[$propertyName])) {
409+
if ($this->isCustomPropertySet($propertyName)) {
405410
return $this->customProperties[$propertyName]['value'];
406411
}
407412

@@ -415,7 +420,7 @@ public function getCustomPropertyValue($propertyName)
415420
*/
416421
public function getCustomPropertyType($propertyName)
417422
{
418-
if (isset($this->customProperties[$propertyName])) {
423+
if ($this->isCustomPropertySet($propertyName)) {
419424
return $this->customProperties[$propertyName]['type'];
420425
}
421426

@@ -473,33 +478,49 @@ public function setCustomProperty($propertyName, $propertyValue = '', $propertyT
473478
*/
474479
public static function convertProperty($propertyValue, $propertyType)
475480
{
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-
}
481+
switch ($propertyType) {
482+
case 'empty': // Empty
483+
return '';
484+
case 'null': // Null
485+
return null;
486+
case 'i1': // 1-Byte Signed Integer
487+
case 'i2': // 2-Byte Signed Integer
488+
case 'i4': // 4-Byte Signed Integer
489+
case 'i8': // 8-Byte Signed Integer
490+
case 'int': // Integer
491+
return (int) $propertyValue;
492+
case 'ui1': // 1-Byte Unsigned Integer
493+
case 'ui2': // 2-Byte Unsigned Integer
494+
case 'ui4': // 4-Byte Unsigned Integer
495+
case 'ui8': // 8-Byte Unsigned Integer
496+
case 'uint': // Unsigned Integer
497+
return abs((int) $propertyValue);
498+
case 'r4': // 4-Byte Real Number
499+
case 'r8': // 8-Byte Real Number
500+
case 'decimal': // Decimal
501+
return (float) $propertyValue;
502+
case 'date': // Date and Time
503+
case 'filetime': // File Time
504+
return strtotime($propertyValue);
505+
case 'bool': // Boolean
506+
return ($propertyValue == 'true') ? true : false;
507+
case 'lpstr': // LPSTR
508+
case 'lpwstr': // LPWSTR
509+
case 'bstr': // Basic String
510+
case 'cy': // Currency
511+
case 'error': // Error Status Code
512+
case 'vector': // Vector
513+
case 'array': // Array
514+
case 'blob': // Binary Blob
515+
case 'oblob': // Binary Blob Object
516+
case 'stream': // Binary Stream
517+
case 'ostream': // Binary Stream Object
518+
case 'storage': // Binary Storage
519+
case 'ostorage': // Binary Storage Object
520+
case 'vstream': // Binary Versioned Stream
521+
case 'clsid': // Class ID
522+
case 'cf': // Clipboard Data
523+
return $propertyValue;
503524
}
504525

505526
return $propertyValue;
@@ -528,4 +549,20 @@ public static function convertPropertyType($propertyType)
528549

529550
return self::PROPERTY_TYPE_UNKNOWN;
530551
}
552+
553+
/**
554+
* Set default for null and empty value
555+
*
556+
* @param mixed $value
557+
* @param mixed $default
558+
* @return mixed
559+
*/
560+
private function setValue($value, $default)
561+
{
562+
if (is_null($value) || $value == '') {
563+
$value = $default;
564+
}
565+
566+
return $value;
567+
}
531568
}

src/PhpWord/Style/AbstractStyle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function setStyleByArray($styles = array())
118118
}
119119

120120
/**
121-
* Set boolean value
121+
* Set default for null and empty value
122122
*
123123
* @param mixed $value
124124
* @param mixed $default

0 commit comments

Comments
 (0)