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

brick / geo / 13971277707

20 Mar 2025 02:05PM UTC coverage: 65.568% (+0.1%) from 65.472%
13971277707

push

github

BenMorel
Accept $prettyPrint in AbstractWktWriter constructor

1889 of 2881 relevant lines covered (65.57%)

1480.09 hits per line

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

68.89
/src/CircularString.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use ArrayIterator;
8
use Brick\Geo\Attribute\NoProxy;
9
use Brick\Geo\Exception\CoordinateSystemException;
10
use Brick\Geo\Exception\EmptyGeometryException;
11
use Brick\Geo\Exception\InvalidGeometryException;
12
use Brick\Geo\Exception\NoSuchGeometryException;
13
use Brick\Geo\Projector\Projector;
14
use Override;
15

16
/**
17
 * A CircularString is a Curve made of zero or more connected circular arc segments.
18
 *
19
 * A circular arc segment is a curved segment defined by three points in a two-dimensional plane;
20
 * the first point cannot be the same as the third point.
21
 *
22
 * @template-implements \IteratorAggregate<int<0, max>, Point>
23
 * @final
24
 */
25
readonly class CircularString extends Curve implements \Countable, \IteratorAggregate
26
{
27
    /**
28
     * The Points that compose this CircularString.
29
     *
30
     * An empty CircularString contains no points.
31
     *
32
     * @var list<Point>
33
     */
34
    protected array $points;
35

36
    /**
37
     * @throws InvalidGeometryException  If the number of points is invalid.
38
     * @throws CoordinateSystemException If different coordinate systems are used.
39
     */
40
    public function __construct(CoordinateSystem $cs, Point ...$points)
41
    {
42
        $numPoints = count($points);
4,001✔
43
        $isEmpty = ($numPoints === 0);
4,001✔
44

45
        parent::__construct($cs, $isEmpty);
4,001✔
46

47
        $this->points = array_values($points);
4,001✔
48

49
        if ($isEmpty) {
4,001✔
50
            return;
856✔
51
        }
52

53
        CoordinateSystem::check($this, ...$points);
3,161✔
54

55
        if ($numPoints < 3) {
3,161✔
56
            throw new InvalidGeometryException('A CircularString must be made of at least 3 points.');
24✔
57
        }
58

59
        if ($numPoints % 2 === 0) {
3,137✔
60
            throw new InvalidGeometryException('A CircularString must have an odd number of points.');
16✔
61
        }
62
    }
63

64
    /**
65
     * Creates a non-empty CircularString composed of the given points.
66
     *
67
     * @param Point    $point1 The first point.
68
     * @param Point ...$pointN The subsequent points.
69
     *
70
     * @throws InvalidGeometryException  If the number of points is invalid.
71
     * @throws CoordinateSystemException If the points use different coordinate systems.
72
     */
73
    public static function of(Point $point1, Point ...$pointN) : CircularString
74
    {
75
        return new CircularString($point1->coordinateSystem(), $point1, ...$pointN);
×
76
    }
77

78
    #[Override]
79
    public function startPoint() : Point
80
    {
81
        if (count($this->points) === 0) {
1,128✔
82
            throw new EmptyGeometryException('The CircularString is empty and has no start point.');
64✔
83
        }
84

85
        return $this->points[0];
1,064✔
86
    }
87

88
    #[Override]
89
    public function endPoint() : Point
90
    {
91
        $count = count($this->points);
712✔
92

93
        if ($count === 0) {
712✔
94
            throw new EmptyGeometryException('The CircularString is empty and has no end point.');
64✔
95
        }
96

97
        return $this->points[$count - 1];
648✔
98
    }
99

100
    /**
101
     * Returns the number of Points in this CircularString.
102
     */
103
    public function numPoints() : int
104
    {
105
        return count($this->points);
64✔
106
    }
107

108
    /**
109
     * Returns the specified Point N in this CircularString.
110
     *
111
     * @param int $n The point number, 1-based.
112
     *
113
     * @throws NoSuchGeometryException If there is no Point at this index.
114
     */
115
    public function pointN(int $n) : Point
116
    {
117
        if (! isset($this->points[$n - 1])) {
152✔
118
            throw new NoSuchGeometryException('There is no Point in this CircularString at index ' . $n);
48✔
119
        }
120

121
        return $this->points[$n - 1];
104✔
122
    }
123

124
    /**
125
     * Returns the points that compose this CircularString.
126
     *
127
     * @return list<Point>
128
     */
129
    public function points() : array
130
    {
131
        return $this->points;
×
132
    }
133

134
    #[NoProxy, Override]
135
    public function geometryType() : string
136
    {
137
        return 'CircularString';
1,751✔
138
    }
139

140
    #[NoProxy, Override]
141
    public function geometryTypeBinary() : int
142
    {
143
        return Geometry::CIRCULARSTRING;
839✔
144
    }
145

146
    #[Override]
147
    public function getBoundingBox() : BoundingBox
148
    {
149
        return array_reduce(
×
150
            $this->points,
×
151
            fn (BoundingBox $boundingBox, Point $point) => $boundingBox->extendedWithPoint($point),
×
152
            BoundingBox::new(),
×
153
        );
×
154
    }
155

156
    /**
157
     * @return list<list<float>>
158
     */
159
    #[Override]
160
    public function toArray() : array
161
    {
162
        return array_map(
512✔
163
            fn (Point $point) => $point->toArray(),
512✔
164
            $this->points,
512✔
165
        );
512✔
166
    }
167

168
    #[Override]
169
    public function project(Projector $projector): CircularString
170
    {
171
        return new CircularString(
×
172
            $projector->getTargetCoordinateSystem($this->coordinateSystem),
×
173
            ...array_map(
×
174
                fn (Point $point) => $point->project($projector),
×
175
                $this->points,
×
176
            ),
×
177
        );
×
178
    }
179

180
    /**
181
     * Returns the number of points in this CircularString.
182
     */
183
    #[Override]
184
    public function count() : int
185
    {
186
        return count($this->points);
847✔
187
    }
188

189
    /**
190
     * Returns an iterator for the points in this CircularString.
191
     *
192
     * @return ArrayIterator<int<0, max>, Point>
193
     */
194
    #[Override]
195
    public function getIterator() : ArrayIterator
196
    {
197
        return new ArrayIterator($this->points);
2,244✔
198
    }
199

200
    /**
201
     * Returns a copy of this CircularString, with the given points added.
202
     */
203
    public function withAddedPoints(Point ...$points): CircularString
204
    {
205
        return new CircularString($this->coordinateSystem, ...$this->points, ...$points);
48✔
206
    }
207
}
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