Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
39 / 39 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Field | |
100.00% |
39 / 39 |
|
100.00% |
11 / 11 |
24 | |
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% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getProperties | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setOptions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| 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 null|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 | * @return self |
| 221 | */ |
| 222 | public function setProperties(array $properties = []) |
| 223 | { |
| 224 | foreach (array_keys($properties) as $propkey) { |
| 225 | if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) { |
| 226 | throw new InvalidArgumentException("Invalid property '$propkey'"); |
| 227 | } |
| 228 | } |
| 229 | $this->properties = array_merge($this->properties, $properties); |
| 230 | |
| 231 | return $this; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get Field properties. |
| 236 | * |
| 237 | * @return array |
| 238 | */ |
| 239 | public function getProperties() |
| 240 | { |
| 241 | return $this->properties; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Set Field options. |
| 246 | * |
| 247 | * @return self |
| 248 | */ |
| 249 | public function setOptions(array $options = []) |
| 250 | { |
| 251 | foreach (array_keys($options) as $optionkey) { |
| 252 | if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') { |
| 253 | throw new InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options'])); |
| 254 | } |
| 255 | } |
| 256 | $this->options = array_merge($this->options, $options); |
| 257 | |
| 258 | return $this; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Get Field properties. |
| 263 | * |
| 264 | * @return array |
| 265 | */ |
| 266 | public function getOptions() |
| 267 | { |
| 268 | return $this->options; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Set Field text. |
| 273 | * |
| 274 | * @param null|mixed|string|TextRun $text |
| 275 | * |
| 276 | * @return null|string|TextRun |
| 277 | */ |
| 278 | public function setText($text = null) |
| 279 | { |
| 280 | if (null !== $text) { |
| 281 | if (is_string($text) || $text instanceof TextRun) { |
| 282 | $this->text = $text; |
| 283 | } else { |
| 284 | throw new InvalidArgumentException('Invalid text'); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return $this->text; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get Field text. |
| 293 | * |
| 294 | * @return string|TextRun |
| 295 | */ |
| 296 | public function getText() |
| 297 | { |
| 298 | return $this->text; |
| 299 | } |
| 300 | } |