Ei kuvausta

AbstractStyle.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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\Style;
  18. use PhpOffice\PhpWord\Shared\String;
  19. /**
  20. * Abstract style class
  21. *
  22. * @since 0.10.0
  23. */
  24. abstract class AbstractStyle
  25. {
  26. /**
  27. * Style name
  28. *
  29. * @var string
  30. */
  31. protected $styleName;
  32. /**
  33. * Index number in Style collection for named style
  34. *
  35. * This number starts from one and defined in Style::setStyleValues()
  36. *
  37. * @var int|null
  38. */
  39. protected $index;
  40. /**
  41. * Aliases
  42. *
  43. * @var array
  44. */
  45. protected $aliases = array();
  46. /**
  47. * Is this an automatic style? (Used primarily in OpenDocument driver)
  48. *
  49. * @var bool
  50. * @since 0.11.0
  51. */
  52. private $isAuto = false;
  53. /**
  54. * Get style name
  55. *
  56. * @return string
  57. */
  58. public function getStyleName()
  59. {
  60. return $this->styleName;
  61. }
  62. /**
  63. * Set style name
  64. *
  65. * @param string $value
  66. * @return self
  67. */
  68. public function setStyleName($value)
  69. {
  70. $this->styleName = $value;
  71. return $this;
  72. }
  73. /**
  74. * Get index number
  75. *
  76. * @return int|null
  77. */
  78. public function getIndex()
  79. {
  80. return $this->index;
  81. }
  82. /**
  83. * Set index number
  84. *
  85. * @param int|null $value
  86. * @return self
  87. */
  88. public function setIndex($value = null)
  89. {
  90. $this->index = $this->setIntVal($value, $this->index);
  91. return $this;
  92. }
  93. /**
  94. * Get is automatic style flag
  95. *
  96. * @return bool
  97. */
  98. public function isAuto()
  99. {
  100. return $this->isAuto;
  101. }
  102. /**
  103. * Set is automatic style flag
  104. *
  105. * @param bool $value
  106. * @return self
  107. */
  108. public function setAuto($value = true)
  109. {
  110. $this->isAuto = $this->setBoolVal($value, $this->isAuto);
  111. return $this;
  112. }
  113. /**
  114. * Return style value of child style object, e.g. `left` from `Indentation` child style of `Paragraph`
  115. *
  116. * @param \PhpOffice\PhpWord\Style\AbstractStyle $substyleObject
  117. * @param string $substyleProperty
  118. * @return mixed
  119. * @since 0.12.0
  120. */
  121. public function getChildStyleValue($substyleObject, $substyleProperty)
  122. {
  123. if ($substyleObject !== null) {
  124. $method = "get{$substyleProperty}";
  125. return $substyleObject->$method();
  126. } else {
  127. return null;
  128. }
  129. }
  130. /**
  131. * Set style value template method
  132. *
  133. * Some child classes have their own specific overrides.
  134. * Backward compability check for versions < 0.10.0 which use underscore
  135. * prefix for their private properties.
  136. * Check if the set method is exists. Throws an exception?
  137. *
  138. * @param string $key
  139. * @param string $value
  140. * @return self
  141. */
  142. public function setStyleValue($key, $value)
  143. {
  144. if (isset($this->aliases[$key])) {
  145. $key = $this->aliases[$key];
  146. }
  147. $method = 'set' . String::removeUnderscorePrefix($key);
  148. if (method_exists($this, $method)) {
  149. $this->$method($value);
  150. }
  151. return $this;
  152. }
  153. /**
  154. * Set style by using associative array
  155. *
  156. * @param array $values
  157. * @return self
  158. */
  159. public function setStyleByArray($values = array())
  160. {
  161. foreach ($values as $key => $value) {
  162. $this->setStyleValue($key, $value);
  163. }
  164. return $this;
  165. }
  166. /**
  167. * Set default for null and empty value
  168. *
  169. * @param string $value (was: mixed)
  170. * @param string $default (was: mixed)
  171. * @return string (was: mixed)
  172. */
  173. protected function setNonEmptyVal($value, $default)
  174. {
  175. if ($value === null || $value == '') {
  176. $value = $default;
  177. }
  178. return $value;
  179. }
  180. /**
  181. * Set bool value
  182. *
  183. * @param bool $value
  184. * @param bool $default
  185. * @return bool
  186. */
  187. protected function setBoolVal($value, $default)
  188. {
  189. if (!is_bool($value)) {
  190. $value = $default;
  191. }
  192. return $value;
  193. }
  194. /**
  195. * Set numeric value
  196. *
  197. * @param mixed $value
  198. * @param int|float|null $default
  199. * @return int|float|null
  200. */
  201. protected function setNumericVal($value, $default = null)
  202. {
  203. if (!is_numeric($value)) {
  204. $value = $default;
  205. }
  206. return $value;
  207. }
  208. /**
  209. * Set integer value: Convert string that contains only numeric into integer
  210. *
  211. * @param int|null $value
  212. * @param int|null $default
  213. * @return int|null
  214. */
  215. protected function setIntVal($value, $default = null)
  216. {
  217. if (is_string($value) && (preg_match('/[^\d]/', $value) == 0)) {
  218. $value = intval($value);
  219. }
  220. if (!is_numeric($value)) {
  221. $value = $default;
  222. } else {
  223. $value = intval($value);
  224. }
  225. return $value;
  226. }
  227. /**
  228. * Set float value: Convert string that contains only numeric into float
  229. *
  230. * @param mixed $value
  231. * @param float|null $default
  232. * @return float|null
  233. */
  234. protected function setFloatVal($value, $default = null)
  235. {
  236. if (is_string($value) && (preg_match('/[^\d\.\,]/', $value) == 0)) {
  237. $value = floatval($value);
  238. }
  239. if (!is_numeric($value)) {
  240. $value = $default;
  241. }
  242. return $value;
  243. }
  244. /**
  245. * Set enum value
  246. *
  247. * @param mixed $value
  248. * @param array $enum
  249. * @param mixed $default
  250. * @return mixed
  251. * @throws \InvalidArgumentException
  252. */
  253. protected function setEnumVal($value = null, $enum = array(), $default = null)
  254. {
  255. if ($value != null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) {
  256. throw new \InvalidArgumentException("Invalid style value: {$value} Options:".join(',', $enum));
  257. } elseif ($value === null || trim($value) == '') {
  258. $value = $default;
  259. }
  260. return $value;
  261. }
  262. /**
  263. * Set object value
  264. *
  265. * @param mixed $value
  266. * @param string $styleName
  267. * @param mixed &$style
  268. * @return mixed
  269. */
  270. protected function setObjectVal($value, $styleName, &$style)
  271. {
  272. $styleClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $styleName;
  273. if (is_array($value)) {
  274. /** @var \PhpOffice\PhpWord\Style\AbstractStyle $style Type hint */
  275. if (!$style instanceof $styleClass) {
  276. $style = new $styleClass();
  277. }
  278. $style->setStyleByArray($value);
  279. } else {
  280. $style = $value;
  281. }
  282. return $style;
  283. }
  284. /**
  285. * Set $property value and set $pairProperty = false when $value = true
  286. *
  287. * @param bool &$property
  288. * @param bool &$pairProperty
  289. * @param bool $value
  290. * @return self
  291. */
  292. protected function setPairedVal(&$property, &$pairProperty, $value)
  293. {
  294. $property = $this->setBoolVal($value, $property);
  295. if ($value == true) {
  296. $pairProperty = false;
  297. }
  298. return $this;
  299. }
  300. /**
  301. * Set style using associative array
  302. *
  303. * @param array $style
  304. * @return self
  305. * @deprecated 0.11.0
  306. * @codeCoverageIgnore
  307. */
  308. public function setArrayStyle(array $style = array())
  309. {
  310. return $this->setStyleByArray($style);
  311. }
  312. }