Skip to content

Commit 3ef0f41

Browse files
committed
New Style\AbstractStyle and 'ODText\Base`
1 parent f1108c4 commit 3ef0f41

21 files changed

+225
-262
lines changed

CHANGELOG.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
44

55
## 0.9.2 - Not yet released
66

7+
This release marked heavy refactorings on internal code structure with the creation of some abstract classes to reduce code duplication. `Element` subnamespace is introduced in this release to replace `Section`.
8+
79
### Features
810

911
- Image: Get image dimensions without EXIF extension - @andrew-kzoo GH-184
@@ -48,14 +50,16 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
4850
### Miscellaneous
4951

5052
- Documentation: Simplify page level docblock - @ivanlanin GH-179
51-
- Writer: Refactor writer classes and make a new AbstractWriter abstract class - @ivanlanin GH-160
52-
- General: Refactor folders: Element and Exception - @ivanlanin GH-187
53-
- General: Remove legacy HashTable and ZipStreamWrapper and all related properties/methods - @ivanlanin GH-187
54-
- Element: Create new AbstractElement abstract class - @ivanlanin GH-187
53+
- Writer: Refactor writer classes and create a new `Write\AbstractWriter` abstract class - @ivanlanin GH-160
54+
- General: Refactor folders: `Element` and `Exception` - @ivanlanin GH-187
55+
- General: Remove legacy `HashTable` and `Shared\ZipStreamWrapper` and all related properties/methods - @ivanlanin GH-187
56+
- Element: New `AbstractElement` abstract class - @ivanlanin GH-187
5557
- Media: Refactor media class to use one method for all docPart (section, header, footer, footnote) - @ivanlanin GH-187
5658
- General: Remove underscore prefix from all private properties name - @ivanlanin GH-187
57-
- General: Move Section Settings to Style\Section - @ivanlanin GH-187
59+
- General: Move Section `Settings` to `Style\Section` - @ivanlanin GH-187
5860
- General: Give `Abstract` prefix and `Interface` suffix for all abstract classes and interfaces as per [PHP-FIG recommendation](https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md) - @ivanlanin GH-187
61+
- Style: New `Style\AbstractStyle` abstract class - @ivanlanin GH-187
62+
- Writer: New 'ODText\Base` class - @ivanlanin GH-187
5963

6064
## 0.9.1 - 27 Mar 2014
6165

src/PhpWord/Style/AbstractStyle.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Style;
11+
12+
/**
13+
* Abstract style class
14+
*
15+
* @since 0.9.2
16+
*/
17+
abstract class AbstractStyle
18+
{
19+
/**
20+
* Set style value template method
21+
*
22+
* Some child classes have their own specific overrides
23+
*
24+
* @param string $key
25+
* @param string $value
26+
*
27+
* @todo Implement type check mechanism, e.g. boolean, integer, enum, defaults
28+
*/
29+
public function setStyleValue($key, $value)
30+
{
31+
// Backward compability check for versions < 0.9.2 which use underscore
32+
// prefix for their private properties
33+
if (substr($key, 0, 1) == '_') {
34+
$key = substr($key, 1);
35+
}
36+
37+
// Check if the set method is exists. Throws an exception?
38+
$method = 'set' . $key;
39+
if (method_exists($this, $method)) {
40+
$this->$method($value);
41+
}
42+
}
43+
}

