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

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

#
PHP | 420 lines | 165 code | 43 blank | 212 comment | 36 complexity | 0b086fa6174cafef5e499111268aaf14 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 - 2009 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 - 2009 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 - 2009 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. * Shrink to fit
  75. *
  76. * @var boolean
  77. */
  78. private $_shrinkToFit;
  79. /**
  80. * Indent - only possible with horizontal alignment left and right
  81. *
  82. * @var int
  83. */
  84. private $_indent;
  85. /**
  86. * Parent Style
  87. *
  88. * @var PHPExcel_Style
  89. */
  90. private $_parent;
  91. /**
  92. * Parent Borders
  93. *
  94. * @var _parentPropertyName string
  95. */
  96. private $_parentPropertyName;
  97. /**
  98. * Create a new PHPExcel_Style_Alignment
  99. */
  100. public function __construct()
  101. {
  102. // Initialise values
  103. $this->_horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  104. $this->_vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  105. $this->_textRotation = 0;
  106. $this->_wrapText = false;
  107. $this->_shrinkToFit = false;
  108. $this->_indent = 0;
  109. }
  110. /**
  111. * Property Prepare bind
  112. *
  113. * Configures this object for late binding as a property of a parent object
  114. *
  115. * @param $parent
  116. * @param $parentPropertyName
  117. */
  118. public function propertyPrepareBind($parent, $parentPropertyName)
  119. {
  120. // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  121. // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  122. $this->_parent = $parent;
  123. $this->_parentPropertyName = $parentPropertyName;
  124. }
  125. /**
  126. * Property Get Bound
  127. *
  128. * Returns the PHPExcel_Style_Alignment that is actual bound to PHPExcel_Style
  129. *
  130. * @return PHPExcel_Style_Alignment
  131. */
  132. private function propertyGetBound() {
  133. if(!isset($this->_parent))
  134. return $this; // I am bound
  135. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  136. return $this->_parent->getAlignment(); // Another one is bound
  137. return $this; // No one is bound yet
  138. }
  139. /**
  140. * Property Begin Bind
  141. *
  142. * If no PHPExcel_Style_Alignment has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  143. *
  144. * @return PHPExcel_Style_Alignment
  145. */
  146. private function propertyBeginBind() {
  147. if(!isset($this->_parent))
  148. return $this; // I am already bound
  149. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  150. return $this->_parent->getAlignment(); // Another one is already bound
  151. $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
  152. $this->_parent = null;
  153. return $this;
  154. }
  155. /**
  156. * Apply styles from array
  157. *
  158. * <code>
  159. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  160. * array(
  161. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  162. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  163. * 'rotation' => 0,
  164. * 'wrap' => true
  165. * )
  166. * );
  167. * </code>
  168. *
  169. * @param array $pStyles Array containing style information
  170. * @throws Exception
  171. */
  172. public function applyFromArray($pStyles = null) {
  173. if (is_array($pStyles)) {
  174. if (array_key_exists('horizontal', $pStyles)) {
  175. $this->setHorizontal($pStyles['horizontal']);
  176. }
  177. if (array_key_exists('vertical', $pStyles)) {
  178. $this->setVertical($pStyles['vertical']);
  179. }
  180. if (array_key_exists('rotation', $pStyles)) {
  181. $this->setTextRotation($pStyles['rotation']);
  182. }
  183. if (array_key_exists('wrap', $pStyles)) {
  184. $this->setWrapText($pStyles['wrap']);
  185. }
  186. if (array_key_exists('shrinkToFit', $pStyles)) {
  187. $this->setShrinkToFit($pStyles['shrinkToFit']);
  188. }
  189. if (array_key_exists('indent', $pStyles)) {
  190. $this->setIndent($pStyles['indent']);
  191. }
  192. } else {
  193. throw new Exception("Invalid style array passed.");
  194. }
  195. }
  196. /**
  197. * Get Horizontal
  198. *
  199. * @return string
  200. */
  201. public function getHorizontal() {
  202. return $this->propertyGetBound()->_horizontal;
  203. }
  204. /**
  205. * Set Horizontal
  206. *
  207. * @param string $pValue
  208. */
  209. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
  210. if ($pValue == '') {
  211. $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  212. }
  213. $this->propertyBeginBind()->_horizontal = $pValue;
  214. }
  215. /**
  216. * Get Vertical
  217. *
  218. * @return string
  219. */
  220. public function getVertical() {
  221. return $this->propertyGetBound()->_vertical;
  222. }
  223. /**
  224. * Set Vertical
  225. *
  226. * @param string $pValue
  227. */
  228. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
  229. if ($pValue == '') {
  230. $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  231. }
  232. $this->propertyBeginBind()->_vertical = $pValue;
  233. }
  234. /**
  235. * Get TextRotation
  236. *
  237. * @return int
  238. */
  239. public function getTextRotation() {
  240. return $this->propertyGetBound()->_textRotation;
  241. }
  242. /**
  243. * Set TextRotation
  244. *
  245. * @param int $pValue
  246. * @throws Exception
  247. */
  248. public function setTextRotation($pValue = 0) {
  249. // Excel2007 value 255 => PHPExcel value -165
  250. if ($pValue == 255) {
  251. $pValue = -165;
  252. }
  253. // Set rotation
  254. if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
  255. $this->propertyBeginBind()->_textRotation = $pValue;
  256. } else {
  257. throw new Exception("Text rotation should be a value between -90 and 90.");
  258. }
  259. }
  260. /**
  261. * Get Wrap Text
  262. *
  263. * @return boolean
  264. */
  265. public function getWrapText() {
  266. return $this->propertyGetBound()->_wrapText;
  267. }
  268. /**
  269. * Set Wrap Text
  270. *
  271. * @param boolean $pValue
  272. */
  273. public function setWrapText($pValue = false) {
  274. if ($pValue == '') {
  275. $pValue = false;
  276. }
  277. $this->propertyBeginBind()->_wrapText = $pValue;
  278. }
  279. /**
  280. * Get Shrink to fit
  281. *
  282. * @return boolean
  283. */
  284. public function getShrinkToFit() {
  285. return $this->propertyGetBound()->_shrinkToFit;
  286. }
  287. /**
  288. * Set Shrink to fit
  289. *
  290. * @param boolean $pValue
  291. */
  292. public function setShrinkToFit($pValue = false) {
  293. if ($pValue == '') {
  294. $pValue = false;
  295. }
  296. $this->propertyBeginBind()->_shrinkToFit = $pValue;
  297. }
  298. /**
  299. * Get indent
  300. *
  301. * @return int
  302. */
  303. public function getIndent() {
  304. return $this->propertyGetBound()->_indent;
  305. }
  306. /**
  307. * Set indent
  308. *
  309. * @param int $pValue
  310. */
  311. public function setIndent($pValue = 0) {
  312. if ($pValue > 0) {
  313. if ($this->getHorizontal() != self::HORIZONTAL_GENERAL && $this->getHorizontal() != self::HORIZONTAL_LEFT && $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
  314. $pValue = 0; // indent not supported
  315. }
  316. }
  317. $this->propertyBeginBind()->_indent = $pValue;
  318. }
  319. /**
  320. * Get hash code
  321. *
  322. * @return string Hash code
  323. */
  324. public function getHashCode() {
  325. $property = $this->propertyGetBound();
  326. return md5(
  327. $property->_horizontal
  328. . $property->_vertical
  329. . $property->_textRotation
  330. . ($property->_wrapText ? 't' : 'f')
  331. . ($property->_shrinkToFit ? 't' : 'f')
  332. . $property->_indent
  333. . __CLASS__
  334. );
  335. }
  336. /**
  337. * Hash index
  338. *
  339. * @var string
  340. */
  341. private $_hashIndex;
  342. /**
  343. * Get hash index
  344. *
  345. * Note that this index may vary during script execution! Only reliable moment is
  346. * while doing a write of a workbook and when changes are not allowed.
  347. *
  348. * @return string Hash index
  349. */
  350. public function getHashIndex() {
  351. return $this->_hashIndex;
  352. }
  353. /**
  354. * Set hash index
  355. *
  356. * Note that this index may vary during script execution! Only reliable moment is
  357. * while doing a write of a workbook and when changes are not allowed.
  358. *
  359. * @param string $value Hash index
  360. */
  361. public function setHashIndex($value) {
  362. $this->_hashIndex = $value;
  363. }
  364. /**
  365. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  366. */
  367. public function __clone() {
  368. $vars = get_object_vars($this);
  369. foreach ($vars as $key => $value) {
  370. if (is_object($value)) {
  371. $this->$key = clone $value;
  372. } else {
  373. $this->$key = $value;
  374. }
  375. }
  376. }
  377. }