Alignment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. * <code>
  111. * $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  112. * array(
  113. * 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  114. * 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  115. * 'textRotation' => 0,
  116. * 'wrapText' => TRUE
  117. * )
  118. * );
  119. * </code>.
  120. *
  121. * @param array $pStyles Array containing style information
  122. *
  123. * @throws PhpSpreadsheetException
  124. *
  125. * @return Alignment
  126. */
  127. public function applyFromArray(array $pStyles)
  128. {
  129. if ($this->isSupervisor) {
  130. $this->getActiveSheet()->getStyle($this->getSelectedCells())
  131. ->applyFromArray($this->getStyleArray($pStyles));
  132. } else {
  133. if (isset($pStyles['horizontal'])) {
  134. $this->setHorizontal($pStyles['horizontal']);
  135. }
  136. if (isset($pStyles['vertical'])) {
  137. $this->setVertical($pStyles['vertical']);
  138. }
  139. if (isset($pStyles['textRotation'])) {
  140. $this->setTextRotation($pStyles['textRotation']);
  141. }
  142. if (isset($pStyles['wrapText'])) {
  143. $this->setWrapText($pStyles['wrapText']);
  144. }
  145. if (isset($pStyles['shrinkToFit'])) {
  146. $this->setShrinkToFit($pStyles['shrinkToFit']);
  147. }
  148. if (isset($pStyles['indent'])) {
  149. $this->setIndent($pStyles['indent']);
  150. }
  151. if (isset($pStyles['readOrder'])) {
  152. $this->setReadOrder($pStyles['readOrder']);
  153. }
  154. }
  155. return $this;
  156. }
  157. /**
  158. * Get Horizontal.
  159. *
  160. * @return string
  161. */
  162. public function getHorizontal()
  163. {
  164. if ($this->isSupervisor) {
  165. return $this->getSharedComponent()->getHorizontal();
  166. }
  167. return $this->horizontal;
  168. }
  169. /**
  170. * Set Horizontal.
  171. *
  172. * @param string $pValue see self::HORIZONTAL_*
  173. *
  174. * @return Alignment
  175. */
  176. public function setHorizontal($pValue)
  177. {
  178. if ($pValue == '') {
  179. $pValue = self::HORIZONTAL_GENERAL;
  180. }
  181. if ($this->isSupervisor) {
  182. $styleArray = $this->getStyleArray(['horizontal' => $pValue]);
  183. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  184. } else {
  185. $this->horizontal = $pValue;
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Get Vertical.
  191. *
  192. * @return string
  193. */
  194. public function getVertical()
  195. {
  196. if ($this->isSupervisor) {
  197. return $this->getSharedComponent()->getVertical();
  198. }
  199. return $this->vertical;
  200. }
  201. /**
  202. * Set Vertical.
  203. *
  204. * @param string $pValue see self::VERTICAL_*
  205. *
  206. * @return Alignment
  207. */
  208. public function setVertical($pValue)
  209. {
  210. if ($pValue == '') {
  211. $pValue = self::VERTICAL_BOTTOM;
  212. }
  213. if ($this->isSupervisor) {
  214. $styleArray = $this->getStyleArray(['vertical' => $pValue]);
  215. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  216. } else {
  217. $this->vertical = $pValue;
  218. }
  219. return $this;
  220. }
  221. /**
  222. * Get TextRotation.
  223. *
  224. * @return int
  225. */
  226. public function getTextRotation()
  227. {
  228. if ($this->isSupervisor) {
  229. return $this->getSharedComponent()->getTextRotation();
  230. }
  231. return $this->textRotation;
  232. }
  233. /**
  234. * Set TextRotation.
  235. *
  236. * @param int $pValue
  237. *
  238. * @throws PhpSpreadsheetException
  239. *
  240. * @return Alignment
  241. */
  242. public function setTextRotation($pValue)
  243. {
  244. // Excel2007 value 255 => PhpSpreadsheet value -165
  245. if ($pValue == 255) {
  246. $pValue = -165;
  247. }
  248. // Set rotation
  249. if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
  250. if ($this->isSupervisor) {
  251. $styleArray = $this->getStyleArray(['textRotation' => $pValue]);
  252. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  253. } else {
  254. $this->textRotation = $pValue;
  255. }
  256. } else {
  257. throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
  258. }
  259. return $this;
  260. }
  261. /**
  262. * Get Wrap Text.
  263. *
  264. * @return bool
  265. */
  266. public function getWrapText()
  267. {
  268. if ($this->isSupervisor) {
  269. return $this->getSharedComponent()->getWrapText();
  270. }
  271. return $this->wrapText;
  272. }
  273. /**
  274. * Set Wrap Text.
  275. *
  276. * @param bool $pValue
  277. *
  278. * @return Alignment
  279. */
  280. public function setWrapText($pValue)
  281. {
  282. if ($pValue == '') {
  283. $pValue = false;
  284. }
  285. if ($this->isSupervisor) {
  286. $styleArray = $this->getStyleArray(['wrapText' => $pValue]);
  287. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  288. } else {
  289. $this->wrapText = $pValue;
  290. }
  291. return $this;
  292. }
  293. /**
  294. * Get Shrink to fit.
  295. *
  296. * @return bool
  297. */
  298. public function getShrinkToFit()
  299. {
  300. if ($this->isSupervisor) {
  301. return $this->getSharedComponent()->getShrinkToFit();
  302. }
  303. return $this->shrinkToFit;
  304. }
  305. /**
  306. * Set Shrink to fit.
  307. *
  308. * @param bool $pValue
  309. *
  310. * @return Alignment
  311. */
  312. public function setShrinkToFit($pValue)
  313. {
  314. if ($pValue == '') {
  315. $pValue = false;
  316. }
  317. if ($this->isSupervisor) {
  318. $styleArray = $this->getStyleArray(['shrinkToFit' => $pValue]);
  319. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  320. } else {
  321. $this->shrinkToFit = $pValue;
  322. }
  323. return $this;
  324. }
  325. /**
  326. * Get indent.
  327. *
  328. * @return int
  329. */
  330. public function getIndent()
  331. {
  332. if ($this->isSupervisor) {
  333. return $this->getSharedComponent()->getIndent();
  334. }
  335. return $this->indent;
  336. }
  337. /**
  338. * Set indent.
  339. *
  340. * @param int $pValue
  341. *
  342. * @return Alignment
  343. */
  344. public function setIndent($pValue)
  345. {
  346. if ($pValue > 0) {
  347. if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
  348. $this->getHorizontal() != self::HORIZONTAL_LEFT &&
  349. $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
  350. $pValue = 0; // indent not supported
  351. }
  352. }
  353. if ($this->isSupervisor) {
  354. $styleArray = $this->getStyleArray(['indent' => $pValue]);
  355. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  356. } else {
  357. $this->indent = $pValue;
  358. }
  359. return $this;
  360. }
  361. /**
  362. * Get read order.
  363. *
  364. * @return int
  365. */
  366. public function getReadOrder()
  367. {
  368. if ($this->isSupervisor) {
  369. return $this->getSharedComponent()->getReadOrder();
  370. }
  371. return $this->readOrder;
  372. }
  373. /**
  374. * Set read order.
  375. *
  376. * @param int $pValue
  377. *
  378. * @return Alignment
  379. */
  380. public function setReadOrder($pValue)
  381. {
  382. if ($pValue < 0 || $pValue > 2) {
  383. $pValue = 0;
  384. }
  385. if ($this->isSupervisor) {
  386. $styleArray = $this->getStyleArray(['readOrder' => $pValue]);
  387. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  388. } else {
  389. $this->readOrder = $pValue;
  390. }
  391. return $this;
  392. }
  393. /**
  394. * Get hash code.
  395. *
  396. * @return string Hash code
  397. */
  398. public function getHashCode()
  399. {
  400. if ($this->isSupervisor) {
  401. return $this->getSharedComponent()->getHashCode();
  402. }
  403. return md5(
  404. $this->horizontal .
  405. $this->vertical .
  406. $this->textRotation .
  407. ($this->wrapText ? 't' : 'f') .
  408. ($this->shrinkToFit ? 't' : 'f') .
  409. $this->indent .
  410. $this->readOrder .
  411. __CLASS__
  412. );
  413. }
  414. }