• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / geo / 13687942239

05 Mar 2025 11:54PM UTC coverage: 84.507% (+0.07%) from 84.44%
13687942239

push

github

BenMorel
Remove Psalm-specific annotations

1560 of 1846 relevant lines covered (84.51%)

1977.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.42
/src/CurvePolygon.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use ArrayIterator;
8
use Brick\Geo\Exception\CoordinateSystemException;
9
use Brick\Geo\Exception\EmptyGeometryException;
10
use Brick\Geo\Exception\NoSuchGeometryException;
11
use Brick\Geo\Projector\Projector;
12
use Override;
13

14
/**
15
 * A CurvePolygon is a planar Surface defined by 1 exterior boundary and 0 or more interior boundaries.
16
 *
17
 * A CurvePolygon instance differs from a Polygon instance in that a CurvePolygon instance may contain
18
 * the following circular arc segments: CircularString and CompoundCurve in addition to LineString.
19
 *
20
 * @template-implements \IteratorAggregate<Curve>
21
 */
22
final class CurvePolygon extends Surface implements \Countable, \IteratorAggregate
23
{
24
    /**
25
     * The rings that compose this CurvePolygon.
26
     *
27
     * The first one represents the exterior ring, and the
28
     * (optional) other ones represent the interior rings (holes) of the CurvePolygon.
29
     *
30
     * An empty CurvePolygon contains no rings.
31
     *
32
     * @var list<Curve>
33
     */
34
    protected array $rings = [];
35

36
    /**
37
     * The coordinate system of each of the rings must match the one of the CurvePolygon.
38
     *
39
     * @param CoordinateSystem $cs       The coordinate system of the CurvePolygon.
40
     * @param Curve            ...$rings The rings that compose the CurvePolygon.
41
     *
42
     * @throws CoordinateSystemException If different coordinate systems are used.
43
     */
44
    public function __construct(CoordinateSystem $cs, Curve ...$rings)
45
    {
46
        parent::__construct($cs, ! $rings);
1,841✔
47

48
        if (! $rings) {
1,841✔
49
            return;
854✔
50
        }
51

52
        CoordinateSystem::check($this, ...$rings);
1,001✔
53

54
        $this->rings = array_values($rings);
1,001✔
55
    }
56

57
    /**
58
     * Creates a non-empty CurvePolygon composed of the given rings.
59
     *
60
     * @param Curve    $exteriorRing  The exterior ring.
61
     * @param Curve ...$interiorRings The interior rings, if any.
62
     *
63
     * @throws CoordinateSystemException If the rings use different coordinate systems.
64
     */
65
    public static function of(Curve $exteriorRing, Curve ...$interiorRings) : CurvePolygon
66
    {
67
        return new CurvePolygon($exteriorRing->coordinateSystem(), $exteriorRing, ...$interiorRings);
×
68
    }
69

70
    /**
71
     * Returns the exterior ring of this CurvePolygon.
72
     *
73
     * @throws EmptyGeometryException
74
     */
75
    public function exteriorRing() : Curve
76
    {
77
        if ($this->isEmpty) {
140✔
78
            throw new EmptyGeometryException('An empty CurvePolygon has no exterior ring.');
28✔
79
        }
80

81
        return $this->rings[0];
112✔
82
    }
83

84
    /**
85
     * Returns the number of interior rings in this CurvePolygon.
86
     */
87
    public function numInteriorRings() : int
88
    {
89
        if ($this->isEmpty) {
63✔
90
            return 0;
28✔
91
        }
92

93
        return count($this->rings) - 1;
35✔
94
    }
95

96
    /**
97
     * Returns the specified interior ring N in this CurvePolygon.
98
     *
99
     * @param int $n The ring number, 1-based.
100
     *
101
     * @throws NoSuchGeometryException If there is no interior ring at this index.
102
     */
103
    public function interiorRingN(int $n) : Curve
104
    {
105
        if ($n === 0 || ! isset($this->rings[$n])) {
308✔
106
            throw new NoSuchGeometryException('There is no interior ring in this CurvePolygon at index ' . $n);
252✔
107
        }
108

109
        return $this->rings[$n];
56✔
110
    }
111

112
    /**
113
     * Returns the interior rings in this CurvePolygon.
114
     *
115
     * @return list<Curve>
116
     */
117
    public function interiorRings() : array
118
    {
119
        return array_slice($this->rings, 1);
70✔
120
    }
121

122
    #[Override]
123
    public function geometryType() : string
124
    {
125
        return 'CurvePolygon';
784✔
126
    }
127

128
    #[Override]
129
    public function geometryTypeBinary() : int
130
    {
131
        return Geometry::CURVEPOLYGON;
350✔
132
    }
133

134
    #[Override]
135
    public function getBoundingBox() : BoundingBox
136
    {
137
        $boundingBox = BoundingBox::new();
×
138

139
        foreach ($this->rings as $ring) {
×
140
            $boundingBox = $boundingBox->extendedWithBoundingBox($ring->getBoundingBox());
×
141
        }
142

143
        return $boundingBox;
×
144
    }
145

146
    #[Override]
147
    public function toArray() : array
148
    {
149
        return array_map(
224✔
150
            fn (Curve $ring) => $ring->toArray(),
224✔
151
            $this->rings,
224✔
152
        );
224✔
153
    }
154

155
    #[Override]
156
    public function project(Projector $projector): static
157
    {
158
        return new CurvePolygon(
×
159
            $projector->getTargetCoordinateSystem($this->coordinateSystem),
×
160
            ...array_map(
×
161
                fn (Curve $ring) => $ring->project($projector),
×
162
                $this->rings,
×
163
            ),
×
164
        );
×
165
    }
166

167
    /**
168
     * Returns the number of rings (exterior + interior) in this CurvePolygon.
169
     */
170
    #[Override]
171
    public function count() : int
172
    {
173
        return count($this->rings);
350✔
174
    }
175

176
    /**
177
     * Returns an iterator for the rings (exterior + interior) in this CurvePolygon.
178
     *
179
     * @return ArrayIterator<int<0, max>, Curve>
180
     */
181
    #[Override]
182
    public function getIterator() : ArrayIterator
183
    {
184
        return new ArrayIterator($this->rings);
798✔
185
    }
186

187
    /**
188
     * Returns a copy of this CurvePolygon, with the exterior ring replaced with the given one.
189
     */
190
    public function withExteriorRing(Curve $exteriorRing) : CurvePolygon
191
    {
192
        return new CurvePolygon($this->coordinateSystem, $exteriorRing, ...$this->interiorRings());
28✔
193
    }
194

195
    /**
196
     * Returns a copy of this CurvePolygon, with the interior rings replaced with the given ones.
197
     */
198
    public function withInteriorRings(Curve ...$interiorRings) : CurvePolygon
199
    {
200
        return new CurvePolygon($this->coordinateSystem, $this->exteriorRing(), ...$interiorRings);
42✔
201
    }
202

203
    /**
204
     * Returns a copy of this CurvePolygon, with the given interior rings added.
205
     */
206
    public function withAddedInteriorRings(Curve ...$interiorRings) : CurvePolygon
207
    {
208
        return new CurvePolygon($this->coordinateSystem, $this->exteriorRing(), ...$this->interiorRings(), ...$interiorRings);
42✔
209
    }
210
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc