PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 320 lines | 126 code | 33 blank | 161 comment | 25 complexity | 77d2036461af46ab2c2939c070ea91d4 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. * Parent Style
  75. *
  76. * @var PHPExcel_Style
  77. */
  78. private $_parent;
  79. /**
  80. * Parent Borders
  81. *
  82. * @var _parentPropertyName string
  83. */
  84. private $_parentPropertyName;
  85. /**
  86. * Create a new PHPExcel_Style_Alignment
  87. */
  88. public function __construct()
  89. {
  90. // Initialise values
  91. $this->_horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  92. $this->_vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  93. $this->_textRotation = 0;
  94. $this->_wrapText = false;
  95. }
  96. /**
  97. * Property Prepare bind
  98. *
  99. * Configures this object for late binding as a property of a parent object
  100. *
  101. * @param $parent
  102. * @param $parentPropertyName
  103. */
  104. public function propertyPrepareBind($parent, $parentPropertyName)
  105. {
  106. // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  107. // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  108. $this->_parent = $parent;
  109. $this->_parentPropertyName = $parentPropertyName;
  110. }
  111. /**
  112. * Property Get Bound
  113. *
  114. * Returns the PHPExcel_Style_Alignment that is actual bound to PHPExcel_Style
  115. *
  116. * @return PHPExcel_Style_Alignment
  117. */
  118. private function propertyGetBound() {
  119. if(!isset($this->_parent))
  120. return $this; // I am bound
  121. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  122. return $this->_parent->getAlignment(); // Another one is bound
  123. return $this; // No one is bound yet
  124. }
  125. /**
  126. * Property Begin Bind
  127. *
  128. * If no PHPExcel_Style_Alignment has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  129. *
  130. * @return PHPExcel_Style_Alignment
  131. */
  132. private function propertyBeginBind() {
  133. if(!isset($this->_parent))
  134. return $this; // I am already bound
  135. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  136. return $this->_parent->getAlignment(); // Another one is already bound
  137. $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
  138. $this->_parent = null;
  139. return $this;
  140. }
  141. /**
  142. * Apply styles from array
  143. *
  144. * <code>
  145. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  146. * array(
  147. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  148. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  149. * 'rotation' => 0,
  150. * 'wrap' => true
  151. * )
  152. * );
  153. * </code>
  154. *
  155. * @param array $pStyles Array containing style information
  156. * @throws Exception
  157. */
  158. public function applyFromArray($pStyles = null) {
  159. if (is_array($pStyles)) {
  160. if (array_key_exists('horizontal', $pStyles)) {
  161. $this->setHorizontal($pStyles['horizontal']);
  162. }
  163. if (array_key_exists('vertical', $pStyles)) {
  164. $this->setVertical($pStyles['vertical']);
  165. }
  166. if (array_key_exists('rotation', $pStyles)) {
  167. $this->setTextRotation($pStyles['rotation']);
  168. }
  169. if (array_key_exists('wrap', $pStyles)) {
  170. $this->setWrapText($pStyles['wrap']);
  171. }
  172. } else {
  173. throw new Exception("Invalid style array passed.");
  174. }
  175. }
  176. /**
  177. * Get Horizontal
  178. *
  179. * @return string
  180. */
  181. public function getHorizontal() {
  182. return $this->propertyGetBound()->_horizontal;
  183. }
  184. /**
  185. * Set Horizontal
  186. *
  187. * @param string $pValue
  188. */
  189. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
  190. if ($pValue == '') {
  191. $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  192. }
  193. $this->propertyBeginBind()->_horizontal = $pValue;
  194. }
  195. /**
  196. * Get Vertical
  197. *
  198. * @return string
  199. */
  200. public function getVertical() {
  201. return $this->propertyGetBound()->_vertical;
  202. }
  203. /**
  204. * Set Vertical
  205. *
  206. * @param string $pValue
  207. */
  208. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
  209. if ($pValue == '') {
  210. $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  211. }
  212. $this->propertyBeginBind()->_vertical = $pValue;
  213. }
  214. /**
  215. * Get TextRotation
  216. *
  217. * @return int
  218. */
  219. public function getTextRotation() {
  220. return $this->propertyGetBound()->_textRotation;
  221. }
  222. /**
  223. * Set TextRotation
  224. *
  225. * @param int $pValue
  226. * @throws Exception
  227. */
  228. public function setTextRotation($pValue = 0) {
  229. // Excel2007 value 255 => PHPExcel value -165
  230. if ($pValue == 255) {
  231. $pValue = -165;
  232. }
  233. // Set rotation
  234. if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
  235. $this->propertyBeginBind()->_textRotation = $pValue;
  236. } else {
  237. throw new Exception("Text rotation should be a value between -90 and 90.");
  238. }
  239. }
  240. /**
  241. * Get Wrap Text
  242. *
  243. * @return boolean
  244. */
  245. public function getWrapText() {
  246. return $this->propertyGetBound()->_wrapText;
  247. }
  248. /**
  249. * Set Wrap Text
  250. *
  251. * @param boolean $pValue
  252. */
  253. public function setWrapText($pValue = false) {
  254. if ($pValue == '') {
  255. $pValue = false;
  256. }
  257. $this->propertyBeginBind()->_wrapText = $pValue;
  258. }
  259. /**
  260. * Get hash code
  261. *
  262. * @return string Hash code
  263. */
  264. public function getHashCode() {
  265. $property = $this->propertyGetBound();
  266. return md5(
  267. $property->_horizontal
  268. . $property->_vertical
  269. . $property->_textRotation
  270. . ($property->_wrapText ? 't' : 'f')
  271. . __CLASS__
  272. );
  273. }
  274. /**
  275. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  276. */
  277. public function __clone() {
  278. $vars = get_object_vars($this);
  279. foreach ($vars as $key => $value) {
  280. if (is_object($value)) {
  281. $this->$key = clone $value;
  282. } else {
  283. $this->$key = $value;
  284. }
  285. }
  286. }
  287. }