Skip to content

Commit

Permalink
Added backward compatibility for deprecated alignment options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Syroeshko committed Nov 14, 2015
1 parent d3908de commit d74c0bd
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 65 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ Place announcement text here.
### Changed
- Improved error message for the case when `autoload.php` is not found. - @RomanSyroeshko #371
- Renamed the `align` option of `NumberingLevel`, `Frame`, `Table`, and `Paragraph` styles into `alignment`. - @RomanSyroeshko
- Bootstrap script for the manual installation scenario (now include `bootstrap.php` instead of `src/PhpWord/Autoloader.php`). - @RomanSyroeshko

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

### Removed
- `PhpOffice\PhpWord\Style\Alignment`. Style properties, which previously stored instances of this class, now deal with strings.
Expand Down
28 changes: 28 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. test bootstrap
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

$vendorDirPath = realpath(__DIR__ . '/vendor');
if (file_exists($vendorDirPath . '/autoload.php')) {
require $vendorDirPath . '/autoload.php';
} else {
throw new Exception(
sprintf(
'Could not find file \'%s\'. It is generated by Composer. Use \'install --prefer-source\' or \'update --prefer-source\' Composer commands to move forward.',
$vendorDirPath . '/autoload.php'
)
);
}
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
],
"require": {
"php": ">=5.3.3",
"ext-xml": "*"
"ext-xml": "*",
"zendframework/zend-validator": "2.5.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
Expand All @@ -44,14 +45,15 @@
"phploc/phploc": "2.*",
"dompdf/dompdf":"0.6.*",
"tecnick.com/tcpdf": "6.*",
"mpdf/mpdf": "5.*"
"mpdf/mpdf": "5.*",
"zendframework/zend-validator": "2.5.*"
},
"suggest": {
"ext-zip": "Used to write DOCX and ODT",
"ext-gd2": "Used to add images",
"ext-xmlwriter": "Used to write DOCX and ODT",
"ext-xsl": "Used to apply XSL style sheet to main document part of OOXML template",
"dompdf/dompdf": "Used to write PDF"
"ext-zip": "Allows writing DOCX and ODT",
"ext-gd2": "Allows adding images",
"ext-xmlwriter": "Allows writing DOCX and ODT",
"ext-xsl": "Allows applying XSL style sheet to main document part of OOXML template",
"dompdf/dompdf": "Allows writing PDF"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 6 additions & 9 deletions docs/installing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ Installation
------------

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

Using Composer
~~~~~~~~~~~~~~

To install via Composer, add the following lines to your
``composer.json``:
To install via Composer, add the following lines to your ``composer.json``:

.. code-block:: json
Expand All @@ -51,8 +49,8 @@ Notice: all contributions must be done against the developer branch.
}
Manual install
~~~~~~~~~~~~~~
Manual installation
~~~~~~~~~~~~~~~~~~~

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

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

.. code-block:: php
require_once '/path/to/src/PhpWord/Autoloader.php';
require_once "${path_to_the_cloned_repo}/bootstrap.php";
\PhpOffice\PhpWord\Autoloader::register();
Expand Down
3 changes: 0 additions & 3 deletions samples/Sample_Footer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
/**
* Footer file
*/
if (CLI) {
return;
}
Expand Down
8 changes: 2 additions & 6 deletions samples/Sample_Header.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<?php
require_once __DIR__ . '/../src/PhpWord/Autoloader.php';
require_once __DIR__ . '/../bootstrap.php';

date_default_timezone_set('UTC');

/**
* Header file
*/
use PhpOffice\PhpWord\Autoloader;
use PhpOffice\PhpWord\Settings;

date_default_timezone_set('UTC');
error_reporting(E_ALL);
define('CLI', (PHP_SAPI == 'cli') ? true : false);
define('EOL', CLI ? PHP_EOL : '<br />');
Expand Down
3 changes: 0 additions & 3 deletions src/PhpWord/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

namespace PhpOffice\PhpWord;

