Skip to content

Commit 34c7045

Browse files
committed
Use objects to specify lengths, colors, and border styles
This enables PHPWord to validate input more accurately, and simplifies setting values for authors.
1 parent 85e9144 commit 34c7045

File tree

498 files changed

+7862
-4566
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

498 files changed

+7862
-4566
lines changed

.php_cs.dist

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ return PhpCsFixer\Config::create()
2323
'combine_consecutive_unsets' => true,
2424
'concat_space' => array('spacing' => 'one'),
2525
'declare_equal_normalize' => true,
26-
'declare_strict_types' => false, // Too early to adopt strict types
26+
'declare_strict_types' => true,
2727
'dir_constant' => true,
2828
'elseif' => true,
2929
'encoding' => true,
@@ -71,6 +71,7 @@ return PhpCsFixer\Config::create()
7171
'no_spaces_after_function_name' => true,
7272
'no_spaces_around_offset' => true,
7373
'no_spaces_inside_parenthesis' => true,
74+
'no_superfluous_phpdoc_tags' => true,
7475
'no_trailing_comma_in_list_call' => true,
7576
'no_trailing_comma_in_singleline_array' => true,
7677
'no_trailing_whitespace' => true,
@@ -109,10 +110,11 @@ return PhpCsFixer\Config::create()
109110
'phpdoc_single_line_var_spacing' => true,
110111
'phpdoc_summary' => false,
111112
'phpdoc_to_comment' => true,
113+
'phpdoc_to_return_type' => false,
112114
'phpdoc_trim' => true,
113115
'phpdoc_types' => true,
114116
'phpdoc_var_without_name' => true,
115-
'pow_to_exponentiation' => false,
117+
'pow_to_exponentiation' => true,
116118
'pre_increment' => false,
117119
'protected_to_private' => true,
118120
'psr0' => true,
@@ -137,7 +139,7 @@ return PhpCsFixer\Config::create()
137139
'switch_case_semicolon_to_colon' => true,
138140
'switch_case_space' => true,
139141
'ternary_operator_spaces' => true,
140-
'ternary_to_null_coalescing' => false, // Cannot use that with PHP 5.6
142+
'ternary_to_null_coalescing' => true,
141143
'trailing_comma_in_multiline_array' => true,
142144
'trim_array_spaces' => false,
143145
'unary_operator_spaces' => true,

.travis.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ language: php
33
dist: precise
44

55
php:
6-
- 5.3
7-
- 5.4
8-
- 5.5
9-
- 5.6
106
- 7.0
117
- 7.1
128
- 7.2
@@ -16,12 +12,9 @@ matrix:
1612
include:
1713
- php: 7.0
1814
env: COVERAGE=1
19-
- php: 5.3
20-
env: COMPOSER_MEMORY_LIMIT=2G
2115
- php: 7.3
2216
env: DEPENDENCIES="--ignore-platform-reqs"
2317
exclude:
24-
- php: 5.3
2518
- php: 7.0
2619
- php: 7.3
2720

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
v0.17.0 (?? ??? 2019)
77
----------------------
8+
This release marked the addition of return type declarations (PHP 7+).
9+
810
### Added
911
- Add RightToLeft table presentation. @troosan #1550
1012
- Set complex type in template @troosan #1565
1113
- Add support for page vertical alignment. @troosan #672 #1569
14+
- Length validation with `PhpOffice\PhpWord\Style\Lengths\{Absolute, Auto, Percent}` @0b10011 #1669
15+
- Color validation with `PhpOffice\PhpWord\Style\Colors\{BasicColor, SpecialColor}` @0b10011 #1669
16+
- BorderStyle validation with `PhpOffice\PhpWord\Style\BorderStyle` @0b10011 #1669
1217

1318
### Fixed
1419
- Fix HTML border-color parsing. @troosan #1551 #1570
20+
- Fixed specifying cell widths, background color, etc on `PhpOffice\PhpWord\Style\Cell` @0b10011 #1669
1521

1622
### Miscellaneous
23+
-
1724
- Use embedded http server to test loading of remote images @troosan #
1825

