Skip to content

Commit 47082f4

Browse files
committed
Use objects to specify lengths, colors, and border styles
1 parent 8fbd060 commit 47082f4

File tree

499 files changed

+8034
-4592
lines changed

Some content is hidden

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

499 files changed

+8034
-4592
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: xenial
44

55
php:
6-
- 5.3
7-
- 5.4
8-
- 5.5
9-
- 5.6
106
- 7.0
117
- 7.1
128
- 7.2
@@ -27,9 +23,6 @@ matrix:
2723
- php: 7.3
2824
env: DEPENDENCIES="--ignore-platform-reqs"
2925
exclude:
30-
- php: 5.3
31-
- php: 5.4
32-
- php: 5.5
3326
- php: 7.0
3427
- php: 7.3
3528
allow_failures:

CHANGELOG.md

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

66
v0.17.0 (?? ??? 2019)
77
----------------------
8+
This release marked the addition of strict typing and 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 and automatic unit conversion 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
17+
- Support for additional `border-style` values (`hidden`, `groove`, `ridge`, `inset`, and `outset`) in HTML documents @0b10011 #1669
18+
- Support for all named colors from CSS in HTML documents @0b10011 #1669
19+
- Support for borders on `Cell` @0b10011 #1669
20+
- Support for writing percent and auto column widths on `Table` to ODT files @0b10011 #1669
21+
- Support for `space` and `shadow` on `Border` (and support for writing to Word2007) @0b10011 #1669
22+
23+
### Changed
24+
- `float` and `int` are no longer supported for lengths. Only `PhpOffice\PhpWord\Style\Lengths\{Absolute, Auto, Percent}` are allowed. @0b10011 #1669
25+
- `string` is no longer supported for colors. Only `PhpOffice\PhpWord\Style\Colors\{BasicColor, SpecialColor}` is allowed. @0b10011 #1669
26+
- `string` is no longer supported for border styles. Only `PhpOffice\PhpWord\Style\BorderStyle` is allowed. @0b10011 #1669
1227

1328
### Fixed
1429
- Fix HTML border-color parsing. @troosan #1551 #1570
30+
- Fixed specifying cell widths, background color, etc on `PhpOffice\PhpWord\Style\Cell` @0b10011 #1669
31+
- Escape arrays of replacements in `TemplateProcessor` @0b10011 #1669
1532

1633
### Miscellaneous
34+
-
1735
- Use embedded http server to test loading of remote images @troosan #
1836

1937
v0.16.0 (30 dec 2018)

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ The following is a basic usage example of the PHPWord library.
101101
<?php
102102
require_once 'bootstrap.php';
103103

104+
use PhpOffice\PhpWord\PhpWord;
105+
use PhpOffice\PhpWord\Style\Font;
106+
use PhpOffice\PhpWord\Style\Colors\Hex;
107+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
108+
104109
// Creating the new document...
105-
$phpWord = new \PhpOffice\PhpWord\PhpWord();
110+
$phpWord = new PhpWord();
106111

107112
/* Note: any element you append to a document must reside inside of a Section. */
108113

@@ -127,14 +132,14 @@ $section->addText(
127132
'"Great achievement is usually born of great sacrifice, '
128133
. 'and is never the result of selfishness." '
129134
. '(Napoleon Hill)',
130-
array('name' => 'Tahoma', 'size' => 10)
135+
array('name' => 'Tahoma', 'size' => Absolute::from('pt', 10))
131136
);
132137

