Skip to content

Commit d74c0bd

Browse files
author
Roman Syroeshko
committed
Added backward compatibility for deprecated alignment options.
1 parent d3908de commit d74c0bd

File tree

14 files changed

+187
-65
lines changed

14 files changed

+187
-65
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ Place announcement text here.
1616
### Changed
1717
- Improved error message for the case when `autoload.php` is not found. - @RomanSyroeshko #371
1818
- Renamed the `align` option of `NumberingLevel`, `Frame`, `Table`, and `Paragraph` styles into `alignment`. - @RomanSyroeshko
19+
- Bootstrap script for the manual installation scenario (now include `bootstrap.php` instead of `src/PhpWord/Autoloader.php`). - @RomanSyroeshko
1920

2021
### Deprecated
2122
- `getAlign` and `setAlign` methods of `NumberingLevel`, `Frame`, `Table`, and `Paragraph` styles.
2223
Use the correspondent `getAlignment` and `setAlignment` methods instead.
24+
- `left`, `right`, and `justify` alignment options for paragraphs (now are mapped to `Jc::START`, `Jc::END`, and `Jc::BOTH`).
25+
- `left`, `right`, and `justify` alignment options for tables (now are mapped to `Jc::START`, `Jc::END`, and `Jc::CENTER`).
2326

2427
### Removed
2528
- `PhpOffice\PhpWord\Style\Alignment`. Style properties, which previously stored instances of this class, now deal with strings.

bootstrap.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. test bootstrap
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
$vendorDirPath = realpath(__DIR__ . '/vendor');
19+
if (file_exists($vendorDirPath . '/autoload.php')) {
20+
require $vendorDirPath . '/autoload.php';
21+
} else {
22+
throw new Exception(
23+
sprintf(
24+
'Could not find file \'%s\'. It is generated by Composer. Use \'install --prefer-source\' or \'update --prefer-source\' Composer commands to move forward.',
25+
$vendorDirPath . '/autoload.php'
26+
)
27+
);
28+
}

