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