133138
// Adding Text element with font customized using named font style...
134139
$fontStyleName = 'oneUserDefinedStyle';
135140
$phpWord->addFontStyle(
136141
$fontStyleName,
137-
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
142+
array('name' => 'Tahoma', 'size' => Absolute::from('pt', 10), 'color' => new Hex('1B2232'), 'bold' => true)
138143
);
139144
$section->addText(
140145
'"The greatest accomplishment is not in never falling, '
@@ -144,10 +149,10 @@ $section->addText(
144149
);
145150

146151
// Adding Text element with font customized using explicitly created font style object...
147-
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
152+
$fontStyle = new Font();
148153
$fontStyle->setBold(true);
149154
$fontStyle->setName('Tahoma');
150-
$fontStyle->setSize(13);
155+
$fontStyle->setSize(Absolute::from('pt', 13));
151156
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
152157
$myTextElement->setFontStyle($fontStyle);
153158

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ section. Example:
2323

2424
.. code-block:: php
2525
26+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
27+
2628
$sectionStyle = array(
2729
'orientation' => 'landscape',
28-
'marginTop' => 600,
30+
'marginTop' => Absolute::from('twip', 600),
2931
'colsNum' => 2,
3032
);
3133

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: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
1515
<?php
1616
require_once 'bootstrap.php';
1717
18+
use PhpOffice\PhpWord\IOFactory;
19+
use PhpOffice\PhpWord\PhpWord;
20+
use PhpOffice\PhpWord\Style\Font;
21+
use PhpOffice\PhpWord\Style\Colors\Hex;
22+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
23+
1824
// Creating the new document...
19-
$phpWord = new \PhpOffice\PhpWord\PhpWord();
25+
$phpWord = new PhpWord();
2026
2127
/* Note: any element you append to a document must reside inside of a Section. */
2228
@@ -41,14 +47,14 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
4147
'"Great achievement is usually born of great sacrifice, '
4248
. 'and is never the result of selfishness." '
4349
. '(Napoleon Hill)',
44-
array('name' => 'Tahoma', 'size' => 10)
50+
array('name' => 'Tahoma', 'size' => Absolute::from('pt', 10))
4551
);
4652
4753
// Adding Text element with font customized using named font style...
4854
$fontStyleName = 'oneUserDefinedStyle';
4955
$phpWord->addFontStyle(
5056
$fontStyleName,
51-
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
57+
array('name' => 'Tahoma', 'size' => Absolute::from('pt', 10), 'color' => new Hex('1B2232'), 'bold' => true)
5258
);
5359
$section->addText(
5460
'"The greatest accomplishment is not in never falling, '
@@ -58,23 +64,23 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
5864
);
5965
6066
// Adding Text element with font customized using explicitly created font style object...
61-
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
67+
$fontStyle = new Font();
6268
$fontStyle->setBold(true);
6369
$fontStyle->setName('Tahoma');
64-
$fontStyle->setSize(13);
70+
$fontStyle->setSize(Absolute::from('pt', 13));
6571
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
6672
$myTextElement->setFontStyle($fontStyle);
6773
6874
// Saving the document as OOXML file...
69-
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
75+
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
7076
$objWriter->save('helloWorld.docx');
7177
7278
// Saving the document as ODF file...
73-
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
79+
$objWriter = IOFactory::createWriter($phpWord, 'ODText');
7480
$objWriter->save('helloWorld.odt');
7581
7682
// Saving the document as HTML file...
77-
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
83+
$objWriter = IOFactory::createWriter($phpWord, 'HTML');
7884
$objWriter->save('helloWorld.html');
7985
8086
/* Note: we skip RTF, because it's not XML-based and requires a different example. */
@@ -138,8 +144,10 @@ default font by using the following two functions:
138144

139145
.. code-block:: php
140146
147+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
148+
141149
$phpWord->setDefaultFontName('Times New Roman');
142-
$phpWord->setDefaultFontSize(12);
150+
$phpWord->setDefaultFontSize(Absolute::from('pt', 12));
143151
144152
Document settings
145153
-----------------
@@ -251,25 +259,34 @@ name. Use the following functions:
251259
Measurement units
252260
-----------------
253261

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.
262+
While Open Office XML uses twentieths of a point ("twips" or 1/1440 inch)
263+
for most of its measurements,
264+
all lengths in PHPWord use an instance of ``\PhpOffice\PhpWord\Style\Lengths\Length``.
265+
Most measurements are limited to absolute units
266+
(rather than percent or "auto" values),
267+
so those use ``\PhpOffice\PhpWord\Style\Lengths\Absolute`` exclusively.
268+
Methods are typehinted to ensure the right measurement is provided.
256269

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

260275
.. code-block:: php
261276
277+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
278+
262279
// Paragraph with 6 points space after
263280
$phpWord->addParagraphStyle('My Style', array(
264-
'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6))
265-
);
281+
'spaceAfter' => Absolute::from('pt', 6)
282+
));
266283
267284
$section = $phpWord->addSection();
268285
$sectionStyle = $section->getStyle();
269286
// half inch left margin
270-
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
287+
$sectionStyle->setMarginLeft(Absolute::from('in', .5));
271288
// 2 cm right margin
272-
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
289+
$sectionStyle->setMarginRight(Absolute::from('cm', 2));
273290
274291
Document protection
275292
-------------------
@@ -317,12 +334,14 @@ There is no limit if the option is not set or the provided value is ``0``.
317334
Hyphenation Zone
318335
~~~~~~~~~~~~~~~~
319336

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

323340
.. code-block:: php
324341
325-
$phpWord->getSettings()->setHyphenationZone(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(1));
342+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
343+
344+
$phpWord->getSettings()->setHyphenationZone(Absolute::from('cm', 1));
326345
327346
Hyphenate Caps
328347
~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)