1926
v0.16.0 (30 dec 2018)

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@
5858
"fix": "Fixes issues found by PHP-CS"
5959
},
6060
"require": {
61-
"php": "^5.3.3 || ^7.0",
61+
"php": "^7.0",
6262
"ext-xml": "*",
6363
"zendframework/zend-escaper": "^2.2",
6464
"phpoffice/common": "^0.2.9"
6565
},
6666
"require-dev": {
6767
"ext-zip": "*",
6868
"ext-gd": "*",
69-
"phpunit/phpunit": "^4.8.36 || ^7.0",
70-
"squizlabs/php_codesniffer": "^2.9",
71-
"friendsofphp/php-cs-fixer": "^2.2",
72-
"phpmd/phpmd": "2.*",
73-
"phploc/phploc": "2.* || 3.* || 4.*",
69+
"phpunit/phpunit": "^6.0 || ^7.0",
70+
"squizlabs/php_codesniffer": "*",
71+
"friendsofphp/php-cs-fixer": "*",
72+
"phpmd/phpmd": "*",
73+
"phploc/phploc": "*",
7474
"dompdf/dompdf":"0.8.*",
7575
"tecnickcom/tcpdf": "6.*",
7676
"mpdf/mpdf": "5.7.4 || 6.* || 7.*",

docs/containers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ section. Example:
2525
2626
$sectionStyle = array(
2727
'orientation' => 'landscape',
28-
'marginTop' => 600,
28+
'marginTop' => Absolute::from('twip', 600),
2929
'colsNum' => 2,
3030
);
3131

docs/elements.rst

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ Table style can be defined with ``addTableStyle``:
218218
.. code-block:: php
219219
220220
$tableStyle = array(
221-
'borderColor' => '006699',
222-
'borderSize' => 6,
223-
'cellMargin' => 50
221+
'borderColor' => new Hex('006699'),
222+
'borderSize' => Absolute::from('twip', 6),
223+
'cellMargin' => Absolute::from('twip', 50)
224224
);
225-
$firstRowStyle = array('bgColor' => '66BBFF');
225+
$firstRowStyle = array('bgColor' => new Hex('66BBFF'));
226226
$phpWord->addTableStyle('myTable', $tableStyle, $firstRowStyle);
227227
$table = $section->addTable('myTable');
228228
@@ -235,7 +235,7 @@ You can span a cell on multiple columns by using ``gridSpan`` or multiple rows b
235235

236236
.. code-block:: php
237237
238-
$cell = $table->addCell(200);
238+
$cell = $table->addCell(Absolute::from('twip', 200));
239239
$cell->getStyle()->setGridSpan(5);
240240
241241
See ``Sample_09_Tables.php`` for more code sample.
@@ -260,10 +260,10 @@ Examples:
260260
$section->addImage(
261261
'mars.jpg',
262262
array(
263-
'width' => 100,
264-
'height' => 100,
265-
'marginTop' => -1,
266-
'marginLeft' => -1,
263+
'width' => Absolute::from('twip', 100),
264+
'height' => Absolute::from('twip', 100),
265+
'marginTop' => Absolute::from('twip', -1),
266+
'marginLeft' => Absolute::from('twip', -1),
267267
'wrappingStyle' => 'behind'
268268
)
269269
);
@@ -285,7 +285,7 @@ header reference. After creating a header, you can use the
285285
286286
$section = $phpWord->addSection();
287287
$header = $section->addHeader();
288-
$header->addWatermark('resources/_earth.jpg', array('marginTop' => 200, 'marginLeft' => 55));
288+
$header->addWatermark('resources/_earth.jpg', array('marginTop' => Absolute::from('twip', 200), 'marginLeft' => Absolute::from('twip', 55)));
289289
290290
Objects
291291
-------
@@ -316,7 +316,7 @@ Options for ``$tocStyle``:
316316

317317
- ``tabLeader``. Fill type between the title text and the page number. Use the defined constants in ``\PhpOffice\PhpWord\Style\TOC``.
318318
- ``tabPos``. The position of the tab where the page number appears in *twip*.
319-
- ``indent``. The indent factor of the titles in *twip*.
319+
- ``indent``. The indent factor of the titles (``\PhpOffice\PhpWord\Style\Lengths\Absolute``).
320320

321321
Footnotes & endnotes
322322
--------------------
@@ -437,13 +437,13 @@ Line elements can be added to sections by using ``addLine``.
437437
438438
Available line style attributes:
439439

440-
- ``weight``. Line width in *twip*.
441-
- ``color``. Defines the color of stroke.
440+
- ``weight``. Line width (``\PhpOffice\PhpWord\Style\Lengths\Absolute``).
441+
- ``color``. Defines the color of stroke (``\PhpOffice\PhpWord\Style\Colors\BasicColor``).
442442
- ``dash``. Line types: dash, rounddot, squaredot, dashdot, longdash, longdashdot, longdashdotdot.
443443
- ``beginArrow``. Start type of arrow: block, open, classic, diamond, oval.
444444
- ``endArrow``. End type of arrow: block, open, classic, diamond, oval.
445-
- ``width``. Line-object width in *pt*.
446-
- ``height``. Line-object height in *pt*.
445+
- ``width``. Line-object width (``\PhpOffice\PhpWord\Style\Lengths\Absolute``).
446+
- ``height``. Line-object height (``\PhpOffice\PhpWord\Style\Lengths\Absolute``).
447447
- ``flip``. Flip the line element: true, false.
448448

449449
Chart

docs/faq.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ the merrier, right?
1818
I’ve been running PHPWord from CodePlex flawlessly, but I can’t use the latest PHPWord from GitHub. Why?
1919
--------------------------------------------------------------------------------------------------------
2020

21-
PHPWord requires PHP 5.3+ since 0.8, while PHPWord 0.6.3 from CodePlex
21+
PHPWord requires PHP7.0+ since 0.17 (PHP 5.3+ since 0.8), while PHPWord 0.6.3 from CodePlex
2222
can run with PHP 5.2. There’s a lot of new features that we can get from
2323
PHP 5.3 and it’s been around since 2009! You should upgrade your PHP
2424
version to use PHPWord 0.8+.

docs/general.rst

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
4141
'"Great achievement is usually born of great sacrifice, '
4242
. 'and is never the result of selfishness." '
4343
. '(Napoleon Hill)',
44-
array('name' => 'Tahoma', 'size' => 10)
44+
array('name' => 'Tahoma', 'size' => Absolute::from('pt', 10))
4545
);
4646
4747
// Adding Text element with font customized using named font style...
4848
$fontStyleName = 'oneUserDefinedStyle';
4949
$phpWord->addFontStyle(
5050
$fontStyleName,
51-
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
51+
array('name' => 'Tahoma', 'size' => Absolute::from('pt', 10), 'color' => new Hex('1B2232'), 'bold' => true)
5252
);
5353
$section->addText(
5454
'"The greatest accomplishment is not in never falling, '
@@ -61,7 +61,7 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
6161
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
6262
$fontStyle->setBold(true);
6363
$fontStyle->setName('Tahoma');
64-
$fontStyle->setSize(13);
64+
$fontStyle->setSize(Absolute::from('pt', 13));
6565
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
6666
$myTextElement->setFontStyle($fontStyle);
6767
@@ -139,7 +139,7 @@ default font by using the following two functions:
139139
.. code-block:: php
140140
141141
$phpWord->setDefaultFontName('Times New Roman');
142-
$phpWord->setDefaultFontSize(12);
142+
$phpWord->setDefaultFontSize(Absolute::from('pt', 12));
143143
144144
Document settings
145145
-----------------
@@ -251,25 +251,34 @@ name. Use the following functions:
251251
Measurement units
252252
-----------------
253253

254-
The base length unit in Open Office XML is twip. Twip means "TWentieth
255-
of an Inch Point", i.e. 1 twip = 1/1440 inch.
254+
While Open Office XML uses twentieths of a point ("twips" or 1/1440 inch)
255+
for most of its measurements,
256+
all lengths in PHPWord use an instance of ``\PhpOffice\PhpWord\Style\Lengths\Length``.
257+
Most measurements are limited to absolute units
258+
(rather than percent or "auto" values),
259+
so those use ``\PhpOffice\PhpWord\Style\Lengths\Absolute`` exclusively.
260+
Methods are typehinted to ensure the right measurement is provided.
256261

257-
You can use PHPWord helper functions to convert inches, centimeters, or
258-
points to twip.
262+
You can use ``\PhpOffice\PhpWord\Style\Lengths\Absolute``
263+
to specify lengths in units you're comfortable with (eg, "pt"),
264+
and PHPWord will automatically convert those lengths
265+
to the appropriate ones for the format being exported to.
259266

260267
.. code-block:: php
261268
269+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
270+
262271
// Paragraph with 6 points space after
263272
$phpWord->addParagraphStyle('My Style', array(
264-
'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6))
273+
'spaceAfter' => Absolute::from('pt', 6))
265274
);
266275
267276
$section = $phpWord->addSection();
268277
$sectionStyle = $section->getStyle();
269278
// half inch left margin
270-
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
279+
$sectionStyle->setMarginLeft(Absolute::from('in', .5);
271280
// 2 cm right margin
272-
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
281+
$sectionStyle->setMarginRight(Absolute::from('cm', 2);
273282
274283
Document protection
275284
-------------------
@@ -317,12 +326,12 @@ There is no limit if the option is not set or the provided value is ``0``.
317326
Hyphenation Zone
318327
~~~~~~~~~~~~~~~~
319328

320-
The hyphenation zone (in *twip*) is the allowed amount of whitespace before hyphenation is applied.
329+
The hyphenation zone is the allowed amount of whitespace before hyphenation is applied.
321330
The smaller the hyphenation zone the more words are hyphenated. Or in other words, the wider the hyphenation zone the less words are hyphenated.
322331

323332
.. code-block:: php
324333
325-
$phpWord->getSettings()->setHyphenationZone(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(1));
334+
$phpWord->getSettings()->setHyphenationZone(\PhpOffice\PhpWord\Style\Lengths\Absolute::from('cm', 1));
326335
327336
Hyphenate Caps
328337
~~~~~~~~~~~~~~

docs/installing.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Requirements
88

99
Mandatory:
1010

11-
- PHP 5.3.3+
11+
- PHP 7+
1212
- `XML Parser <http://www.php.net/manual/en/xml.installation.php>`__ extension
1313
- `Zend\\Escaper <http://framework.zend.com/manual/current/en/modules/zend.escaper.introduction.html>`__ component
1414
- Zend\\Stdlib component
@@ -34,7 +34,7 @@ Example:
3434
3535
{
3636
"require": {
37-
"phpoffice/phpword": "v0.14.*"
37+
"phpoffice/phpword": "v0.16.*"
3838
}
3939
}
4040

docs/recipes.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ Use absolute positioning relative to margin horizontally and to line vertically.
1010

1111
.. code-block:: php
1212
13+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
14+
1315
$imageStyle = array(
14-
'width' => 40,
15-
'height' => 40,
16+
'width' => Absolute::from('twip', 40),
17+
'height' => Absolute::from('twip', 40),
1618
'wrappingStyle' => 'square',
1719
'positioning' => 'absolute',
1820
'posHorizontalRel' => 'margin',
@@ -72,10 +74,13 @@ Apply 'HeadingN' paragraph style to TextRun or Link. Sample code:
7274

7375
.. code-block:: php
7476
77+
use PhpOffice\PhpWord\Style\Colors\Hex
78+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
79+
7580
$phpWord = new \PhpOffice\PhpWord\PhpWord();
76-
$phpWord->addTitleStyle(1, array('size' => 16, 'bold' => true));
77-
$phpWord->addTitleStyle(2, array('size' => 14, 'bold' => true));
78-
$phpWord->addFontStyle('Link', array('color' => '0000FF', 'underline' => 'single'));
81+
$phpWord->addTitleStyle(1, array('size' => Absolute::from('pt', 16), 'bold' => true));
82+
$phpWord->addTitleStyle(2, array('size' => Absolute::from('pt', 14), 'bold' => true));
83+
$phpWord->addFontStyle('Link', array('color' => new Hex('0000FF'), 'underline' => 'single'));
7984
8085
$section = $phpWord->addSection();
8186

0 commit comments

Comments
 (0)