PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
PHP | 251 lines | 102 code | 22 blank | 127 comment | 21 complexity | 15bed5886f0ec81ae74ea8a5109ef9e7 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 - 2008 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 - 2008 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. /** 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 - 2008 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. // Excel2007 value 255 => PHPExcel value -165
  173. if ($pValue == 255) {
  174. $pValue = -165;
  175. }
  176. // Set rotation
  177. if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
  178. $this->_textRotation = $pValue;
  179. } else {
  180. throw new Exception("Text rotation should be a value between -90 and 90.");
  181. }
  182. }
  183. /**
  184. * Get Wrap Text
  185. *
  186. * @return boolean
  187. */
  188. public function getWrapText() {
  189. return $this->_wrapText;
  190. }
  191. /**
  192. * Set Wrap Text
  193. *
  194. * @param boolean $pValue
  195. */
  196. public function setWrapText($pValue = false) {
  197. if ($pValue == '') {
  198. $pValue = false;
  199. }
  200. $this->_wrapText = $pValue;
  201. }
  202. /**
  203. * Get hash code
  204. *
  205. * @return string Hash code
  206. */
  207. public function getHashCode() {
  208. return md5(
  209. $this->_horizontal
  210. . $this->_vertical
  211. . $this->_textRotation
  212. . ($this->_wrapText ? 't' : 'f')
  213. . __CLASS__
  214. );
  215. }
  216. /**
  217. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  218. */
  219. public function __clone() {
  220. $vars = get_object_vars($this);
  221. foreach ($vars as $key => $value) {
  222. if (is_object($value)) {
  223. $this->$key = clone $value;
  224. } else {
  225. $this->$key = $value;
  226. }
  227. }
  228. }
  229. }