Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
41 / 41 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
Field | |
100.00% |
41 / 41 |
|
100.00% |
11 / 11 |
26 | |
100.00% |
1 / 1 |
setFontStyle | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
getFontStyle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
setType | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setProperties | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
getProperties | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setOptions | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
getOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setText | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
getText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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\Element; |
20 | |
21 | use InvalidArgumentException; |
22 | use PhpOffice\PhpWord\Style\Font; |
23 | |
24 | /** |
25 | * Field element. |
26 | * |
27 | * @since 0.11.0 |
28 | * @see http://www.schemacentral.com/sc/ooxml/t-w_CT_SimpleField.html |
29 | */ |
30 | class Field extends AbstractElement |
31 | { |
32 | /** |
33 | * Field properties and options. Depending on type, a field can have different properties |
34 | * and options. |
35 | * |
36 | * @var array |
37 | */ |
38 | protected $fieldsArray = [ |
39 | 'PAGE' => [ |
40 | 'properties' => [ |
41 | 'format' => ['Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'], |
42 | ], |
43 | 'options' => ['PreserveFormat'], |
44 | ], |
45 | 'NUMPAGES' => [ |
46 | 'properties' => [ |
47 | 'format' => ['Arabic', 'ArabicDash', 'CardText', 'DollarText', 'Ordinal', 'OrdText', |
48 | 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN', 'Caps', 'FirstCap', 'Lower', 'Upper', ], |
49 | 'numformat' => ['0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%'], |
50 | ], |
51 | 'options' => ['PreserveFormat'], |
52 | ], |
53 | 'DATE' => [ |
54 | 'properties' => [ |
55 | 'dateformat' => [ |
56 | // Generic formats |
57 | 'yyyy-MM-dd', 'yyyy-MM', 'MMM-yy', 'MMM-yyyy', 'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss', |
58 | // Day-Month-Year formats |
59 | 'dddd d MMMM yyyy', 'd MMMM yyyy', 'd-MMM-yy', 'd MMM. yy', |
60 | 'd-M-yy', 'd-M-yy h:mm', 'd-M-yy h:mm:ss', 'd-M-yy h:mm am/pm', 'd-M-yy h:mm:ss am/pm', 'd-M-yy HH:mm', 'd-M-yy HH:mm:ss', |
61 | 'd/M/yy', 'd/M/yy h:mm', 'd/M/yy h:mm:ss', 'd/M/yy h:mm am/pm', 'd/M/yy h:mm:ss am/pm', 'd/M/yy HH:mm', 'd/M/yy HH:mm:ss', |
62 | 'd-M-yyyy', 'd-M-yyyy h:mm', 'd-M-yyyy h:mm:ss', 'd-M-yyyy h:mm am/pm', 'd-M-yyyy h:mm:ss am/pm', 'd-M-yyyy HH:mm', 'd-M-yyyy HH:mm:ss', |
63 | 'd/M/yyyy', 'd/M/yyyy h:mm', 'd/M/yyyy h:mm:ss', 'd/M/yyyy h:mm am/pm', 'd/M/yyyy h:mm:ss am/pm', 'd/M/yyyy HH:mm', 'd/M/yyyy HH:mm:ss', |
64 | // Month-Day-Year formats |
65 | 'dddd, MMMM d yyyy', 'MMMM d yyyy', 'MMM-d-yy', 'MMM. d yy', |
66 | 'M-d-yy', 'M-d-yy h:mm', 'M-d-yy h:mm:ss', 'M-d-yy h:mm am/pm', 'M-d-yy h:mm:ss am/pm', 'M-d-yy HH:mm', 'M-d-yy HH:mm:ss', |
67 | 'M/d/yy', 'M/d/yy h:mm', 'M/d/yy h:mm:ss', 'M/d/yy h:mm am/pm', 'M/d/yy h:mm:ss am/pm', 'M/d/yy HH:mm', 'M/d/yy HH:mm:ss', |
68 | 'M-d-yyyy', 'M-d-yyyy h:mm', 'M-d-yyyy h:mm:ss', 'M-d-yyyy h:mm am/pm', 'M-d-yyyy h:mm:ss am/pm', 'M-d-yyyy HH:mm', 'M-d-yyyy HH:mm:ss', |
69 | 'M/d/yyyy', 'M/d/yyyy h:mm', 'M/d/yyyy h:mm:ss', 'M/d/yyyy h:mm am/pm', 'M/d/yyyy h:mm:ss am/pm', 'M/d/yyyy HH:mm', 'M/d/yyyy HH:mm:ss', |
70 | ], |
71 | ], |
72 | 'options' => ['PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat'], |
73 | ], |
74 | 'MACROBUTTON' => [ |
75 | 'properties' => ['macroname' => ''], |
76 | ], |
77 | 'XE' => [ |
78 | 'properties' => [], |
79 | 'options' => ['Bold', 'Italic'], |
80 | ], |
81 | 'INDEX' => [ |
82 | 'properties' => [], |
83 | 'options' => ['PreserveFormat'], |
84 | ], |
85 | 'STYLEREF' => [ |
86 | 'properties' => ['StyleIdentifier' => ''], |
87 | 'options' => ['PreserveFormat'], |
88 | ], |
89 | 'FILENAME' => [ |
90 | 'properties' => [ |
91 | 'format' => ['Upper', 'Lower', 'FirstCap', 'Caps'], |
92 | ], |
93 | 'options' => ['Path', 'PreserveFormat'], |
94 | ], |
95 | 'REF' => [ |
96 | 'properties' => ['name' => ''], |
97 | 'options' => ['f', 'h', 'n', 'p', 'r', 't', 'w'], |
98 | ], |
99 | ]; |
100 | |
101 | /** |
102 | * Field type. |
103 | * |
104 | * @var string |
105 | */ |
106 | protected $type; |
107 | |
108 | /** |
109 | * Field text. |
110 | * |
111 | * @var string|TextRun |
112 | */ |
113 | protected $text; |
114 | |
115 | /** |
116 | * Field properties. |
117 | * |
118 | * @var array |
119 | */ |
120 | protected $properties = []; |
121 | |
122 | /** |
123 | * Field options. |
124 | * |
125 | * @var array |
126 | */ |
127 | protected $options = []; |
128 | |
129 | /** |
130 | * Font style. |
131 | * |
132 | * @var Font|string |
133 | */ |
134 | protected $fontStyle; |
135 | |
136 | /** |
137 | * Set Font style. |
138 | * |
139 | * @param array|Font|string $style |
140 | * |
141 | * @return Font|string |
142 | */ |
143 | public function setFontStyle($style = null) |
144 | { |
145 | if ($style instanceof Font) { |
146 | $this->fontStyle = $style; |
147 | } elseif (is_array($style)) { |
148 | $this->fontStyle = new Font('text'); |
149 | $this->fontStyle->setStyleByArray($style); |
150 | } elseif (null === $style) { |
151 | $this->fontStyle = null; |
152 | } else { |
153 | $this->fontStyle = $style; |
154 | } |
155 | |
156 | return $this->fontStyle; |
157 | } |
158 | |
159 | /** |
160 | * Get Font style. |
161 | * |
162 | * @return Font|string |
163 | */ |
164 | public function getFontStyle() |
165 | { |
166 | return $this->fontStyle; |
167 | } |
168 | |
169 | /** |
170 | * Create a new Field Element. |
171 | * |
172 | * @param string $type |
173 | * @param array $properties |
174 | * @param array $options |
175 | * @param null|string|TextRun $text |
176 | * @param array|Font|string $fontStyle |
177 | */ |
178 | public function __construct($type = null, $properties = [], $options = [], $text = null, $fontStyle = null) |
179 | { |
180 | $this->setType($type); |
181 | $this->setProperties($properties); |
182 | $this->setOptions($options); |
183 | $this->setText($text); |
184 | $this->setFontStyle($fontStyle); |
185 | } |
186 | |
187 | /** |
188 | * Set Field type. |
189 | * |
190 | * @param string $type |
191 | * |
192 | * @return string |
193 | */ |
194 | public function setType($type = null) |
195 | { |
196 | if (isset($type)) { |
197 | if (isset($this->fieldsArray[$type])) { |
198 | $this->type = $type; |
199 | } else { |
200 | throw new InvalidArgumentException("Invalid type '$type'"); |
201 | } |
202 | } |
203 | |
204 | return $this->type; |
205 | } |
206 | |
207 | /** |
208 | * Get Field type. |
209 | * |
210 | * @return string |
211 | */ |
212 | public function getType() |
213 | { |
214 | return $this->type; |
215 | } |
216 | |
217 | /** |
218 | * Set Field properties. |
219 | * |
220 | * @param array $properties |
221 | * |
222 | * @return self |
223 | */ |
224 | public function setProperties($properties = []) |
225 | { |
226 | if (is_array($properties)) { |
227 | foreach (array_keys($properties) as $propkey) { |
228 | if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) { |
229 | throw new InvalidArgumentException("Invalid property '$propkey'"); |
230 | } |
231 | } |
232 | $this->properties = array_merge($this->properties, $properties); |
233 | } |
234 | |
235 | return $this->properties; |
236 | } |
237 | |
238 | /** |
239 | * Get Field properties. |
240 | * |
241 | * @return array |
242 | */ |
243 | public function getProperties() |
244 | { |
245 | return $this->properties; |
246 | } |
247 | |
248 | /** |
249 | * Set Field options. |
250 | * |
251 | * @param array $options |
252 | * |
253 | * @return self |
254 | */ |
255 | public function setOptions($options = []) |
256 | { |
257 | if (is_array($options)) { |
258 | foreach (array_keys($options) as $optionkey) { |
259 | if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') { |
260 | throw new InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options'])); |
261 | } |
262 | } |
263 | $this->options = array_merge($this->options, $options); |
264 | } |
265 | |
266 | return $this->options; |
267 | } |
268 | |
269 | /** |
270 | * Get Field properties. |
271 | * |
272 | * @return array |
273 | */ |
274 | public function getOptions() |
275 | { |
276 | return $this->options; |
277 | } |
278 | |
279 | /** |
280 | * Set Field text. |
281 | * |
282 | * @param null|string|TextRun $text |
283 | * |
284 | * @return null|string|TextRun |
285 | */ |
286 | public function setText($text = null) |
287 | { |
288 | if (isset($text)) { |
289 | if (is_string($text) || $text instanceof TextRun) { |
290 | $this->text = $text; |
291 | } else { |
292 | throw new InvalidArgumentException('Invalid text'); |
293 | } |
294 | } |
295 | |
296 | return $this->text; |
297 | } |
298 | |
299 | /** |
300 | * Get Field text. |
301 | * |
302 | * @return string|TextRun |
303 | */ |
304 | public function getText() |
305 | { |
306 | return $this->text; |
307 | } |
308 | } |