src/PhpWord/Style/Cell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Table cell style
1414
*/
15-
class Cell
15+
class Cell extends AbstractStyle
1616
{
1717
const TEXT_DIR_BTLR = 'btLr';
1818
const TEXT_DIR_TBRL = 'tbRl';

src/PhpWord/Style/Font.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Font style
1717
*/
18-
class Font
18+
class Font extends AbstractStyle
1919
{
2020
const UNDERLINE_NONE = 'none';
2121
const UNDERLINE_DASH = 'dash';
@@ -202,23 +202,6 @@ public function setArrayStyle(array $style = array())
202202
return $this;
203203
}
204204

205-
/**
206-
* Set style value
207-
*
208-
* @param string $key
209-
* @param mixed $value
210-
*/
211-
public function setStyleValue($key, $value)
212-
{
213-
if (substr($key, 0, 1) == '_') {
214-
$key = substr($key, 1);
215-
}
216-
$method = 'set' . $key;
217-
if (method_exists($this, $method)) {
218-
$this->$method($value);
219-
}
220-
}
221-
222205
/**
223206
* Get font name
224207
*

src/PhpWord/Style/Image.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Image and memory image style
1414
*/
15-
class Image
15+
class Image extends AbstractStyle
1616
{
1717
const WRAPPING_STYLE_INLINE = 'inline';
1818
const WRAPPING_STYLE_SQUARE = 'square';
@@ -41,13 +41,6 @@ class Image
4141
*/
4242
private $align;
4343

44-
/**
45-
* Wrapping style
46-
*
47-
* @var string
48-
*/
49-
private $wrappingStyle;
50-
5144
/**
5245
* Margin Top
5346
*
@@ -62,6 +55,13 @@ class Image
6255
*/
6356
private $marginLeft;
6457

58+
/**
59+
* Wrapping style
60+
*
61+
* @var string
62+
*/
63+
private $wrappingStyle;
64+
6565
/**
6666
* Create new image style
6767
*/
@@ -75,20 +75,6 @@ public function __construct()
7575
$this->setWrappingStyle(self::WRAPPING_STYLE_INLINE);
7676
}
7777

78-
/**
79-
* Set style value
80-
*
81-
* @param string $key
82-
* @param mixed $value
83-
*/
84-
public function setStyleValue($key, $value)
85-
{
86-
if (substr($key, 0, 1) == '_') {
87-
$key = substr($key, 1);
88-
}
89-
$this->$key = $value;
90-
}
91-
9278
/**
9379
* Get width
9480
*/

src/PhpWord/Style/ListItem.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* List item style
1414
*/
15-
class ListItem
15+
class ListItem extends AbstractStyle
1616
{
1717
const TYPE_NUMBER = 7;
1818
const TYPE_NUMBER_NESTED = 8;
@@ -34,20 +34,6 @@ public function __construct()
3434
$this->listType = self::TYPE_BULLET_FILLED;
3535
}
3636

37-
/**
38-
* Set style value
39-
*
40-
* @param string $key
41-
* @param string $value
42-
*/
43-
public function setStyleValue($key, $value)
44-
{
45-
if (substr($key, 0, 1) == '_') {
46-
$key = substr($key, 1);
47-
}
48-
$this->$key = $value;
49-
}
50-
5137
/**
5238
* Set List Type
5339
*

src/PhpWord/Style/Paragraph.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Paragraph style
1616
*/
17-
class Paragraph
17+
class Paragraph extends AbstractStyle
1818
{
1919
const LINE_HEIGHT = 240;
2020

src/PhpWord/Style/Row.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Table row style
1414
*/
15-
class Row
15+
class Row extends AbstractStyle
1616
{
1717
/**
1818
* Repeat table row on every new page
@@ -42,20 +42,6 @@ public function __construct()
4242
{
4343
}
4444

45-
/**
46-
* Set style value
47-
*
48-
* @param string $key
49-
* @param mixed $value
50-
*/
51-
public function setStyleValue($key, $value)
52-
{
53-
if (substr($key, 0, 1) == '_') {
54-
$key = substr($key, 1);
55-
}
56-
$this->$key = $value;
57-
}
58-
5945
/**
6046
* Set tblHeader
6147
*

src/PhpWord/Style/Section.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Section settings
1414
*/
15-
class Section
15+
class Section extends AbstractStyle
1616
{
1717
/**
1818
* Default Page Size Width

src/PhpWord/Style/TOC.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* TOC style
1414
*/
15-
class TOC
15+
class TOC extends AbstractStyle
1616
{
1717
const TABLEADER_DOT = 'dot';
1818
const TABLEADER_UNDERSCORE = 'underscore';

src/PhpWord/Style/Tab.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Tab style
1616
*/
17-
class Tab
17+
class Tab extends AbstractStyle
1818
{
1919
/**
2020
* Tab Stop Type

src/PhpWord/Style/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Table style
1414
*/
15-
class Table
15+
class Table extends AbstractStyle
1616
{
1717
/**
1818
* Style for first row

src/PhpWord/Style/Tabs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Tabs style
1616
*/
17-
class Tabs
17+
class Tabs extends AbstractStyle
1818
{
1919
/**
2020
* Tabs

src/PhpWord/Writer/AbstractWriter.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PhpOffice\PhpWord\Exception\Exception;
1313
use PhpOffice\PhpWord\PhpWord;
14+
use PhpOffice\PhpWord\Settings;
1415

1516
/**
1617
* Abstract writer class
@@ -148,21 +149,21 @@ public function getDiskCachingDirectory()
148149
/**
149150
* Get temporary file name
150151
*
151-
* If $pFilename is php://output or php://stdout, make it a temporary file
152+
* If $filename is php://output or php://stdout, make it a temporary file
152153
*
153-
* @param string $pFilename
154+
* @param string $filename
154155
* @return string
155156
*/
156-
protected function getTempFile($pFilename)
157+
protected function getTempFile($filename)
157158
{
158-
$this->originalFilename = $pFilename;
159-
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
160-
$pFilename = @tempnam(sys_get_temp_dir(), 'phpword_');
161-
if ($pFilename == '') {
162-
$pFilename = $this->originalFilename;
159+
$this->originalFilename = $filename;
160+
if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') {
161+
$filename = @tempnam(sys_get_temp_dir(), 'phpword_');
162+
if ($filename == '') {
163+
$filename = $this->originalFilename;
163164
}
164165
}
165-
$this->tempFilename = $pFilename;
166+
$this->tempFilename = $filename;
166167

167168
return $this->tempFilename;
168169
}
@@ -181,4 +182,37 @@ protected function cleanupTempFile()
181182
@unlink($this->tempFilename);
182183
}
183184
}
185+
186+
/**
187+
* Get ZipArchive object
188+
*
189+
* @param string $filename
190+
* @return mixed ZipArchive object
191+
*/
192+
protected function getZipArchive($filename)
193+
{
194+
// Create new ZIP file and open it for writing
195+
$zipClass = Settings::getZipClass();
196+
$objZip = new $zipClass();
197+
198+
// Retrieve OVERWRITE and CREATE constants from the instantiated zip class
199+
// This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
200+
$ro = new \ReflectionObject($objZip);
201+
$zipOverWrite = $ro->getConstant('OVERWRITE');
202+
$zipCreate = $ro->getConstant('CREATE');
203+
204+
// Remove any existing file
205+
if (file_exists($filename)) {
206+
unlink($filename);
207+
}
208+
209+
// Try opening the ZIP file
210+
if ($objZip->open($filename, $zipOverWrite) !== true) {
211+
if ($objZip->open($filename, $zipCreate) !== true) {
212+
throw new Exception("Could not open " . $filename . " for writing.");
213+
}
214+
}
215+
216+
return $objZip;
217+
}
184218
}

0 commit comments

Comments
 (0)