/**
* Autoloader
*/
class Autoloader
{
/** @const string */
Expand Down
50 changes: 37 additions & 13 deletions src/PhpWord/SimpleType/Jc.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\SimpleType;

use Zend\Validator\InArray;

/**
* Horizontal Alignment Type.
*
Expand All @@ -35,23 +37,45 @@ final class Jc
const LOW_KASHIDA = 'lowKashida';
const THAI_DISTRIBUTE = 'thaiDistribute';

/**
* @deprecated 0.13.0 Use `START` instead.
*/
const LEFT = 'left';
/**
* @deprecated 0.13.0 Use `END` instead.
*/
const RIGHT = 'right';
/**
* @deprecated 0.13.0 Use `BOTH` instead.
*/
const JUSTIFY = 'justify';

/**
* @since 0.13.0
*
* @return string[]
* @return \Zend\Validator\InArray
*/
final public static function getAllowedValues()
{
return array(
self::START,
self::CENTER,
self::END,
self::MEDIUM_KASHIDA,
self::DISTRIBUTE,
self::NUM_TAB,
self::HIGH_KASHIDA,
self::LOW_KASHIDA,
self::THAI_DISTRIBUTE,
final public static function getValidator() {
// todo: consider caching validator instances.
return new InArray(
array (
'haystack' => array(
self::START,
self::CENTER,
self::END,
self::BOTH,
self::MEDIUM_KASHIDA,
self::DISTRIBUTE,
self::NUM_TAB,
self::HIGH_KASHIDA,
self::LOW_KASHIDA,
self::THAI_DISTRIBUTE,
self::LEFT,
self::RIGHT,
self::JUSTIFY,
),
'strict' => InArray::COMPARE_STRICT,
)
);
}
}
28 changes: 24 additions & 4 deletions src/PhpWord/SimpleType/JcTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\SimpleType;

use Zend\Validator\InArray;

/**
* Table Alignment Type.
*
Expand All @@ -28,13 +30,31 @@ final class JcTable
const CENTER = 'center';
const END = 'end';

/**
* @deprecated 0.13.0 Use `START` instead.
*/
const LEFT = 'left';
/**
* @deprecated 0.13.0 Use `END` instead.
*/
const RIGHT = 'right';
/**
* @deprecated 0.13.0 Use `CENTER` instead.
*/
const JUSTIFY = 'justify';

/**
* @since 0.13.0
*
* @return string[]
* @return \Zend\Validator\InArray
*/
final public static function getAllowedValues()
{
return array(self::START, self::CENTER, self::END);
final public static function getValidator() {
// todo: consider caching validator instances.
return new InArray(
array (
'haystack' => array(self::START, self::CENTER, self::END, self::LEFT, self::RIGHT, self::JUSTIFY),
'strict' => InArray::COMPARE_STRICT,
)
);
}
}
21 changes: 19 additions & 2 deletions src/PhpWord/Style/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,25 @@ public function getAlignment()
*/
public function setAlignment($value)
{
if (in_array($value, Jc::getAllowedValues(), true)) {
$this->alignment = $value;
if (Jc::getValidator()->isValid($value)) {
$alignment = '';

switch ($value) {
case Jc::LEFT:
$alignment = Jc::START;
break;
case Jc::RIGHT:
$alignment = Jc::END;
break;
case Jc::JUSTIFY:
$alignment = Jc::BOTH;
break;
default:
$alignment = $value;
break;
}

$this->alignment = $alignment;
}

return $this;
Expand Down
21 changes: 19 additions & 2 deletions src/PhpWord/Style/NumberingLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,25 @@ public function getAlignment()
*/
public function setAlignment($value)
{
if (in_array($value, Jc::getAllowedValues(), true)) {
$this->alignment = $value;
if (Jc::getValidator()->isValid($value)) {
$alignment = '';

switch ($value) {
case Jc::LEFT:
$alignment = Jc::START;
break;
case Jc::RIGHT:
$alignment = Jc::END;
break;
case Jc::JUSTIFY:
$alignment = Jc::BOTH;
break;
default:
$alignment = $value;
break;
}

$this->alignment = $alignment;
}

return $this;
Expand Down
21 changes: 19 additions & 2 deletions src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,25 @@ public function getAlignment()
*/
public function setAlignment($value)
{
if (in_array($value, Jc::getAllowedValues(), true)) {
$this->alignment = $value;
if (Jc::getValidator()->isValid($value)) {
$alignment = '';

switch ($value) {
case Jc::LEFT:
$alignment = Jc::START;
break;
case Jc::RIGHT:
$alignment = Jc::END;
break;
case Jc::JUSTIFY:
$alignment = Jc::BOTH;
break;
default:
$alignment = $value;
break;
}

$this->alignment = $alignment;
}

return $this;
Expand Down
21 changes: 19 additions & 2 deletions src/PhpWord/Style/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,25 @@ public function getAlignment()
*/
public function setAlignment($value)
{
if (in_array($value, JcTable::getAllowedValues(), true)) {
$this->alignment = $value;
if (JcTable::getValidator()->isValid($value)) {
$alignment = '';

switch ($value) {
case JcTable::LEFT:
$alignment = JcTable::START;
break;
case JcTable::RIGHT:
$alignment = JcTable::END;
break;
case JcTable::JUSTIFY:
$alignment = JcTable::CENTER;
break;
default:
$alignment = $value;
break;
}

$this->alignment = $alignment;
}

return $this;
Expand Down
Loading

0 comments on commit d74c0bd

Please sign in to comment.