No Description

Field.php 4.8KB

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