Alignment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Style;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. class Alignment extends Supervisor
  5. {
  6. // Horizontal alignment styles
  7. const HORIZONTAL_GENERAL = 'general';
  8. const HORIZONTAL_LEFT = 'left';
  9. const HORIZONTAL_RIGHT = 'right';
  10. const HORIZONTAL_CENTER = 'center';
  11. const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
  12. const HORIZONTAL_JUSTIFY = 'justify';
  13. const HORIZONTAL_FILL = 'fill';
  14. const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only
  15. // Vertical alignment styles
  16. const VERTICAL_BOTTOM = 'bottom';
  17. const VERTICAL_TOP = 'top';
  18. const VERTICAL_CENTER = 'center';
  19. const VERTICAL_JUSTIFY = 'justify';
  20. const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
  21. // Read order
  22. const READORDER_CONTEXT = 0;
  23. const READORDER_LTR = 1;
  24. const READORDER_RTL = 2;
  25. /**
  26. * Horizontal alignment.
  27. *
  28. * @var string
  29. */
  30. protected $horizontal = self::HORIZONTAL_GENERAL;
  31. /**
  32. * Vertical alignment.
  33. *
  34. * @var string
  35. */
  36. protected $vertical = self::VERTICAL_BOTTOM;
  37. /**
  38. * Text rotation.
  39. *
  40. * @var int
  41. */
  42. protected $textRotation = 0;
  43. /**
  44. * Wrap text.
  45. *
  46. * @var bool
  47. */
  48. protected $wrapText = false;
  49. /**
  50. * Shrink to fit.
  51. *
  52. * @var bool
  53. */
  54. protected $shrinkToFit = false;
  55. /**
  56. * Indent - only possible with horizontal alignment left and right.
  57. *
  58. * @var int
  59. */
  60. protected $indent = 0;
  61. /**
  62. * Read order.
  63. *
  64. * @var int
  65. */
  66. protected $readOrder = 0;
  67. /**
  68. * Create a new Alignment.
  69. *
  70. * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  71. * Leave this value at default unless you understand exactly what
  72. * its ramifications are
  73. * @param bool $isConditional Flag indicating if this is a conditional style or not
  74. * Leave this value at default unless you understand exactly what
  75. * its ramifications are
  76. */
  77. public function __construct($isSupervisor = false, $isConditional = false)
  78. {
  79. // Supervisor?
  80. parent::__construct($isSupervisor);
  81. if ($isConditional) {
  82. $this->horizontal = null;
  83. $this->vertical = null;
  84. $this->textRotation = null;
  85. }
  86. }
  87. /**
  88. * Get the shared style component for the currently active cell in currently active sheet.
  89. * Only used for style supervisor.
  90. *
  91. * @return Alignment
  92. */
  93. public function getSharedComponent()
  94. {
  95. return $this->parent->getSharedComponent()->getAlignment();
  96. }
  97. /**
  98. * Build style array from subcomponents.
  99. *
  100. * @param array $array
  101. *
  102. * @return array
  103. */
  104. public function getStyleArray($array)
  105. {
  106. return ['alignment' => $array];
  107. }
  108. /**
  109. * Apply styles from array.
  110. *
  111. * <code>
  112. * $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  113. * [
  114. * 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  115. * 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  116. * 'textRotation' => 0,
  117. * 'wrapText' => TRUE
  118. * ]
  119. * );
  120. * </code>
  121. *
  122. * @param array $pStyles Array containing style information
  123. *
  124. * @throws PhpSpreadsheetException
  125. *
  126. * @return Alignment
  127. */
  128. public function applyFromArray(array $pStyles)
  129. {
  130. if ($this->isSupervisor) {
  131. $this->getActiveSheet()->getStyle($this->getSelectedCells())
  132. ->applyFromArray($this->getStyleArray($pStyles));
  133. } else {
  134. if (isset($pStyles['horizontal'])) {
  135. $this->setHorizontal($pStyles['horizontal']);
  136. }
  137. if (isset($pStyles['vertical'])) {
  138. $this->setVertical($pStyles['vertical']);
  139. }
  140. if (isset($pStyles['textRotation'])) {
  141. $this->setTextRotation($pStyles['textRotation']);
  142. }
  143. if (isset($pStyles['wrapText'])) {
  144. $this->setWrapText($pStyles['wrapText']);
  145. }
  146. if (isset($pStyles['shrinkToFit'])) {
  147. $this->setShrinkToFit($pStyles['shrinkToFit']);
  148. }
  149. if (isset($pStyles['indent'])) {
  150. $this->setIndent($pStyles['indent']);
  151. }
  152. if (isset($pStyles['readOrder'])) {
  153. $this->setReadOrder($pStyles['readOrder']);
  154. }
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Get Horizontal.
  160. *
  161. * @return string
  162. */
  163. public function getHorizontal()
  164. {
  165. if ($this->isSupervisor) {
  166. return $this->getSharedComponent()->getHorizontal();
  167. }
  168. return $this->horizontal;
  169. }
  170. /**
  171. * Set Horizontal.
  172. *
  173. * @param string $pValue see self::HORIZONTAL_*
  174. *
  175. * @return Alignment
  176. */
  177. public function setHorizontal($pValue)
  178. {
  179. if ($pValue == '') {
  180. $pValue = self::HORIZONTAL_GENERAL;
  181. }
  182. if ($this->isSupervisor) {
  183. $styleArray = $this->getStyleArray(['horizontal' => $pValue]);
  184. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  185. } else {
  186. $this->horizontal = $pValue;
  187. }
  188. return $this;
  189. }
  190. /**
  191. * Get Vertical.
  192. *
  193. * @return string
  194. */
  195. public function getVertical()
  196. {
  197. if ($this->isSupervisor) {
  198. return $this->getSharedComponent()->getVertical();
  199. }
  200. return $this->vertical;
  201. }
  202. /**
  203. * Set Vertical.
  204. *
  205. * @param string $pValue see self::VERTICAL_*
  206. *
  207. * @return Alignment
  208. */
  209. public function setVertical($pValue)
  210. {
  211. if ($pValue == '') {
  212. $pValue = self::VERTICAL_BOTTOM;
  213. }
  214. if ($this->isSupervisor) {
  215. $styleArray = $this->getStyleArray(['vertical' => $pValue]);
  216. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  217. } else {
  218. $this->vertical = $pValue;
  219. }
  220. return $this;
  221. }
  222. /**
  223. * Get TextRotation.
  224. *
  225. * @return int
  226. */
  227. public function getTextRotation()
  228. {
  229. if ($this->isSupervisor) {
  230. return $this->getSharedComponent()->getTextRotation();
  231. }
  232. return $this->textRotation;
  233. }
  234. /**
  235. * Set TextRotation.
  236. *
  237. * @param int $pValue
  238. *
  239. * @throws PhpSpreadsheetException
  240. *
  241. * @return Alignment
  242. */
  243. public function setTextRotation($pValue)
  244. {
  245. // Excel2007 value 255 => PhpSpreadsheet value -165
  246. if ($pValue == 255) {
  247. $pValue = -165;
  248. }
  249. // Set rotation
  250. if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
  251. if ($this->isSupervisor) {
  252. $styleArray = $this->getStyleArray(['textRotation' => $pValue]);
  253. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  254. } else {
  255. $this->textRotation = $pValue;
  256. }
  257. } else {
  258. throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
  259. }
  260. return $this;
  261. }
  262. /**
  263. * Get Wrap Text.
  264. *
  265. * @return bool
  266. */
  267. public function getWrapText()
  268. {
  269. if ($this->isSupervisor) {
  270. return $this->getSharedComponent()->getWrapText();
  271. }
  272. return $this->wrapText;
  273. }
  274. /**
  275. * Set Wrap Text.
  276. *
  277. * @param bool $pValue
  278. *
  279. * @return Alignment
  280. */
  281. public function setWrapText($pValue)
  282. {
  283. if ($pValue == '') {
  284. $pValue = false;
  285. }
  286. if ($this->isSupervisor) {
  287. $styleArray = $this->getStyleArray(['wrapText' => $pValue]);
  288. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  289. } else {
  290. $this->wrapText = $pValue;
  291. }
  292. return $this;
  293. }
  294. /**
  295. * Get Shrink to fit.
  296. *
  297. * @return bool
  298. */
  299. public function getShrinkToFit()
  300. {
  301. if ($this->isSupervisor) {
  302. return $this->getSharedComponent()->getShrinkToFit();
  303. }
  304. return $this->shrinkToFit;
  305. }
  306. /**
  307. * Set Shrink to fit.
  308. *
  309. * @param bool $pValue
  310. *
  311. * @return Alignment
  312. */
  313. public function setShrinkToFit($pValue)
  314. {
  315. if ($pValue == '') {
  316. $pValue = false;
  317. }
  318. if ($this->isSupervisor) {
  319. $styleArray = $this->getStyleArray(['shrinkToFit' => $pValue]);
  320. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  321. } else {
  322. $this->shrinkToFit = $pValue;
  323. }
  324. return $this;
  325. }
  326. /**
  327. * Get indent.
  328. *
  329. * @return int
  330. */
  331. public function getIndent()
  332. {
  333. if ($this->isSupervisor) {
  334. return $this->getSharedComponent()->getIndent();
  335. }
  336. return $this->indent;
  337. }
  338. /**
  339. * Set indent.
  340. *
  341. * @param int $pValue
  342. *
  343. * @return Alignment
  344. */
  345. public function setIndent($pValue)
  346. {
  347. if ($pValue > 0) {
  348. if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
  349. $this->getHorizontal() != self::HORIZONTAL_LEFT &&
  350. $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
  351. $pValue = 0; // indent not supported
  352. }
  353. }
  354. if ($this->isSupervisor) {
  355. $styleArray = $this->getStyleArray(['indent' => $pValue]);
  356. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  357. } else {
  358. $this->indent = $pValue;
  359. }
  360. return $this;
  361. }
  362. /**
  363. * Get read order.
  364. *
  365. * @return int
  366. */
  367. public function getReadOrder()
  368. {
  369. if ($this->isSupervisor) {
  370. return $this->getSharedComponent()->getReadOrder();
  371. }
  372. return $this->readOrder;
  373. }
  374. /**
  375. * Set read order.
  376. *
  377. * @param int $pValue
  378. *
  379. * @return Alignment
  380. */
  381. public function setReadOrder($pValue)
  382. {
  383. if ($pValue < 0 || $pValue > 2) {
  384. $pValue = 0;
  385. }
  386. if ($this->isSupervisor) {
  387. $styleArray = $this->getStyleArray(['readOrder' => $pValue]);
  388. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  389. } else {
  390. $this->readOrder = $pValue;
  391. }
  392. return $this;
  393. }
  394. /**
  395. * Get hash code.
  396. *
  397. * @return string Hash code
  398. */
  399. public function getHashCode()
  400. {
  401. if ($this->isSupervisor) {
  402. return $this->getSharedComponent()->getHashCode();
  403. }
  404. return md5(
  405. $this->horizontal .
  406. $this->vertical .
  407. $this->textRotation .
  408. ($this->wrapText ? 't' : 'f') .
  409. ($this->shrinkToFit ? 't' : 'f') .
  410. $this->indent .
  411. $this->readOrder .
  412. __CLASS__
  413. );
  414. }
  415. }