-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathStyle.php
218 lines (199 loc) · 5.26 KB
/
Style.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?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.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Style\AbstractStyle;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Numbering;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style\Table;
/**
* Style collection.
*/
class Style
{
/**
* Style register.
*
* @var array
*/
private static $styles = [];
/**
* Add paragraph style.
*
* @param string $styleName
* @param AbstractStyle|array $styles
*
* @return Paragraph
*/
public static function addParagraphStyle($styleName, $styles)
{
return self::setStyleValues($styleName, new Paragraph(), $styles);
}
/**
* Add font style.
*
* @param string $styleName
* @param AbstractStyle|array $fontStyle
* @param AbstractStyle|array $paragraphStyle
*
* @return Font
*/
public static function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
{
return self::setStyleValues($styleName, new Font('text', $paragraphStyle), $fontStyle);
}
/**
* Add link style.
*
* @param string $styleName
* @param AbstractStyle|array $styles
*
* @return Font
*/
public static function addLinkStyle($styleName, $styles)
{
return self::setStyleValues($styleName, new Font('link'), $styles);
}
/**
* Add numbering style.
*
* @param string $styleName
* @param AbstractStyle|array $styleValues
*
* @return Numbering
*
* @since 0.10.0
*/
public static function addNumberingStyle($styleName, $styleValues)
{
return self::setStyleValues($styleName, new Numbering(), $styleValues);
}
/**
* Add title style.
*
* @param null|int $depth Provide null to set title font
* @param AbstractStyle|array $fontStyle
* @param AbstractStyle|array $paragraphStyle
*
* @return Font
*/
public static function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
{
if (empty($depth)) {
$styleName = 'Title';
} else {
$styleName = "Heading_{$depth}";
}
return self::setStyleValues($styleName, new Font('title', $paragraphStyle), $fontStyle);
}
/**
* Add table style.
*
* @param string $styleName
* @param array $styleTable
* @param null|array $styleFirstRow
*
* @return Table
*/
public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
{
return self::setStyleValues($styleName, new Table($styleTable, $styleFirstRow), null);
}
/**
* Count styles.
*
* @return int
*
* @since 0.10.0
*/
public static function countStyles()
{
return count(self::$styles);
}
/**
* Reset styles.
*
* @since 0.10.0
*/
public static function resetStyles(): void
{
self::$styles = [];
}
/**
* Set default paragraph style.
*
* @param AbstractStyle|array $styles Paragraph style definition
*
* @return Paragraph
*/
public static function setDefaultParagraphStyle($styles)
{
return self::addParagraphStyle('Normal', $styles);
}
/**
* Get all styles.
*
* @return AbstractStyle[]
*/
public static function getStyles()
{
return self::$styles;
}
/**
* Get style by name.
*
* @param string $styleName
*
* @return ?AbstractStyle Paragraph|Font|Table|Numbering
*/
public static function getStyle($styleName)
{
if (isset(self::$styles[$styleName])) {
return self::$styles[$styleName];
}
return null;
}
/**
* Set style values and put it to static style collection.
*
* The $styleValues could be an array or object
*
* @param string $name
* @param AbstractStyle $style
* @param AbstractStyle|array $value
*
* @return AbstractStyle
*/
private static function setStyleValues($name, $style, $value = null)
{
if (!isset(self::$styles[$name])) {
if ($value !== null) {
if (is_array($value)) {
$style->setStyleByArray($value);
} elseif ($value instanceof AbstractStyle) {
if (get_class($style) == get_class($value)) {
$style = $value;
}
}
}
$style->setStyleName($name);
$style->setIndex(self::countStyles() + 1); // One based index
self::$styles[$name] = $style;
}
return self::getStyle($name);
}
}