PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Style/Alignment.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 494 lines | 229 code | 40 blank | 225 comment | 56 complexity | 4bd1c556a61de33c4b19f3840c8dc649 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_Style_Alignment
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Alignment 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. /* Vertical alignment styles */
  44. const VERTICAL_BOTTOM = 'bottom';
  45. const VERTICAL_TOP = 'top';
  46. const VERTICAL_CENTER = 'center';
  47. const VERTICAL_JUSTIFY = 'justify';
  48. /**
  49. * Horizontal
  50. *
  51. * @var string
  52. */
  53. private $_horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  54. /**
  55. * Vertical
  56. *
  57. * @var string
  58. */
  59. private $_vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  60. /**
  61. * Text rotation
  62. *
  63. * @var int
  64. */
  65. private $_textRotation = 0;
  66. /**
  67. * Wrap text
  68. *
  69. * @var boolean
  70. */
  71. private $_wrapText = false;
  72. /**
  73. * Shrink to fit
  74. *
  75. * @var boolean
  76. */
  77. private $_shrinkToFit = false;
  78. /**
  79. * Indent - only possible with horizontal alignment left and right
  80. *
  81. * @var int
  82. */
  83. private $_indent = 0;
  84. /**
  85. * Parent Borders
  86. *
  87. * @var _parentPropertyName string
  88. */
  89. private $_parentPropertyName;
  90. /**
  91. * Supervisor?
  92. *
  93. * @var boolean
  94. */
  95. private $_isSupervisor;
  96. /**
  97. * Parent. Only used for supervisor
  98. *
  99. * @var PHPExcel_Style
  100. */
  101. private $_parent;
  102. /**
  103. * Create a new PHPExcel_Style_Alignment
  104. *
  105. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  106. * Leave this value at default unless you understand exactly what
  107. * its ramifications are
  108. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  109. * Leave this value at default unless you understand exactly what
  110. * its ramifications are
  111. */
  112. public function __construct($isSupervisor = false, $isConditional = false)
  113. {
  114. // Supervisor?
  115. $this->_isSupervisor = $isSupervisor;
  116. if ($isConditional) {
  117. $this->_horizontal = NULL;
  118. $this->_vertical = NULL;
  119. $this->_textRotation = NULL;
  120. }
  121. }
  122. /**
  123. * Bind parent. Only used for supervisor
  124. *
  125. * @param PHPExcel $parent
  126. * @return PHPExcel_Style_Alignment
  127. */
  128. public function bindParent($parent)
  129. {
  130. $this->_parent = $parent;
  131. return $this;
  132. }
  133. /**
  134. * Is this a supervisor or a real style component?
  135. *
  136. * @return boolean
  137. */
  138. public function getIsSupervisor()
  139. {
  140. return $this->_isSupervisor;
  141. }
  142. /**
  143. * Get the shared style component for the currently active cell in currently active sheet.
  144. * Only used for style supervisor
  145. *
  146. * @return PHPExcel_Style_Alignment
  147. */
  148. public function getSharedComponent()
  149. {
  150. return $this->_parent->getSharedComponent()->getAlignment();
  151. }
  152. /**
  153. * Get the currently active sheet. Only used for supervisor
  154. *
  155. * @return PHPExcel_Worksheet
  156. */
  157. public function getActiveSheet()
  158. {
  159. return $this->_parent->getActiveSheet();
  160. }
  161. /**
  162. * Get the currently active cell coordinate in currently active sheet.
  163. * Only used for supervisor
  164. *
  165. * @return string E.g. 'A1'
  166. */
  167. public function getSelectedCells()
  168. {
  169. return $this->getActiveSheet()->getSelectedCells();
  170. }
  171. /**
  172. * Get the currently active cell coordinate in currently active sheet.
  173. * Only used for supervisor
  174. *
  175. * @return string E.g. 'A1'
  176. */
  177. public function getActiveCell()
  178. {
  179. return $this->getActiveSheet()->getActiveCell();
  180. }
  181. /**
  182. * Build style array from subcomponents
  183. *
  184. * @param array $array
  185. * @return array
  186. */
  187. public function getStyleArray($array)
  188. {
  189. return array('alignment' => $array);
  190. }
  191. /**
  192. * Apply styles from array
  193. *
  194. * <code>
  195. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  196. * array(
  197. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  198. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  199. * 'rotation' => 0,
  200. * 'wrap' => true
  201. * )
  202. * );
  203. * </code>
  204. *
  205. * @param array $pStyles Array containing style information
  206. * @throws Exception
  207. * @return PHPExcel_Style_Alignment
  208. */
  209. public function applyFromArray($pStyles = null) {
  210. if (is_array($pStyles)) {
  211. if ($this->_isSupervisor) {
  212. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  213. } else {
  214. if (array_key_exists('horizontal', $pStyles)) {
  215. $this->setHorizontal($pStyles['horizontal']);
  216. }
  217. if (array_key_exists('vertical', $pStyles)) {
  218. $this->setVertical($pStyles['vertical']);
  219. }
  220. if (array_key_exists('rotation', $pStyles)) {
  221. $this->setTextRotation($pStyles['rotation']);
  222. }
  223. if (array_key_exists('wrap', $pStyles)) {
  224. $this->setWrapText($pStyles['wrap']);
  225. }
  226. if (array_key_exists('shrinkToFit', $pStyles)) {
  227. $this->setShrinkToFit($pStyles['shrinkToFit']);
  228. }
  229. if (array_key_exists('indent', $pStyles)) {
  230. $this->setIndent($pStyles['indent']);
  231. }
  232. }
  233. } else {
  234. throw new Exception("Invalid style array passed.");
  235. }
  236. return $this;
  237. }
  238. /**
  239. * Get Horizontal
  240. *
  241. * @return string
  242. */
  243. public function getHorizontal() {
  244. if ($this->_isSupervisor) {
  245. return $this->getSharedComponent()->getHorizontal();
  246. }
  247. return $this->_horizontal;
  248. }
  249. /**
  250. * Set Horizontal
  251. *
  252. * @param string $pValue
  253. * @return PHPExcel_Style_Alignment
  254. */
  255. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
  256. if ($pValue == '') {
  257. $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  258. }
  259. if ($this->_isSupervisor) {
  260. $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
  261. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  262. }
  263. else {
  264. $this->_horizontal = $pValue;
  265. }
  266. return $this;
  267. }
  268. /**
  269. * Get Vertical
  270. *
  271. * @return string
  272. */
  273. public function getVertical() {
  274. if ($this->_isSupervisor) {
  275. return $this->getSharedComponent()->getVertical();
  276. }
  277. return $this->_vertical;
  278. }
  279. /**
  280. * Set Vertical
  281. *
  282. * @param string $pValue
  283. * @return PHPExcel_Style_Alignment
  284. */
  285. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
  286. if ($pValue == '') {
  287. $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  288. }
  289. if ($this->_isSupervisor) {
  290. $styleArray = $this->getStyleArray(array('vertical' => $pValue));
  291. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  292. } else {
  293. $this->_vertical = $pValue;
  294. }
  295. return $this;
  296. }
  297. /**
  298. * Get TextRotation
  299. *
  300. * @return int
  301. */
  302. public function getTextRotation() {
  303. if ($this->_isSupervisor) {
  304. return $this->getSharedComponent()->getTextRotation();
  305. }
  306. return $this->_textRotation;
  307. }
  308. /**
  309. * Set TextRotation
  310. *
  311. * @param int $pValue
  312. * @throws Exception
  313. * @return PHPExcel_Style_Alignment
  314. */
  315. public function setTextRotation($pValue = 0) {
  316. // Excel2007 value 255 => PHPExcel value -165
  317. if ($pValue == 255) {
  318. $pValue = -165;
  319. }
  320. // Set rotation
  321. if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
  322. if ($this->_isSupervisor) {
  323. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  324. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  325. } else {
  326. $this->_textRotation = $pValue;
  327. }
  328. } else {
  329. throw new Exception("Text rotation should be a value between -90 and 90.");
  330. }
  331. return $this;
  332. }
  333. /**
  334. * Get Wrap Text
  335. *
  336. * @return boolean
  337. */
  338. public function getWrapText() {
  339. if ($this->_isSupervisor) {
  340. return $this->getSharedComponent()->getWrapText();
  341. }
  342. return $this->_wrapText;
  343. }
  344. /**
  345. * Set Wrap Text
  346. *
  347. * @param boolean $pValue
  348. * @return PHPExcel_Style_Alignment
  349. */
  350. public function setWrapText($pValue = false) {
  351. if ($pValue == '') {
  352. $pValue = false;
  353. }
  354. if ($this->_isSupervisor) {
  355. $styleArray = $this->getStyleArray(array('wrap' => $pValue));
  356. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  357. } else {
  358. $this->_wrapText = $pValue;
  359. }
  360. return $this;
  361. }
  362. /**
  363. * Get Shrink to fit
  364. *
  365. * @return boolean
  366. */
  367. public function getShrinkToFit() {
  368. if ($this->_isSupervisor) {
  369. return $this->getSharedComponent()->getShrinkToFit();
  370. }
  371. return $this->_shrinkToFit;
  372. }
  373. /**
  374. * Set Shrink to fit
  375. *
  376. * @param boolean $pValue
  377. * @return PHPExcel_Style_Alignment
  378. */
  379. public function setShrinkToFit($pValue = false) {
  380. if ($pValue == '') {
  381. $pValue = false;
  382. }
  383. if ($this->_isSupervisor) {
  384. $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
  385. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  386. } else {
  387. $this->_shrinkToFit = $pValue;
  388. }
  389. return $this;
  390. }
  391. /**
  392. * Get indent
  393. *
  394. * @return int
  395. */
  396. public function getIndent() {
  397. if ($this->_isSupervisor) {
  398. return $this->getSharedComponent()->getIndent();
  399. }
  400. return $this->_indent;
  401. }
  402. /**
  403. * Set indent
  404. *
  405. * @param int $pValue
  406. * @return PHPExcel_Style_Alignment
  407. */
  408. public function setIndent($pValue = 0) {
  409. if ($pValue > 0) {
  410. if ($this->getHorizontal() != self::HORIZONTAL_GENERAL && $this->getHorizontal() != self::HORIZONTAL_LEFT && $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
  411. $pValue = 0; // indent not supported
  412. }
  413. }
  414. if ($this->_isSupervisor) {
  415. $styleArray = $this->getStyleArray(array('indent' => $pValue));
  416. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  417. } else {
  418. $this->_indent = $pValue;
  419. }
  420. return $this;
  421. }
  422. /**
  423. * Get hash code
  424. *
  425. * @return string Hash code
  426. */
  427. public function getHashCode() {
  428. if ($this->_isSupervisor) {
  429. return $this->getSharedComponent()->getHashCode();
  430. }
  431. return md5(
  432. $this->_horizontal
  433. . $this->_vertical
  434. . $this->_textRotation
  435. . ($this->_wrapText ? 't' : 'f')
  436. . ($this->_shrinkToFit ? 't' : 'f')
  437. . $this->_indent
  438. . __CLASS__
  439. );
  440. }
  441. /**
  442. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  443. */
  444. public function __clone() {
  445. $vars = get_object_vars($this);
  446. foreach ($vars as $key => $value) {
  447. if ((is_object($value)) && ($key != '_parent')) {
  448. $this->$key = clone $value;
  449. } else {
  450. $this->$key = $value;
  451. }
  452. }
  453. }
  454. }