Field.php 9.0 KB

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