/vendor/phpoffice/phpexcel/Classes/PHPExcel/Style/Alignment.php

https://gitlab.com/techniconline/kmc · PHP · 475 lines · 243 code · 36 blank · 196 comment · 56 complexity · d6a406532a92c473268131965fb434a5 MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Style
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Style_Alignment
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Alignment extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  35. {
  36. /* Horizontal alignment styles */
  37. const HORIZONTAL_GENERAL = 'general';
  38. const HORIZONTAL_LEFT = 'left';
  39. const HORIZONTAL_RIGHT = 'right';
  40. const HORIZONTAL_CENTER = 'center';
  41. const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
  42. const HORIZONTAL_JUSTIFY = 'justify';
  43. const HORIZONTAL_FILL = 'fill';
  44. const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only
  45. /* Vertical alignment styles */
  46. const VERTICAL_BOTTOM = 'bottom';
  47. const VERTICAL_TOP = 'top';
  48. const VERTICAL_CENTER = 'center';
  49. const VERTICAL_JUSTIFY = 'justify';
  50. const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
  51. /* Read order */
  52. const READORDER_CONTEXT = 0;
  53. const READORDER_LTR = 1;
  54. const READORDER_RTL = 2;
  55. /**
  56. * Horizontal alignment
  57. *
  58. * @var string
  59. */
  60. protected $_horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  61. /**
  62. * Vertical alignment
  63. *
  64. * @var string
  65. */
  66. protected $_vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  67. /**
  68. * Text rotation
  69. *
  70. * @var integer
  71. */
  72. protected $_textRotation = 0;
  73. /**
  74. * Wrap text
  75. *
  76. * @var boolean
  77. */
  78. protected $_wrapText = FALSE;
  79. /**
  80. * Shrink to fit
  81. *
  82. * @var boolean
  83. */
  84. protected $_shrinkToFit = FALSE;
  85. /**
  86. * Indent - only possible with horizontal alignment left and right
  87. *
  88. * @var integer
  89. */
  90. protected $_indent = 0;
  91. /**
  92. * Read order
  93. *
  94. * @var integer
  95. */
  96. protected $_readorder = 0;
  97. /**
  98. * Create a new PHPExcel_Style_Alignment
  99. *
  100. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  101. * Leave this value at default unless you understand exactly what
  102. * its ramifications are
  103. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  104. * Leave this value at default unless you understand exactly what
  105. * its ramifications are
  106. */
  107. public function __construct($isSupervisor = FALSE, $isConditional = FALSE)
  108. {
  109. // Supervisor?
  110. parent::__construct($isSupervisor);
  111. if ($isConditional) {
  112. $this->_horizontal = NULL;
  113. $this->_vertical = NULL;
  114. $this->_textRotation = NULL;
  115. }
  116. }
  117. /**
  118. * Get the shared style component for the currently active cell in currently active sheet.
  119. * Only used for style supervisor
  120. *
  121. * @return PHPExcel_Style_Alignment
  122. */
  123. public function getSharedComponent()
  124. {
  125. return $this->_parent->getSharedComponent()->getAlignment();
  126. }
  127. /**
  128. * Build style array from subcomponents
  129. *
  130. * @param array $array
  131. * @return array
  132. */
  133. public function getStyleArray($array)
  134. {
  135. return array('alignment' => $array);
  136. }
  137. /**
  138. * Apply styles from array
  139. *
  140. * <code>
  141. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  142. * array(
  143. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  144. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  145. * 'rotation' => 0,
  146. * 'wrap' => TRUE
  147. * )
  148. * );
  149. * </code>
  150. *
  151. * @param array $pStyles Array containing style information
  152. * @throws PHPExcel_Exception
  153. * @return PHPExcel_Style_Alignment
  154. */
  155. public function applyFromArray($pStyles = NULL)
  156. {
  157. if (is_array($pStyles)) {
  158. if ($this->_isSupervisor) {
  159. $this->getActiveSheet()->getStyle($this->getSelectedCells())
  160. ->applyFromArray($this->getStyleArray($pStyles));
  161. } else {
  162. if (isset($pStyles['horizontal'])) {
  163. $this->setHorizontal($pStyles['horizontal']);
  164. }
  165. if (isset($pStyles['vertical'])) {
  166. $this->setVertical($pStyles['vertical']);
  167. }
  168. if (isset($pStyles['rotation'])) {
  169. $this->setTextRotation($pStyles['rotation']);
  170. }
  171. if (isset($pStyles['wrap'])) {
  172. $this->setWrapText($pStyles['wrap']);
  173. }
  174. if (isset($pStyles['shrinkToFit'])) {
  175. $this->setShrinkToFit($pStyles['shrinkToFit']);
  176. }
  177. if (isset($pStyles['indent'])) {
  178. $this->setIndent($pStyles['indent']);
  179. }
  180. if (isset($pStyles['readorder'])) {
  181. $this->setReadorder($pStyles['readorder']);
  182. }
  183. }
  184. } else {
  185. throw new PHPExcel_Exception("Invalid style array passed.");
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Get Horizontal
  191. *
  192. * @return string
  193. */
  194. public function getHorizontal()
  195. {
  196. if ($this->_isSupervisor) {
  197. return $this->getSharedComponent()->getHorizontal();
  198. }
  199. return $this->_horizontal;
  200. }
  201. /**
  202. * Set Horizontal
  203. *
  204. * @param string $pValue
  205. * @return PHPExcel_Style_Alignment
  206. */
  207. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL)
  208. {
  209. if ($pValue == '') {
  210. $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  211. }
  212. if ($this->_isSupervisor) {
  213. $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
  214. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  215. } else {
  216. $this->_horizontal = $pValue;
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Get Vertical
  222. *
  223. * @return string
  224. */
  225. public function getVertical()
  226. {
  227. if ($this->_isSupervisor) {
  228. return $this->getSharedComponent()->getVertical();
  229. }
  230. return $this->_vertical;
  231. }
  232. /**
  233. * Set Vertical
  234. *
  235. * @param string $pValue
  236. * @return PHPExcel_Style_Alignment
  237. */
  238. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM)
  239. {
  240. if ($pValue == '') {
  241. $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  242. }
  243. if ($this->_isSupervisor) {
  244. $styleArray = $this->getStyleArray(array('vertical' => $pValue));
  245. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  246. } else {
  247. $this->_vertical = $pValue;
  248. }
  249. return $this;
  250. }
  251. /**
  252. * Get TextRotation
  253. *
  254. * @return int
  255. */
  256. public function getTextRotation()
  257. {
  258. if ($this->_isSupervisor) {
  259. return $this->getSharedComponent()->getTextRotation();
  260. }
  261. return $this->_textRotation;
  262. }
  263. /**
  264. * Set TextRotation
  265. *
  266. * @param int $pValue
  267. * @throws PHPExcel_Exception
  268. * @return PHPExcel_Style_Alignment
  269. */
  270. public function setTextRotation($pValue = 0)
  271. {
  272. // Excel2007 value 255 => PHPExcel value -165
  273. if ($pValue == 255) {
  274. $pValue = -165;
  275. }
  276. // Set rotation
  277. if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
  278. if ($this->_isSupervisor) {
  279. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  280. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  281. } else {
  282. $this->_textRotation = $pValue;
  283. }
  284. } else {
  285. throw new PHPExcel_Exception("Text rotation should be a value between -90 and 90.");
  286. }
  287. return $this;
  288. }
  289. /**
  290. * Get Wrap Text
  291. *
  292. * @return boolean
  293. */
  294. public function getWrapText()
  295. {
  296. if ($this->_isSupervisor) {
  297. return $this->getSharedComponent()->getWrapText();
  298. }
  299. return $this->_wrapText;
  300. }
  301. /**
  302. * Set Wrap Text
  303. *
  304. * @param boolean $pValue
  305. * @return PHPExcel_Style_Alignment
  306. */
  307. public function setWrapText($pValue = FALSE)
  308. {
  309. if ($pValue == '') {
  310. $pValue = FALSE;
  311. }
  312. if ($this->_isSupervisor) {
  313. $styleArray = $this->getStyleArray(array('wrap' => $pValue));
  314. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  315. } else {
  316. $this->_wrapText = $pValue;
  317. }
  318. return $this;
  319. }
  320. /**
  321. * Get Shrink to fit
  322. *
  323. * @return boolean
  324. */
  325. public function getShrinkToFit()
  326. {
  327. if ($this->_isSupervisor) {
  328. return $this->getSharedComponent()->getShrinkToFit();
  329. }
  330. return $this->_shrinkToFit;
  331. }
  332. /**
  333. * Set Shrink to fit
  334. *
  335. * @param boolean $pValue
  336. * @return PHPExcel_Style_Alignment
  337. */
  338. public function setShrinkToFit($pValue = FALSE)
  339. {
  340. if ($pValue == '') {
  341. $pValue = FALSE;
  342. }
  343. if ($this->_isSupervisor) {
  344. $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
  345. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  346. } else {
  347. $this->_shrinkToFit = $pValue;
  348. }
  349. return $this;
  350. }
  351. /**
  352. * Get indent
  353. *
  354. * @return int
  355. */
  356. public function getIndent()
  357. {
  358. if ($this->_isSupervisor) {
  359. return $this->getSharedComponent()->getIndent();
  360. }
  361. return $this->_indent;
  362. }
  363. /**
  364. * Set indent
  365. *
  366. * @param int $pValue
  367. * @return PHPExcel_Style_Alignment
  368. */
  369. public function setIndent($pValue = 0)
  370. {
  371. if ($pValue > 0) {
  372. if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
  373. $this->getHorizontal() != self::HORIZONTAL_LEFT &&
  374. $this->getHorizontal() != self::HORIZONTAL_RIGHT
  375. ) {
  376. $pValue = 0; // indent not supported
  377. }
  378. }
  379. if ($this->_isSupervisor) {
  380. $styleArray = $this->getStyleArray(array('indent' => $pValue));
  381. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  382. } else {
  383. $this->_indent = $pValue;
  384. }
  385. return $this;
  386. }
  387. /**
  388. * Get read order
  389. *
  390. * @return integer
  391. */
  392. public function getReadorder()
  393. {
  394. if ($this->_isSupervisor) {
  395. return $this->getSharedComponent()->getReadorder();
  396. }
  397. return $this->_readorder;
  398. }
  399. /**
  400. * Set read order
  401. *
  402. * @param int $pValue
  403. * @return PHPExcel_Style_Alignment
  404. */
  405. public function setReadorder($pValue = 0)
  406. {
  407. if ($pValue < 0 || $pValue > 2) {
  408. $pValue = 0;
  409. }
  410. if ($this->_isSupervisor) {
  411. $styleArray = $this->getStyleArray(array('readorder' => $pValue));
  412. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  413. } else {
  414. $this->_readorder = $pValue;
  415. }
  416. return $this;
  417. }
  418. /**
  419. * Get hash code
  420. *
  421. * @return string Hash code
  422. */
  423. public function getHashCode()
  424. {
  425. if ($this->_isSupervisor) {
  426. return $this->getSharedComponent()->getHashCode();
  427. }
  428. return md5(
  429. $this->_horizontal
  430. . $this->_vertical
  431. . $this->_textRotation
  432. . ($this->_wrapText ? 't' : 'f')
  433. . ($this->_shrinkToFit ? 't' : 'f')
  434. . $this->_indent
  435. . $this->_readorder
  436. . __CLASS__
  437. );
  438. }
  439. }