Skip to content

Commit 136f549

Browse files
Progi1984tugmaks
andauthored
Reader Word2007 : Respect paragraph indent units (#2726)
Co-authored-by: Maksim Tiugaev <tugmaks@yandex.ru>
1 parent 337dc27 commit 136f549

File tree

7 files changed

+308
-75
lines changed

7 files changed

+308
-75
lines changed

docs/changes/1.x/1.4.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
- Writer ODText: Support for images inside a textRun by [@Progi1984](https://github.com/Progi1984) fixing [#2240](https://github.com/PHPOffice/PHPWord/issues/2240) in [#2668](https://github.com/PHPOffice/PHPWord/pull/2668)
1717
- Allow vAlign and vMerge on Style\Cell to be set to null by [@SpraxDev](https://github.com/SpraxDev) fixing [#2673](https://github.com/PHPOffice/PHPWord/issues/2673) in [#2676](https://github.com/PHPOffice/PHPWord/pull/2676)
1818
- Reader HTML: Support for differents size units for table by [@Progi1984](https://github.com/Progi1984) fixing [#2384](https://github.com/PHPOffice/PHPWord/issues/2384), [#2701](https://github.com/PHPOffice/PHPWord/issues/2701) in [#2725](https://github.com/PHPOffice/PHPWord/pull/2725)
19+
- Reader Word2007 : Respect paragraph indent units by [@tugmaks](https://github.com/tugmaks) & [@Progi1984](https://github.com/Progi1984) fixing [#507](https://github.com/PHPOffice/PHPWord/issues/507) in [#2726](https://github.com/PHPOffice/PHPWord/pull/2726)
1920

2021
### Miscellaneous
2122

2223
- Bump dompdf/dompdf from 2.0.4 to 3.0.0 by [@dependabot](https://github.com/dependabot) fixing [#2621](https://github.com/PHPOffice/PHPWord/issues/2621) in [#2666](https://github.com/PHPOffice/PHPWord/pull/2666)
2324
- Add test case to make sure vMerge defaults to 'continue' by [@SpraxDev](https://github.com/SpraxDev) in [#2677](https://github.com/PHPOffice/PHPWord/pull/2677)
2425

26+
### Deprecations
27+
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::getIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::getIndentLeft()`
28+
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::setHanging()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentHanging()`
29+
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::setIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentLeft()`
30+
2531
### BC Breaks

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,10 @@ protected function readParagraphStyle(XMLReader $xmlReader, DOMElement $domNode)
662662
'alignment' => [self::READ_VALUE, 'w:jc'],
663663
'basedOn' => [self::READ_VALUE, 'w:basedOn'],
664664
'next' => [self::READ_VALUE, 'w:next'],
665-
'indent' => [self::READ_VALUE, 'w:ind', 'w:left'],
666-
'hanging' => [self::READ_VALUE, 'w:ind', 'w:hanging'],
665+
'indentLeft' => [self::READ_VALUE, 'w:ind', 'w:left'],
666+
'indentRight' => [self::READ_VALUE, 'w:ind', 'w:right'],
667+
'indentHanging' => [self::READ_VALUE, 'w:ind', 'w:hanging'],
668+
'indentFirstLine' => [self::READ_VALUE, 'w:ind', 'w:firstLine'],
667669
'spaceAfter' => [self::READ_VALUE, 'w:spacing', 'w:after'],
668670
'spaceBefore' => [self::READ_VALUE, 'w:spacing', 'w:before'],
669671
'widowControl' => [self::READ_FALSE, 'w:widowControl'],

src/PhpWord/Style/AbstractStyle.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,11 @@ protected function setEnumVal($value = null, $enum = [], $default = null)
317317
* Set object value.
318318
*
319319
* @param mixed $value
320-
* @param string $styleName
321320
* @param mixed &$style
322321
*
323322
* @return mixed
324323
*/
325-
protected function setObjectVal($value, $styleName, &$style)
324+
protected function setObjectVal($value, string $styleName, &$style)
326325
{
327326
$styleClass = substr(static::class, 0, (int) strrpos(static::class, '\\')) . '\\' . $styleName;
328327
if (is_array($value)) {

src/PhpWord/Style/Indentation.php

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,30 @@ class Indentation extends AbstractStyle
2929
/**
3030
* Left indentation (twip).
3131
*
32-
* @var float|int
32+
* @var null|float
3333
*/
3434
private $left = 0;
3535

3636
/**
3737
* Right indentation (twip).
3838
*
39-
* @var float|int
39+
* @var null|float
4040
*/
4141
private $right = 0;
4242

4343
/**
4444
* Additional first line indentation (twip).
4545
*
46-
* @var float|int
46+
* @var null|float
4747
*/
4848
private $firstLine = 0;
4949

5050
/**
5151
* Indentation removed from first line (twip).
5252
*
53-
* @var float|int
53+
* @var null|float
5454
*/
55-
private $hanging;
55+
private $hanging = 0;
5656

5757
/**
5858
* Create a new instance.
@@ -66,96 +66,72 @@ public function __construct($style = [])
6666

6767
/**
6868
* Get left.
69-
*
70-
* @return float|int
7169
*/
72-
public function getLeft()
70+
public function getLeft(): ?float
7371
{
7472
return $this->left;
7573
}
7674

7775
/**
7876
* Set left.
79-
*
80-
* @param float|int $value
81-
*
82-
* @return self
8377
*/
84-
public function setLeft($value)
78+
public function setLeft(?float $value): self
8579
{
86-
$this->left = $this->setNumericVal($value, $this->left);
80+
$this->left = $this->setNumericVal($value);
8781

8882
return $this;
8983
}
9084

9185
/**
9286
* Get right.
93-
*
94-
* @return float|int
9587
*/
96-
public function getRight()
88+
public function getRight(): ?float
9789
{
9890
return $this->right;
9991
}
10092

10193
/**
10294
* Set right.
103-
*
104-
* @param float|int $value
105-
*
106-
* @return self
10795
*/
108-
public function setRight($value)
96+
public function setRight(?float $value): self
10997
{
110-
$this->right = $this->setNumericVal($value, $this->right);
98+
$this->right = $this->setNumericVal($value);
11199

112100
return $this;
113101
}
114102

115103
/**
116104
* Get first line.
117-
*
118-
* @return float|int
119105
*/
120-
public function getFirstLine()
106+
public function getFirstLine(): ?float
121107
{
122108
return $this->firstLine;
123109
}
124110

125111
/**
126112
* Set first line.
127-
*
128-
* @param float|int $value
129-
*
130-
* @return self
131113
*/
132-
public function setFirstLine($value)
114+
public function setFirstLine(?float $value): self
133115
{
134-
$this->firstLine = $this->setNumericVal($value, $this->firstLine);
116+
$this->firstLine = $this->setNumericVal($value);
135117

136118
return $this;
137119
}
138120

139121
/**
140122
* Get hanging.
141-
*
142-
* @return float|int
143123
*/
144-
public function getHanging()
124+
public function getHanging(): ?float
145125
{
146126
return $this->hanging;
147127
}
148128

149129
/**
150130
* Set hanging.
151-
*
152-
* @param float|int $value
153-
*
154-
* @return self
155131
*/
156-
public function setHanging($value = null)
132+
public function setHanging(?float $value = null): self
157133
{
158-
$this->hanging = $this->setNumericVal($value, $this->hanging);
134+
$this->hanging = $this->setNumericVal($value);
159135

160136
return $this;
161137
}

src/PhpWord/Style/Paragraph.php

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -322,74 +322,132 @@ public function setNext($value = null)
322322
return $this;
323323
}
324324

325+
/**
326+
* Get hanging.
327+
*/
328+
public function getHanging(): ?float
329+
{
330+
return $this->getChildStyleValue($this->indentation, 'hanging');
331+
}
332+
325333
/**
326334
* Get indentation.
327335
*
328-
* @return null|Indentation
336+
* @deprecated 1.4.0 Use getIndentLeft
329337
*/
330-
public function getIndentation()
338+
public function getIndent(): ?float
339+
{
340+
return $this->getChildStyleValue($this->indentation, 'left');
341+
}
342+
343+
/**
344+
* Get indentation.
345+
*/
346+
public function getIndentation(): ?Indentation
331347
{
332348
return $this->indentation;
333349
}
334350

335351
/**
336-
* Set shading.
337-
*
338-
* @param mixed $value
339-
*
340-
* @return self
352+
* Get firstLine.
341353
*/
342-
public function setIndentation($value = null)
354+
public function getIndentFirstLine(): ?float
343355
{
344-
$this->setObjectVal($value, 'Indentation', $this->indentation);
356+
return $this->getChildStyleValue($this->indentation, 'firstLine');
357+
}
345358

346-
return $this;
359+
/**
360+
* Get left indentation.
361+
*/
362+
public function getIndentLeft(): ?float
363+
{
364+
return $this->getChildStyleValue($this->indentation, 'left');
347365
}
348366

349367
/**
350-
* Get indentation.
368+
* Get right indentation.
369+
*/
370+
public function getIndentRight(): ?float
371+
{
372+
return $this->getChildStyleValue($this->indentation, 'right');
373+
}
374+
375+
/**
376+
* Set hanging.
351377
*
352-
* @return int
378+
* @deprecated 1.4.0 Use setIndentHanging
353379
*/
354-
public function getIndent()
380+
public function setHanging(?float $value = null): self
355381
{
356-
return $this->getChildStyleValue($this->indentation, 'left');
382+
return $this->setIndentation(['hanging' => $value]);
357383
}
358384

359385
/**
360386
* Set indentation.
361387
*
362-
* @param int $value
363-
*
364-
* @return self
388+
* @deprecated 1.4.0 Use setIndentLeft
365389
*/
366-
public function setIndent($value = null)
390+
public function setIndent(?float $value = null): self
367391
{
368392
return $this->setIndentation(['left' => $value]);
369393
}
370394

371395
/**
372-
* Get hanging.
396+
* Set indentation.
373397
*
374-
* @return int
398+
* @param array{
399+
* left?:null|float|int|numeric-string,
400+
* right?:null|float|int|numeric-string,
401+
* hanging?:null|float|int|numeric-string,
402+
* firstLine?:null|float|int|numeric-string
403+
* } $value
375404
*/
376-
public function getHanging()
405+
public function setIndentation(array $value = []): self
377406
{
378-
return $this->getChildStyleValue($this->indentation, 'hanging');
407+
$value = array_map(function ($indent) {
408+
if (is_string($indent) || is_numeric($indent)) {
409+
$indent = $this->setFloatVal($indent);
410+
}
411+
412+
return $indent;
413+
}, $value);
414+
$this->setObjectVal($value, 'Indentation', $this->indentation);
415+
416+
return $this;
379417
}
380418

381419
/**
382-
* Set hanging.
383-
*
384-
* @param int $value
385-
*
386-
* @return self
420+
* Set hanging indentation.
387421
*/
388-
public function setHanging($value = null)
422+
public function setIndentHanging(?float $value = null): self
389423
{
390424
return $this->setIndentation(['hanging' => $value]);
391425
}
392426

427+
/**
428+
* Set firstline indentation.
429+
*/
430+
public function setIndentFirstLine(?float $value = null): self
431+
{
432+
return $this->setIndentation(['firstLine' => $value]);
433+
}
434+
435+
/**
436+
* Set left indentation.
437+
*/
438+
public function setIndentLeft(?float $value = null): self
439+
{
440+
return $this->setIndentation(['left' => $value]);
441+
}
442+
443+
/**
444+
* Set right indentation.
445+
*/
446+
public function setIndentRight(?float $value = null): self
447+
{
448+
return $this->setIndentation(['right' => $value]);
449+
}
450+
393451
/**
394452
* Get spacing.
395453
*

0 commit comments

Comments
 (0)