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

longitude-one / spatial-types / 10285190740

07 Aug 2024 01:22PM UTC coverage: 97.04% (-0.4%) from 97.475%
10285190740

push

github

Alexandre-T
Fixing covers attributes.

459 of 473 relevant lines covered (97.04%)

12.26 hits per line

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

95.65
/lib/LongitudeOne/SpatialTypes/Factory/FactoryLineString.php
1
<?php
2
/**
3
 * This file is part of the spatial project.
4
 *
5
 * PHP 8.1 | 8.2 | 8.3
6
 *
7
 * Copyright Alexandre Tranchant <alexandre.tranchant@gmail.com> 2024
8
 * Copyright Longitude One 2024
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 *
13
 */
14

15
declare(strict_types=1);
16

17
namespace LongitudeOne\SpatialTypes\Factory;
18

19
use LongitudeOne\SpatialTypes\Enum\DimensionEnum;
20
use LongitudeOne\SpatialTypes\Enum\FamilyEnum;
21
use LongitudeOne\SpatialTypes\Exception\InvalidDimensionException;
22
use LongitudeOne\SpatialTypes\Exception\InvalidValueException;
23
use LongitudeOne\SpatialTypes\Exception\SpatialTypeExceptionInterface;
24
use LongitudeOne\SpatialTypes\Interfaces\LineStringInterface;
25
use LongitudeOne\SpatialTypes\Interfaces\PointInterface;
26
use LongitudeOne\SpatialTypes\Types\Geography\LineString as GeographyLineString;
27
use LongitudeOne\SpatialTypes\Types\Geometry\LineString as GeometryLineString;
28

29
class FactoryLineString
30
{
31
    /**
32
     * Create a linestring from an array of points.
33
     *
34
     * @param PointInterface[] $points        array of points
35
     * @param ?int             $srid          SRID
36
     * @param FamilyEnum       $family        family
37
     * @param DimensionEnum    $dimensionEnum dimension
38
     *
39
     * @throws SpatialTypeExceptionInterface when something goes wrong during the creation of the linestring
40
     */
41
    public static function fromArrayOfPoints(array $points, ?int $srid = null, FamilyEnum $family = FamilyEnum::GEOMETRY, DimensionEnum $dimensionEnum = DimensionEnum::X_Y): LineStringInterface
5✔
42
    {
43
        if (DimensionEnum::X_Y !== $dimensionEnum) {
5✔
44
            throw new InvalidDimensionException('Only the two-dimensions points are yet supported.');
1✔
45
        }
46

47
        foreach ($points as $point) {
4✔
48
            if (!$point instanceof PointInterface) {
4✔
49
                throw new InvalidValueException('The array must contain only objects implementing PointInterface.');
1✔
50
            }
51
        }
52

53
        $lineString = match ($family) {
3✔
54
            FamilyEnum::GEOGRAPHY => new GeographyLineString([], $srid),
3✔
55
            FamilyEnum::GEOMETRY => new GeometryLineString([], $srid),
1✔
56
        };
3✔
57

58
        foreach ($points as $point) {
3✔
59
            $lineString->addPoint($point);
3✔
60
        }
61

62
        return $lineString;
3✔
63
    }
64

65
    /**
66
     * Create a linestring from an indexed array.
67
     *
68
     * @param array{0: float|int|string, 1: float|int|string, 2 ?: null|float|int, 3 ?: null|\DateTimeInterface|float|int}[]|PointInterface[] $indexedArray indexed array
69
     * @param ?int                                                                                                                            $srid         SRID
70
     * @param FamilyEnum                                                                                                                      $family       family
71
     * @param DimensionEnum                                                                                                                   $dimension    dimension
72
     *
73
     * @throws SpatialTypeExceptionInterface when something goes wrong during the creation of the point or the linestring
74
     */
75
    public static function fromIndexedArray(array $indexedArray, ?int $srid = null, FamilyEnum $family = FamilyEnum::GEOMETRY, DimensionEnum $dimension = DimensionEnum::X_Y): LineStringInterface
2✔
76
    {
77
        $points = [];
2✔
78
        foreach ($indexedArray as $point) {
2✔
79
            if (!is_array($point) && !$point instanceof PointInterface) {
2✔
80
                throw new InvalidValueException('The array must contain only objects implementing PointInterface or array of coordinates.');
×
81
            }
82

83
            if ($point instanceof PointInterface) {
2✔
84
                $points[] = $point;
1✔
85

86
                continue;
1✔
87
            }
88

89
            $points[] = FactoryPoint::fromIndexedArray($point, $srid, $family, $dimension);
1✔
90
        }
91

92
        return self::fromArrayOfPoints($points, $srid, $family, $dimension);
2✔
93
    }
94
}
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