1<?php
2/**
3 * This file is part of FPDI
4 *
5 * @package   setasign\Fpdi
6 * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
7 * @license   http://opensource.org/licenses/mit-license The MIT License
8 */
9
10namespace setasign\Fpdi\PdfReader\DataStructure;
11
12use setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException;
13use setasign\Fpdi\PdfParser\PdfParser;
14use setasign\Fpdi\PdfParser\PdfParserException;
15use setasign\Fpdi\PdfParser\Type\PdfArray;
16use setasign\Fpdi\PdfParser\Type\PdfNumeric;
17use setasign\Fpdi\PdfParser\Type\PdfType;
18use setasign\Fpdi\PdfParser\Type\PdfTypeException;
19
20/**
21 * Class representing a rectangle
22 *
23 * @package setasign\Fpdi\PdfReader\DataStructure
24 */
25class Rectangle
26{
27    /**
28     * @var int|float
29     */
30    protected $llx;
31
32    /**
33     * @var int|float
34     */
35    protected $lly;
36
37    /**
38     * @var int|float
39     */
40    protected $urx;
41
42    /**
43     * @var int|float
44     */
45    protected $ury;
46
47    /**
48     * Create a rectangle instance by a PdfArray.
49     *
50     * @param PdfArray|mixed $array
51     * @param PdfParser $parser
52     * @return Rectangle
53     * @throws PdfTypeException
54     * @throws CrossReferenceException
55     * @throws PdfParserException
56     */
57    public static function byPdfArray($array, PdfParser $parser)
58    {
59        $array = PdfArray::ensure(PdfType::resolve($array, $parser), 4)->value;
60        $ax = PdfNumeric::ensure(PdfType::resolve($array[0], $parser))->value;
61        $ay = PdfNumeric::ensure(PdfType::resolve($array[1], $parser))->value;
62        $bx = PdfNumeric::ensure(PdfType::resolve($array[2], $parser))->value;
63        $by = PdfNumeric::ensure(PdfType::resolve($array[3], $parser))->value;
64
65        return new self($ax, $ay, $bx, $by);
66    }
67
68    /**
69     * Rectangle constructor.
70     *
71     * @param float|int $ax
72     * @param float|int $ay
73     * @param float|int $bx
74     * @param float|int $by
75     */
76    public function __construct($ax, $ay, $bx, $by)
77    {
78        $this->llx = \min($ax, $bx);
79        $this->lly = \min($ay, $by);
80        $this->urx = \max($ax, $bx);
81        $this->ury = \max($ay, $by);
82    }
83
84    /**
85     * Get the width of the rectangle.
86     *
87     * @return float|int
88     */
89    public function getWidth()
90    {
91        return $this->urx - $this->llx;
92    }
93
94    /**
95     * Get the height of the rectangle.
96     *
97     * @return float|int
98     */
99    public function getHeight()
100    {
101        return $this->ury - $this->lly;
102    }
103
104    /**
105     * Get the lower left abscissa.
106     *
107     * @return float|int
108     */
109    public function getLlx()
110    {
111        return $this->llx;
112    }
113
114    /**
115     * Get the lower left ordinate.
116     *
117     * @return float|int
118     */
119    public function getLly()
120    {
121        return $this->lly;
122    }
123
124    /**
125     * Get the upper right abscissa.
126     *
127     * @return float|int
128     */
129    public function getUrx()
130    {
131        return $this->urx;
132    }
133
134    /**
135     * Get the upper right ordinate.
136     *
137     * @return float|int
138     */
139    public function getUry()
140    {
141        return $this->ury;
142    }
143
144    /**
145     * Get the rectangle as an array.
146     *
147     * @return array
148     */
149    public function toArray()
150    {
151        return [
152            $this->llx,
153            $this->lly,
154            $this->urx,
155            $this->ury
156        ];
157    }
158
159    /**
160     * Get the rectangle as a PdfArray.
161     *
162     * @return PdfArray
163     */
164    public function toPdfArray()
165    {
166        $array = new PdfArray();
167        $array->value[] = PdfNumeric::create($this->llx);
168        $array->value[] = PdfNumeric::create($this->lly);
169        $array->value[] = PdfNumeric::create($this->urx);
170        $array->value[] = PdfNumeric::create($this->ury);
171
172        return $array;
173    }
174}
175