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