5
* Copyright (c) 2006 - 2009 PHPExcel
5
* Copyright (c) 2006 - 2010 PHPExcel
7
7
* This library is free software; you can redistribute it and/or
8
8
* modify it under the terms of the GNU Lesser General Public
9
9
* License as published by the Free Software Foundation; either
10
10
* version 2.1 of the License, or (at your option) any later version.
12
12
* This library is distributed in the hope that it will be useful,
13
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
15
* Lesser General Public License for more details.
17
17
* You should have received a copy of the GNU Lesser General Public
18
18
* License along with this library; if not, write to the Free Software
19
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
21
* @category PHPExcel
22
22
* @package PHPExcel_Style
23
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
23
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
24
24
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25
* @version ##VERSION##, ##DATE##
25
* @version 1.7.4, 2010-08-26
29
/** PHPExcel_IComparable */
30
require_once 'PHPExcel/IComparable.php';
34
30
* PHPExcel_Style_Alignment
36
32
* @category PHPExcel
37
33
* @package PHPExcel_Style
38
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
34
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
40
36
class PHPExcel_Style_Alignment implements PHPExcel_IComparable
42
38
/* Horizontal alignment styles */
43
39
const HORIZONTAL_GENERAL = 'general';
44
40
const HORIZONTAL_LEFT = 'left';
45
41
const HORIZONTAL_RIGHT = 'right';
46
42
const HORIZONTAL_CENTER = 'center';
43
const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
47
44
const HORIZONTAL_JUSTIFY = 'justify';
49
46
/* Vertical alignment styles */
50
47
const VERTICAL_BOTTOM = 'bottom';
51
48
const VERTICAL_TOP = 'top';
52
49
const VERTICAL_CENTER = 'center';
53
50
const VERTICAL_JUSTIFY = 'justify';
60
57
private $_horizontal;
67
64
private $_vertical;
74
71
private $_textRotation;
81
78
private $_wrapText;
88
85
private $_shrinkToFit;
91
88
* Indent - only possible with horizontal alignment left and right
100
* @var PHPExcel_Style
108
97
* @var _parentPropertyName string
110
99
private $_parentPropertyName;
106
private $_isSupervisor;
109
* Parent. Only used for supervisor
111
* @var PHPExcel_Style
113
116
* Create a new PHPExcel_Style_Alignment
115
public function __construct()
118
public function __construct($isSupervisor = false)
121
$this->_isSupervisor = $isSupervisor;
117
123
// Initialise values
118
124
$this->_horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
119
125
$this->_vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
127
* Property Prepare bind
133
* Bind parent. Only used for supervisor
129
* Configures this object for late binding as a property of a parent object
132
* @param $parentPropertyName
135
* @param PHPExcel $parent
136
* @return PHPExcel_Style_Alignment
134
public function propertyPrepareBind($parent, $parentPropertyName)
138
public function bindParent($parent)
136
// Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
137
// is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
138
$this->_parent = $parent;
139
$this->_parentPropertyName = $parentPropertyName;
145
* Returns the PHPExcel_Style_Alignment that is actual bound to PHPExcel_Style
147
* @return PHPExcel_Style_Alignment
149
private function propertyGetBound() {
150
if(!isset($this->_parent))
151
return $this; // I am bound
153
if($this->_parent->propertyIsBound($this->_parentPropertyName))
154
return $this->_parent->getAlignment(); // Another one is bound
156
return $this; // No one is bound yet
160
* Property Begin Bind
162
* If no PHPExcel_Style_Alignment has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
164
* @return PHPExcel_Style_Alignment
166
private function propertyBeginBind() {
167
if(!isset($this->_parent))
168
return $this; // I am already bound
170
if($this->_parent->propertyIsBound($this->_parentPropertyName))
171
return $this->_parent->getAlignment(); // Another one is already bound
173
$this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
174
$this->_parent = null;
140
$this->_parent = $parent;
145
* Is this a supervisor or a real style component?
149
public function getIsSupervisor()
151
return $this->_isSupervisor;
155
* Get the shared style component for the currently active cell in currently active sheet.
156
* Only used for style supervisor
158
* @return PHPExcel_Style_Alignment
160
public function getSharedComponent()
162
return $this->_parent->getSharedComponent()->getAlignment();
166
* Get the currently active sheet. Only used for supervisor
168
* @return PHPExcel_Worksheet
170
public function getActiveSheet()
172
return $this->_parent->getActiveSheet();
176
* Get the currently active cell coordinate in currently active sheet.
177
* Only used for supervisor
179
* @return string E.g. 'A1'
181
public function getSelectedCells()
183
return $this->getActiveSheet()->getSelectedCells();
187
* Get the currently active cell coordinate in currently active sheet.
188
* Only used for supervisor
190
* @return string E.g. 'A1'
192
public function getActiveCell()
194
return $this->getActiveSheet()->getActiveCell();
198
* Build style array from subcomponents
200
* @param array $array
203
public function getStyleArray($array)
205
return array('alignment' => $array);
180
209
* Apply styles from array
183
212
* $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
193
222
* @param array $pStyles Array containing style information
194
223
* @throws Exception
224
* @return PHPExcel_Style_Alignment
196
public function applyFromArray($pStyles = null) {
197
if (is_array($pStyles)) {
198
if (array_key_exists('horizontal', $pStyles)) {
199
$this->setHorizontal($pStyles['horizontal']);
201
if (array_key_exists('vertical', $pStyles)) {
202
$this->setVertical($pStyles['vertical']);
204
if (array_key_exists('rotation', $pStyles)) {
205
$this->setTextRotation($pStyles['rotation']);
207
if (array_key_exists('wrap', $pStyles)) {
208
$this->setWrapText($pStyles['wrap']);
210
if (array_key_exists('shrinkToFit', $pStyles)) {
211
$this->setShrinkToFit($pStyles['shrinkToFit']);
213
if (array_key_exists('indent', $pStyles)) {
214
$this->setIndent($pStyles['indent']);
217
throw new Exception("Invalid style array passed.");
226
public function applyFromArray($pStyles = null) {
227
if (is_array($pStyles)) {
228
if ($this->_isSupervisor) {
229
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
231
if (array_key_exists('horizontal', $pStyles)) {
232
$this->setHorizontal($pStyles['horizontal']);
234
if (array_key_exists('vertical', $pStyles)) {
235
$this->setVertical($pStyles['vertical']);
237
if (array_key_exists('rotation', $pStyles)) {
238
$this->setTextRotation($pStyles['rotation']);
240
if (array_key_exists('wrap', $pStyles)) {
241
$this->setWrapText($pStyles['wrap']);
243
if (array_key_exists('shrinkToFit', $pStyles)) {
244
$this->setShrinkToFit($pStyles['shrinkToFit']);
246
if (array_key_exists('indent', $pStyles)) {
247
$this->setIndent($pStyles['indent']);
251
throw new Exception("Invalid style array passed.");
226
261
public function getHorizontal() {
227
return $this->propertyGetBound()->_horizontal;
262
if ($this->_isSupervisor) {
263
return $this->getSharedComponent()->getHorizontal();
265
return $this->_horizontal;
233
271
* @param string $pValue
272
* @return PHPExcel_Style_Alignment
235
274
public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
236
275
if ($pValue == '') {
237
276
$pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
239
$this->propertyBeginBind()->_horizontal = $pValue;
279
if ($this->_isSupervisor) {
280
$styleArray = $this->getStyleArray(array('horizontal' => $pValue));
281
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
284
$this->_horizontal = $pValue;
247
294
public function getVertical() {
248
return $this->propertyGetBound()->_vertical;
295
if ($this->_isSupervisor) {
296
return $this->getSharedComponent()->getVertical();
298
return $this->_vertical;
254
304
* @param string $pValue
305
* @return PHPExcel_Style_Alignment
256
307
public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
257
308
if ($pValue == '') {
258
309
$pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
260
$this->propertyBeginBind()->_vertical = $pValue;
312
if ($this->_isSupervisor) {
313
$styleArray = $this->getStyleArray(array('vertical' => $pValue));
314
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
316
$this->_vertical = $pValue;
264
322
* Get TextRotation
268
326
public function getTextRotation() {
269
return $this->propertyGetBound()->_textRotation;
327
if ($this->_isSupervisor) {
328
return $this->getSharedComponent()->getTextRotation();
330
return $this->_textRotation;
273
334
* Set TextRotation
275
336
* @param int $pValue
276
337
* @throws Exception
338
* @return PHPExcel_Style_Alignment
278
340
public function setTextRotation($pValue = 0) {
279
// Excel2007 value 255 => PHPExcel value -165
341
// Excel2007 value 255 => PHPExcel value -165
280
342
if ($pValue == 255) {
285
347
if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
286
$this->propertyBeginBind()->_textRotation = $pValue;
348
if ($this->_isSupervisor) {
349
$styleArray = $this->getStyleArray(array('rotation' => $pValue));
350
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
352
$this->_textRotation = $pValue;
288
355
throw new Exception("Text rotation should be a value between -90 and 90.");
295
364
* @return boolean
297
366
public function getWrapText() {
298
return $this->propertyGetBound()->_wrapText;
367
if ($this->_isSupervisor) {
368
return $this->getSharedComponent()->getWrapText();
370
return $this->_wrapText;
304
376
* @param boolean $pValue
377
* @return PHPExcel_Style_Alignment
306
379
public function setWrapText($pValue = false) {
307
380
if ($pValue == '') {
310
$this->propertyBeginBind()->_wrapText = $pValue;
383
if ($this->_isSupervisor) {
384
$styleArray = $this->getStyleArray(array('wrap' => $pValue));
385
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
387
$this->_wrapText = $pValue;
314
393
* Get Shrink to fit
316
395
* @return boolean
318
397
public function getShrinkToFit() {
319
return $this->propertyGetBound()->_shrinkToFit;
398
if ($this->_isSupervisor) {
399
return $this->getSharedComponent()->getShrinkToFit();
401
return $this->_shrinkToFit;
323
405
* Set Shrink to fit
325
407
* @param boolean $pValue
408
* @return PHPExcel_Style_Alignment
327
410
public function setShrinkToFit($pValue = false) {
328
411
if ($pValue == '') {
331
$this->propertyBeginBind()->_shrinkToFit = $pValue;
414
if ($this->_isSupervisor) {
415
$styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
416
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
418
$this->_shrinkToFit = $pValue;
351
444
$pValue = 0; // indent not supported
355
$this->propertyBeginBind()->_indent = $pValue;
447
if ($this->_isSupervisor) {
448
$styleArray = $this->getStyleArray(array('indent' => $pValue));
449
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
451
$this->_indent = $pValue;
361
459
* @return string Hash code
363
461
public function getHashCode() {
364
$property = $this->propertyGetBound();
462
if ($this->_isSupervisor) {
463
return $this->getSharedComponent()->getHashCode();
366
$property->_horizontal
367
. $property->_vertical
368
. $property->_textRotation
369
. ($property->_wrapText ? 't' : 'f')
370
. ($property->_shrinkToFit ? 't' : 'f')
468
. $this->_textRotation
469
. ($this->_wrapText ? 't' : 'f')
470
. ($this->_shrinkToFit ? 't' : 'f')
386
* Note that this index may vary during script execution! Only reliable moment is
387
* while doing a write of a workbook and when changes are not allowed.
389
* @return string Hash index
391
public function getHashIndex() {
392
return $this->_hashIndex;
398
* Note that this index may vary during script execution! Only reliable moment is
399
* while doing a write of a workbook and when changes are not allowed.
401
* @param string $value Hash index
403
public function setHashIndex($value) {
404
$this->_hashIndex = $value;
408
477
* Implement PHP __clone to create a deep clone, not just a shallow copy.
410
479
public function __clone() {
411
480
$vars = get_object_vars($this);
412
481
foreach ($vars as $key => $value) {
413
if (is_object($value)) {
482
if ((is_object($value)) && ($key != '_parent')) {
414
483
$this->$key = clone $value;
416
485
$this->$key = $value;