Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Rectangle
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
7 / 7
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 contains
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 intersect
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 union
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 scale
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 expand
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Geometry;
6
7final class Rectangle
8{
9    public function __construct(
10        public readonly float $x,
11        public readonly float $y,
12        public readonly float $width,
13        public readonly float $height,
14    ) {}
15
16    /** @return array<int, float> Returns [llx, lly, urx, ury] as used in PDF boxes */
17    public function toArray(): array
18    {
19        return [$this->x, $this->y, $this->x + $this->width, $this->y + $this->height];
20    }
21
22    public function contains(self $other): bool
23    {
24        return $other->x >= $this->x
25            && $other->y >= $this->y
26            && ($other->x + $other->width)  <= ($this->x + $this->width)
27            && ($other->y + $other->height) <= ($this->y + $this->height);
28    }
29
30    public function intersect(self $other): ?self
31    {
32        $x1 = max($this->x, $other->x);
33        $y1 = max($this->y, $other->y);
34        $x2 = min($this->x + $this->width, $other->x + $other->width);
35        $y2 = min($this->y + $this->height, $other->y + $other->height);
36        if ($x2 <= $x1 || $y2 <= $y1) {
37            return null;
38        }
39        return new self($x1, $y1, $x2 - $x1, $y2 - $y1);
40    }
41
42    public function union(self $other): self
43    {
44        $x1 = min($this->x, $other->x);
45        $y1 = min($this->y, $other->y);
46        $x2 = max($this->x + $this->width, $other->x + $other->width);
47        $y2 = max($this->y + $this->height, $other->y + $other->height);
48        return new self($x1, $y1, $x2 - $x1, $y2 - $y1);
49    }
50
51    public function scale(float $factor): self
52    {
53        return new self(
54            $this->x * $factor,
55            $this->y * $factor,
56            $this->width * $factor,
57            $this->height * $factor,
58        );
59    }
60
61    public function expand(float $margin): self
62    {
63        return new self(
64            $this->x - $margin,
65            $this->y - $margin,
66            $this->width + 2 * $margin,
67            $this->height + 2 * $margin,
68        );
69    }
70}