|
| 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 | + * @link https://github.com/PHPOffice/PHPWord |
| 14 | + * @copyright 2010-2014 PHPWord contributors |
| 15 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | + */ |
| 17 | + |
| 18 | +namespace PhpOffice\PhpWord\Element; |
| 19 | + |
| 20 | +/** |
| 21 | + * Field element |
| 22 | + * |
| 23 | + * @since 0.11.0 |
| 24 | + * @link http://www.schemacentral.com/sc/ooxml/t-w_CT_SimpleField.html |
| 25 | + */ |
| 26 | +class Field extends AbstractElement |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Field properties and options. Depending on type, a field can have different properties |
| 30 | + * and options |
| 31 | + * |
| 32 | + * @var array |
| 33 | + */ |
| 34 | + protected $fieldsArray = array( |
| 35 | + 'PAGE'=>array( |
| 36 | + 'properties'=>array( |
| 37 | + 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), |
| 38 | + ), |
| 39 | + 'options'=>array('PreserveFormat') |
| 40 | + ), |
| 41 | + 'NUMPAGES'=>array( |
| 42 | + 'properties'=>array( |
| 43 | + 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), |
| 44 | + 'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%') |
| 45 | + ), |
| 46 | + 'options'=>array('PreserveFormat') |
| 47 | + ), |
| 48 | + 'DATE'=>array( |
| 49 | + 'properties'=> array( |
| 50 | + 'dateformat' =>array('d-M-yyyy', 'dddd d MMMM yyyy', 'd MMMM yyyy', 'd-M-yy', 'yyyy-MM-dd', |
| 51 | + 'd-MMM-yy', 'd/M/yyyy', 'd MMM. yy', 'd/M/yy', 'MMM-yy', 'd-M-yyy H:mm', 'd-M-yyyy H:mm:ss', |
| 52 | + 'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss') |
| 53 | + ), |
| 54 | + 'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat') |
| 55 | + ) |
| 56 | + ); |
| 57 | + |
| 58 | + /** |
| 59 | + * Field type |
| 60 | + * |
| 61 | + * @var string |
| 62 | + */ |
| 63 | + protected $type; |
| 64 | + |
| 65 | + /** |
| 66 | + * Field properties |
| 67 | + * |
| 68 | + * @var array |
| 69 | + */ |
| 70 | + protected $properties = array(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Field options |
| 74 | + * |
| 75 | + * @var array |
| 76 | + */ |
| 77 | + protected $options = array(); |
| 78 | + |
| 79 | + /** |
| 80 | + * Create a new Field Element |
| 81 | + * |
| 82 | + * @param string $type |
| 83 | + * @param array $properties |
| 84 | + * @param array $options |
| 85 | + */ |
| 86 | + public function __construct($type = null, $properties = array(), $options = array()) |
| 87 | + { |
| 88 | + $this->setType($type); |
| 89 | + $this->setProperties($properties); |
| 90 | + $this->setOptions($options); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Set Field type |
| 95 | + * |
| 96 | + * @param string $type |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + public function setType($type = null) |
| 100 | + { |
| 101 | + if (isset($type)) { |
| 102 | + if (array_key_exists($type, $this->fieldsArray)) { |
| 103 | + $this->type = $type; |
| 104 | + } else { |
| 105 | + throw new \InvalidArgumentException("Invalid type"); |
| 106 | + } |
| 107 | + } |
| 108 | + return $this->type; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get Field type |
| 113 | + * |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + public function getType() |
| 117 | + { |
| 118 | + return $this->type; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Set Field properties |
| 123 | + * |
| 124 | + * @param array $properties |
| 125 | + * @return self |
| 126 | + */ |
| 127 | + public function setProperties($properties = array()) |
| 128 | + { |
| 129 | + if (is_array($properties)) { |
| 130 | + foreach (array_keys($properties) as $propkey) { |
| 131 | + if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) { |
| 132 | + throw new \InvalidArgumentException("Invalid property"); |
| 133 | + } |
| 134 | + } |
| 135 | + $this->properties = array_merge($this->properties, $properties); |
| 136 | + } |
| 137 | + return $this->properties; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get Field properties |
| 142 | + * |
| 143 | + * @return array |
| 144 | + */ |
| 145 | + public function getProperties() |
| 146 | + { |
| 147 | + return $this->properties; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Set Field options |
| 152 | + * |
| 153 | + * @param array $options |
| 154 | + * @return self |
| 155 | + */ |
| 156 | + public function setOptions($options = array()) |
| 157 | + { |
| 158 | + if (is_array($options)) { |
| 159 | + foreach (array_keys($options) as $optionkey) { |
| 160 | + if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) { |
| 161 | + throw new \InvalidArgumentException("Invalid option"); |
| 162 | + } |
| 163 | + } |
| 164 | + $this->options = array_merge($this->options, $options); |
| 165 | + } |
| 166 | + return $this->options; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Get Field properties |
| 171 | + * |
| 172 | + * @return array |
| 173 | + */ |
| 174 | + public function getOptions() |
| 175 | + { |
| 176 | + return $this->options; |
| 177 | + } |
| 178 | +} |
0 commit comments