Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
23 / 23
CRAP
100.00% covered (success)
100.00%
1 / 1
Alignment
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
23 / 23
34
100.00% covered (success)
100.00%
1 / 1
 getHorizontal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setHorizontal
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getVertical
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setVertical
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getLevel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLevel
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getIndent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setIndent
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getMarginLeft
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMarginLeft
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getMarginRight
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMarginRight
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getMarginTop
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMarginTop
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMarginBottom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMarginBottom
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTextDirection
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTextDirection
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isRTL
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setIsRTL
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getHashCode
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getHashIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setHashIndex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Style;
21
22use PhpOffice\PhpPresentation\ComparableInterface;
23use PhpOffice\PhpPresentation\Exception\OutOfBoundsException;
24
25class Alignment implements ComparableInterface
26{
27    // Horizontal alignment
28    public const HORIZONTAL_GENERAL = 'l';
29    public const HORIZONTAL_LEFT = 'l';
30    public const HORIZONTAL_RIGHT = 'r';
31    public const HORIZONTAL_CENTER = 'ctr';
32    public const HORIZONTAL_JUSTIFY = 'just';
33    public const HORIZONTAL_DISTRIBUTED = 'dist';
34
35    // Vertical alignment
36    public const VERTICAL_BASE = 'base';
37    public const VERTICAL_AUTO = 'auto';
38    public const VERTICAL_BOTTOM = 'b';
39    public const VERTICAL_TOP = 't';
40    public const VERTICAL_CENTER = 'ctr';
41
42    // Text direction
43    public const TEXT_DIRECTION_HORIZONTAL = 'horz';
44    public const TEXT_DIRECTION_VERTICAL_90 = 'vert';
45    public const TEXT_DIRECTION_VERTICAL_270 = 'vert270';
46    public const TEXT_DIRECTION_STACKED = 'wordArtVert';
47
48    /**
49     * @var array<int, string>
50     */
51    private $supportedStyles = [
52        self::HORIZONTAL_GENERAL,
53        self::HORIZONTAL_LEFT,
54        self::HORIZONTAL_RIGHT,
55    ];
56
57    /**
58     * Horizontal.
59     *
60     * @var string
61     */
62    private $horizontal = self::HORIZONTAL_LEFT;
63
64    /**
65     * Vertical.
66     *
67     * @var string
68     */
69    private $vertical = self::VERTICAL_BASE;
70
71    /**
72     * Text Direction.
73     *
74     * @var string
75     */
76    private $textDirection = self::TEXT_DIRECTION_HORIZONTAL;
77
78    /**
79     * Level.
80     *
81     * @var int
82     */
83    private $level = 0;
84
85    /**
86     * Indent - only possible with horizontal alignment left and right.
87     *
88     * @var float
89     */
90    private $indent = 0;
91
92    /**
93     * Margin left - only possible with horizontal alignment left and right.
94     *
95     * @var float
96     */
97    private $marginLeft = 0;
98
99    /**
100     * Margin right - only possible with horizontal alignment left and right.
101     *
102     * @var float
103     */
104    private $marginRight = 0;
105
106    /**
107     * Margin top.
108     *
109     * @var float
110     */
111    private $marginTop = 0;
112
113    /**
114     * Margin bottom.
115     *
116     * @var float
117     */
118    private $marginBottom = 0;
119
120    /**
121     * RTL Direction Support.
122     *
123     * @var bool
124     */
125    private $isRTL = false;
126
127    /**
128     * Hash index.
129     *
130     * @var int
131     */
132    private $hashIndex;
133
134    /**
135     * Get Horizontal.
136     */
137    public function getHorizontal(): string
138    {
139        return $this->horizontal;
140    }
141
142    /**
143     * Set Horizontal.
144     */
145    public function setHorizontal(string $pValue = self::HORIZONTAL_LEFT): self
146    {
147        if ('' == $pValue) {
148            $pValue = self::HORIZONTAL_LEFT;
149        }
150        $this->horizontal = $pValue;
151
152        return $this;
153    }
154
155    /**
156     * Get Vertical.
157     */
158    public function getVertical(): string
159    {
160        return $this->vertical;
161    }
162
163    /**
164     * Set Vertical.
165     */
166    public function setVertical(string $pValue = self::VERTICAL_BASE): self
167    {
168        if ('' == $pValue) {
169            $pValue = self::VERTICAL_BASE;
170        }
171        $this->vertical = $pValue;
172
173        return $this;
174    }
175
176    /**
177     * Get Level.
178     */
179    public function getLevel(): int
180    {
181        return $this->level;
182    }
183
184    /**
185     * Set Level.
186     *
187     * @param int $pValue Ranging 0 - 8
188     */
189    public function setLevel(int $pValue = 0): self
190    {
191        if ($pValue < 0) {
192            throw new OutOfBoundsException(0, null, $pValue);
193        }
194        $this->level = $pValue;
195
196        return $this;
197    }
198
199    /**
200     * Get indent.
201     */
202    public function getIndent(): float
203    {
204        return $this->indent;
205    }
206
207    /**
208     * Set indent.
209     */
210    public function setIndent(float $pValue = 0): self
211    {
212        if ($pValue > 0 && !in_array($this->getHorizontal(), $this->supportedStyles)) {
213            $pValue = 0; // indent not supported
214        }
215
216        $this->indent = $pValue;
217
218        return $this;
219    }
220
221    /**
222     * Get margin left.
223     */
224    public function getMarginLeft(): float
225    {
226        return $this->marginLeft;
227    }
228
229    /**
230     * Set margin left.
231     */
232    public function setMarginLeft(float $pValue = 0): self
233    {
234        if ($pValue > 0 && !in_array($this->getHorizontal(), $this->supportedStyles)) {
235            $pValue = 0; // margin left not supported
236        }
237
238        $this->marginLeft = $pValue;
239
240        return $this;
241    }
242
243    /**
244     * Get margin right.
245     */
246    public function getMarginRight(): float
247    {
248        return $this->marginRight;
249    }
250
251    /**
252     * Set margin ight.
253     */
254    public function setMarginRight(float $pValue = 0): self
255    {
256        if ($pValue > 0 && !in_array($this->getHorizontal(), $this->supportedStyles)) {
257            $pValue = 0; // margin right not supported
258        }
259
260        $this->marginRight = $pValue;
261
262        return $this;
263    }
264
265    /**
266     * Get margin top.
267     */
268    public function getMarginTop(): float
269    {
270        return $this->marginTop;
271    }
272
273    /**
274     * Set margin top.
275     */
276    public function setMarginTop(float $pValue = 0): self
277    {
278        $this->marginTop = $pValue;
279
280        return $this;
281    }
282
283    /**
284     * Get margin bottom.
285     */
286    public function getMarginBottom(): float
287    {
288        return $this->marginBottom;
289    }
290
291    /**
292     * Set margin bottom.
293     */
294    public function setMarginBottom(float $pValue = 0): self
295    {
296        $this->marginBottom = $pValue;
297
298        return $this;
299    }
300
301    public function getTextDirection(): string
302    {
303        return $this->textDirection;
304    }
305
306    public function setTextDirection(string $pValue = self::TEXT_DIRECTION_HORIZONTAL): self
307    {
308        if (empty($pValue)) {
309            $pValue = self::TEXT_DIRECTION_HORIZONTAL;
310        }
311        $this->textDirection = $pValue;
312
313        return $this;
314    }
315
316    public function isRTL(): bool
317    {
318        return $this->isRTL;
319    }
320
321    public function setIsRTL(bool $value = false): self
322    {
323        $this->isRTL = $value;
324
325        return $this;
326    }
327
328    /**
329     * Get hash code.
330     *
331     * @return string Hash code
332     */
333    public function getHashCode(): string
334    {
335        return md5(
336            $this->horizontal
337            . $this->vertical
338            . $this->level
339            . $this->indent
340            . $this->marginLeft
341            . $this->marginRight
342            . ($this->isRTL ? '1' : '0')
343            . __CLASS__
344        );
345    }
346
347    /**
348     * Get hash index.
349     *
350     * Note that this index may vary during script execution! Only reliable moment is
351     * while doing a write of a workbook and when changes are not allowed.
352     *
353     * @return null|int Hash index
354     */
355    public function getHashIndex(): ?int
356    {
357        return $this->hashIndex;
358    }
359
360    /**
361     * Set hash index.
362     *
363     * Note that this index may vary during script execution! Only reliable moment is
364     * while doing a write of a workbook and when changes are not allowed.
365     *
366     * @param int $value Hash index
367     *
368     * @return $this
369     */
370    public function setHashIndex(int $value)
371    {
372        $this->hashIndex = $value;
373
374        return $this;
375    }
376}