composer.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
],
3434
"require": {
3535
"php": ">=5.3.3",
36-
"ext-xml": "*"
36+
"ext-xml": "*",
37+
"zendframework/zend-validator": "2.5.*"
3738
},
3839
"require-dev": {
3940
"phpunit/phpunit": "3.7.*",
@@ -44,14 +45,15 @@
4445
"phploc/phploc": "2.*",
4546
"dompdf/dompdf":"0.6.*",
4647
"tecnick.com/tcpdf": "6.*",
47-
"mpdf/mpdf": "5.*"
48+
"mpdf/mpdf": "5.*",
49+
"zendframework/zend-validator": "2.5.*"
4850
},
4951
"suggest": {
50-
"ext-zip": "Used to write DOCX and ODT",
51-
"ext-gd2": "Used to add images",
52-
"ext-xmlwriter": "Used to write DOCX and ODT",
53-
"ext-xsl": "Used to apply XSL style sheet to main document part of OOXML template",
54-
"dompdf/dompdf": "Used to write PDF"
52+
"ext-zip": "Allows writing DOCX and ODT",
53+
"ext-gd2": "Allows adding images",
54+
"ext-xmlwriter": "Allows writing DOCX and ODT",
55+
"ext-xsl": "Allows applying XSL style sheet to main document part of OOXML template",
56+
"dompdf/dompdf": "Allows writing PDF"
5557
},
5658
"autoload": {
5759
"psr-4": {

docs/installing.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ Installation
2222
------------
2323

2424
There are two ways to install PHPWord, i.e. via
25-
`Composer <http://getcomposer.org/>`__ or manually by downloading the
26-
library.
25+
`Composer <http://getcomposer.org/>`__ or manually by downloading the library.
2726

2827
Using Composer
2928
~~~~~~~~~~~~~~
3029

31-
To install via Composer, add the following lines to your
32-
``composer.json``:
30+
To install via Composer, add the following lines to your ``composer.json``:
3331

3432
.. code-block:: json
3533
@@ -51,8 +49,8 @@ Notice: all contributions must be done against the developer branch.
5149
}
5250
5351
54-
Manual install
55-
~~~~~~~~~~~~~~
52+
Manual installation
53+
~~~~~~~~~~~~~~~~~~~
5654

5755
To install manually, you change to the web-server directory of your file system. Then you have 2 possibilities.
5856

@@ -63,12 +61,11 @@ To install manually, you change to the web-server directory of your file system.
6361
6462
git clone https://github.com/PHPOffice/PHPWord.git
6563
66-
To use the library, include ``src/PhpWord/Autoloader.php`` in your PHP script and
67-
invoke ``Autoloader::register``.
64+
To use the library, include ``bootstrap.php`` in your PHP script and invoke ``Autoloader::register``.
6865

6966
.. code-block:: php
7067
71-
require_once '/path/to/src/PhpWord/Autoloader.php';
68+
require_once "${path_to_the_cloned_repo}/bootstrap.php";
7269
\PhpOffice\PhpWord\Autoloader::register();
7370
7471

samples/Sample_Footer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<?php
2-
/**
3-
* Footer file
4-
*/
52
if (CLI) {
63
return;
74
}

samples/Sample_Header.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<?php
2-
require_once __DIR__ . '/../src/PhpWord/Autoloader.php';
2+
require_once __DIR__ . '/../bootstrap.php';
33

4-
date_default_timezone_set('UTC');
5-
6-
/**
7-
* Header file
8-
*/
94
use PhpOffice\PhpWord\Autoloader;
105
use PhpOffice\PhpWord\Settings;
116

7+
date_default_timezone_set('UTC');
128
error_reporting(E_ALL);
139
define('CLI', (PHP_SAPI == 'cli') ? true : false);
1410
define('EOL', CLI ? PHP_EOL : '<br />');

src/PhpWord/Autoloader.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
namespace PhpOffice\PhpWord;
1919

20-
/**
21-
* Autoloader
22-
*/
2320
class Autoloader
2421
{
2522
/** @const string */

src/PhpWord/SimpleType/Jc.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\SimpleType;
1919

20+
use Zend\Validator\InArray;
21+
2022
/**
2123
* Horizontal Alignment Type.
2224
*
@@ -35,23 +37,45 @@ final class Jc
3537
const LOW_KASHIDA = 'lowKashida';
3638
const THAI_DISTRIBUTE = 'thaiDistribute';
3739

40+
/**
41+
* @deprecated 0.13.0 Use `START` instead.
42+
*/
43+
const LEFT = 'left';
44+
/**
45+
* @deprecated 0.13.0 Use `END` instead.
46+
*/
47+
const RIGHT = 'right';
48+
/**
49+
* @deprecated 0.13.0 Use `BOTH` instead.
50+
*/
51+
const JUSTIFY = 'justify';
52+
3853
/**
3954
* @since 0.13.0
4055
*
41-
* @return string[]
56+
* @return \Zend\Validator\InArray
4257
*/
43-
final public static function getAllowedValues()
44-
{
45-
return array(
46-
self::START,
47-
self::CENTER,
48-
self::END,
49-
self::MEDIUM_KASHIDA,
50-
self::DISTRIBUTE,
51-
self::NUM_TAB,
52-
self::HIGH_KASHIDA,
53-
self::LOW_KASHIDA,
54-
self::THAI_DISTRIBUTE,
58+
final public static function getValidator() {
59+
// todo: consider caching validator instances.
60+
return new InArray(
61+
array (
62+
'haystack' => array(
63+
self::START,
64+
self::CENTER,
65+
self::END,
66+
self::BOTH,
67+
self::MEDIUM_KASHIDA,
68+
self::DISTRIBUTE,
69+
self::NUM_TAB,
70+
self::HIGH_KASHIDA,
71+
self::LOW_KASHIDA,
72+
self::THAI_DISTRIBUTE,
73+
self::LEFT,
74+
self::RIGHT,
75+
self::JUSTIFY,
76+
),
77+
'strict' => InArray::COMPARE_STRICT,
78+
)
5579
);
5680
}
5781
}

src/PhpWord/SimpleType/JcTable.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\SimpleType;
1919

20+
use Zend\Validator\InArray;
21+
2022
/**
2123
* Table Alignment Type.
2224
*
@@ -28,13 +30,31 @@ final class JcTable
2830
const CENTER = 'center';
2931
const END = 'end';
3032

33+
/**
34+
* @deprecated 0.13.0 Use `START` instead.
35+
*/
36+
const LEFT = 'left';
37+
/**
38+
* @deprecated 0.13.0 Use `END` instead.
39+
*/
40+
const RIGHT = 'right';
41+
/**
42+
* @deprecated 0.13.0 Use `CENTER` instead.
43+
*/
44+
const JUSTIFY = 'justify';
45+
3146
/**
3247
* @since 0.13.0
3348
*
34-
* @return string[]
49+
* @return \Zend\Validator\InArray
3550
*/
36-
final public static function getAllowedValues()
37-
{
38-
return array(self::START, self::CENTER, self::END);
51+
final public static function getValidator() {
52+
// todo: consider caching validator instances.
53+
return new InArray(
54+
array (
55+
'haystack' => array(self::START, self::CENTER, self::END, self::LEFT, self::RIGHT, self::JUSTIFY),
56+
'strict' => InArray::COMPARE_STRICT,
57+
)
58+
);
3959
}
4060
}

src/PhpWord/Style/Frame.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,25 @@ public function getAlignment()
200200
*/
201201
public function setAlignment($value)
202202
{
203-
if (in_array($value, Jc::getAllowedValues(), true)) {
204-
$this->alignment = $value;
203+
if (Jc::getValidator()->isValid($value)) {
204+
$alignment = '';
205+
206+
switch ($value) {
207+
case Jc::LEFT:
208+
$alignment = Jc::START;
209+
break;
210+
case Jc::RIGHT:
211+
$alignment = Jc::END;
212+
break;
213+
case Jc::JUSTIFY:
214+
$alignment = Jc::BOTH;
215+
break;
216+
default:
217+
$alignment = $value;
218+
break;
219+
}
220+
221+
$this->alignment = $alignment;
205222
}
206223

207224
return $this;

src/PhpWord/Style/NumberingLevel.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,25 @@ public function getAlignment()
298298
*/
299299
public function setAlignment($value)
300300
{
301-
if (in_array($value, Jc::getAllowedValues(), true)) {
302-
$this->alignment = $value;
301+
if (Jc::getValidator()->isValid($value)) {
302+
$alignment = '';
303+
304+
switch ($value) {
305+
case Jc::LEFT:
306+
$alignment = Jc::START;
307+
break;
308+
case Jc::RIGHT:
309+
$alignment = Jc::END;
310+
break;
311+
case Jc::JUSTIFY:
312+
$alignment = Jc::BOTH;
313+
break;
314+
default:
315+
$alignment = $value;
316+
break;
317+
}
318+
319+
$this->alignment = $alignment;
303320
}
304321

305322
return $this;

src/PhpWord/Style/Paragraph.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,25 @@ public function getAlignment()
232232
*/
233233
public function setAlignment($value)
234234
{
235-
if (in_array($value, Jc::getAllowedValues(), true)) {
236-
$this->alignment = $value;
235+
if (Jc::getValidator()->isValid($value)) {
236+
$alignment = '';
237+
238+
switch ($value) {
239+
case Jc::LEFT:
240+
$alignment = Jc::START;
241+
break;
242+
case Jc::RIGHT:
243+
$alignment = Jc::END;
244+
break;
245+
case Jc::JUSTIFY:
246+
$alignment = Jc::BOTH;
247+
break;
248+
default:
249+
$alignment = $value;
250+
break;
251+
}
252+
253+
$this->alignment = $alignment;
237254
}
238255

239256
return $this;

src/PhpWord/Style/Table.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,25 @@ public function getAlignment()
509509
*/
510510
public function setAlignment($value)
511511
{
512-
if (in_array($value, JcTable::getAllowedValues(), true)) {
513-
$this->alignment = $value;
512+
if (JcTable::getValidator()->isValid($value)) {
513+
$alignment = '';
514+
515+
switch ($value) {
516+
case JcTable::LEFT:
517+
$alignment = JcTable::START;
518+
break;
519+
case JcTable::RIGHT:
520+
$alignment = JcTable::END;
521+
break;
522+
case JcTable::JUSTIFY:
523+
$alignment = JcTable::CENTER;
524+
break;
525+
default:
526+
$alignment = $value;
527+
break;
528+
}
529+
530+
$this->alignment = $alignment;
514531
}
515532

516533
return $this;

0 commit comments

Comments
 (0)