PageRenderTime 40ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.4.0/Classes/PHPExcel/Style/Alignment.php

#
PHP | 245 lines | 99 code | 21 blank | 125 comment | 17 complexity | 6537d21da9b306469ddb974702f3683f MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 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 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_IComparable */
  28. require_once 'PHPExcel/IComparable.php';
  29. /**
  30. * PHPExcel_Style_Alignment
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel_Style
  34. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Style_Alignment implements PHPExcel_IComparable
  37. {
  38. /* Horizontal alignment styles */
  39. const HORIZONTAL_GENERAL = 'general';
  40. const HORIZONTAL_LEFT = 'left';
  41. const HORIZONTAL_RIGHT = 'right';
  42. const HORIZONTAL_CENTER = 'center';
  43. const HORIZONTAL_JUSTIFY = 'justify';
  44. /* Vertical alignment styles */
  45. const VERTICAL_BOTTOM = 'bottom';
  46. const VERTICAL_TOP = 'top';
  47. const VERTICAL_CENTER = 'center';
  48. const VERTICAL_JUSTIFY = 'justify';
  49. /**
  50. * Horizontal
  51. *
  52. * @var string
  53. */
  54. private $_horizontal;
  55. /**
  56. * Vertical
  57. *
  58. * @var string
  59. */
  60. private $_vertical;
  61. /**
  62. * Text rotation
  63. *
  64. * @var int
  65. */
  66. private $_textRotation;
  67. /**
  68. * Wrap text
  69. *
  70. * @var boolean
  71. */
  72. private $_wrapText;
  73. /**
  74. * Create a new PHPExcel_Style_Alignment
  75. */
  76. public function __construct()
  77. {
  78. // Initialise values
  79. $this->_horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  80. $this->_vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  81. $this->_textRotation = 0;
  82. $this->_wrapText = false;
  83. }
  84. /**
  85. * Apply styles from array
  86. *
  87. * <code>
  88. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  89. * array(
  90. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  91. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  92. * 'rotation' => 0,
  93. * 'wrap' => true
  94. * )
  95. * );
  96. * </code>
  97. *
  98. * @param array $pStyles Array containing style information
  99. * @throws Exception
  100. */
  101. public function applyFromArray($pStyles = null) {
  102. if (is_array($pStyles)) {
  103. if (array_key_exists('horizontal', $pStyles)) {
  104. $this->setHorizontal($pStyles['horizontal']);
  105. }
  106. if (array_key_exists('vertical', $pStyles)) {
  107. $this->setVertical($pStyles['vertical']);
  108. }
  109. if (array_key_exists('rotation', $pStyles)) {
  110. $this->setTextRotation($pStyles['rotation']);
  111. }
  112. if (array_key_exists('wrap', $pStyles)) {
  113. $this->setWrapText($pStyles['wrap']);
  114. }
  115. } else {
  116. throw new Exception("Invalid style array passed.");
  117. }
  118. }
  119. /**
  120. * Get Horizontal
  121. *
  122. * @return string
  123. */
  124. public function getHorizontal() {
  125. return $this->_horizontal;
  126. }
  127. /**
  128. * Set Horizontal
  129. *
  130. * @param string $pValue
  131. */
  132. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
  133. if ($pValue == '') {
  134. $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  135. }
  136. $this->_horizontal = $pValue;
  137. }
  138. /**
  139. * Get Vertical
  140. *
  141. * @return string
  142. */
  143. public function getVertical() {
  144. return $this->_vertical;
  145. }
  146. /**
  147. * Set Vertical
  148. *
  149. * @param string $pValue
  150. */
  151. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
  152. if ($pValue == '') {
  153. $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  154. }
  155. $this->_vertical = $pValue;
  156. }
  157. /**
  158. * Get TextRotation
  159. *
  160. * @return int
  161. */
  162. public function getTextRotation() {
  163. return $this->_textRotation;
  164. }
  165. /**
  166. * Set TextRotation
  167. *
  168. * @param int $pValue
  169. * @throws Exception
  170. */
  171. public function setTextRotation($pValue = 0) {
  172. if ($pValue >= 0 && $pValue <= 180) {
  173. $this->_textRotation = $pValue;
  174. } else {
  175. throw new Exception("Text rotation should be a value between 0 and 180.");
  176. }
  177. }
  178. /**
  179. * Get Wrap Text
  180. *
  181. * @return boolean
  182. */
  183. public function getWrapText() {
  184. return $this->_wrapText;
  185. }
  186. /**
  187. * Set Wrap Text
  188. *
  189. * @param boolean $pValue
  190. */
  191. public function setWrapText($pValue = false) {
  192. if ($pValue == '') {
  193. $pValue = false;
  194. }
  195. $this->_wrapText = $pValue;
  196. }
  197. /**
  198. * Get hash code
  199. *
  200. * @return string Hash code
  201. */
  202. public function getHashCode() {
  203. return md5(
  204. $this->_horizontal
  205. . $this->_vertical
  206. . $this->_textRotation
  207. . $this->_wrapText
  208. . __CLASS__
  209. );
  210. }
  211. /**
  212. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  213. */
  214. public function __clone() {
  215. $vars = get_object_vars($this);
  216. foreach ($vars as $key => $value) {
  217. if (is_object($value)) {
  218. $this->$key = clone $value;
  219. } else {
  220. $this->$key = $value;
  221. }
  222. }
  223. }
  224. }