AbstractStyle.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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\Style;
  18. use PhpOffice\PhpWord\Shared\Text;
  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. }
  127. return null;
  128. }
  129. /**
  130. * Set style value template method
  131. *
  132. * Some child classes have their own specific overrides.
  133. * Backward compability check for versions < 0.10.0 which use underscore
  134. * prefix for their private properties.
  135. * Check if the set method is exists. Throws an exception?
  136. *
  137. * @param string $key
  138. * @param string $value
  139. * @return self
  140. */
  141. public function setStyleValue($key, $value)
  142. {
  143. if (isset($this->aliases[$key])) {
  144. $key = $this->aliases[$key];
  145. }
  146. $method = 'set' . Text::removeUnderscorePrefix($key);
  147. if (method_exists($this, $method)) {
  148. $this->$method($value);
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Set style by using associative array
  154. *
  155. * @param array $values
  156. * @return self
  157. */
  158. public function setStyleByArray($values = array())
  159. {
  160. foreach ($values as $key => $value) {
  161. $this->setStyleValue($key, $value);
  162. }
  163. return $this;
  164. }
  165. /**
  166. * Set default for null and empty value
  167. *
  168. * @param string $value (was: mixed)
  169. * @param string $default (was: mixed)
  170. * @return string (was: mixed)
  171. */
  172. protected function setNonEmptyVal($value, $default)
  173. {
  174. if ($value === null || $value == '') {
  175. $value = $default;
  176. }
  177. return $value;
  178. }
  179. /**
  180. * Set bool value
  181. *
  182. * @param bool $value
  183. * @param bool $default
  184. * @return bool
  185. */
  186. protected function setBoolVal($value, $default)
  187. {
  188. if (!is_bool($value)) {
  189. $value = $default;
  190. }
  191. return $value;
  192. }
  193. /**
  194. * Set numeric value
  195. *
  196. * @param mixed $value
  197. * @param int|float|null $default
  198. * @return int|float|null
  199. */
  200. protected function setNumericVal($value, $default = null)
  201. {
  202. if (!is_numeric($value)) {
  203. $value = $default;
  204. }
  205. return $value;
  206. }
  207. /**
  208. * Set integer value: Convert string that contains only numeric into integer
  209. *
  210. * @param int|null $value
  211. * @param int|null $default
  212. * @return int|null
  213. */
  214. protected function setIntVal($value, $default = null)
  215. {
  216. if (is_string($value) && (preg_match('/[^\d]/', $value) == 0)) {
  217. $value = (int) $value;
  218. }
  219. if (!is_numeric($value)) {
  220. $value = $default;
  221. } else {
  222. $value = (int) $value;
  223. }
  224. return $value;
  225. }
  226. /**
  227. * Set float value: Convert string that contains only numeric into float
  228. *
  229. * @param mixed $value
  230. * @param float|null $default
  231. * @return float|null
  232. */
  233. protected function setFloatVal($value, $default = null)
  234. {
  235. if (is_string($value) && (preg_match('/[^\d\.\,]/', $value) == 0)) {
  236. $value = (float) $value;
  237. }
  238. if (!is_numeric($value)) {
  239. $value = $default;
  240. }
  241. return $value;
  242. }
  243. /**
  244. * Set enum value
  245. *
  246. * @param mixed $value
  247. * @param array $enum
  248. * @param mixed $default
  249. *
  250. * @throws \InvalidArgumentException
  251. * @return mixed
  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:" . implode(',', $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. * @deprecated 0.11.0
  304. *
  305. * @param array $style
  306. *
  307. * @return self
  308. *
  309. * @codeCoverageIgnore
  310. */
  311. public function setArrayStyle(array $style = array())
  312. {
  313. return $this->setStyleByArray($style);
  314. }
  315. }