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

brick / geo / 13349853684

16 Feb 2025 12:13AM UTC coverage: 80.033% (+32.7%) from 47.314%
13349853684

push

github

BenMorel
Use PHP 8.4 lazy proxies

27 of 48 new or added lines in 2 files covered. (56.25%)

55 existing lines in 4 files now uncovered.

1443 of 1803 relevant lines covered (80.03%)

499.41 hits per line

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

31.43
/src/IO/WKBReader.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\IO;
6

7
use Brick\Geo\CircularString;
8
use Brick\Geo\CompoundCurve;
9
use Brick\Geo\CurvePolygon;
10
use Brick\Geo\Geometry;
11
use Brick\Geo\Exception\GeometryIOException;
12
use Brick\Geo\GeometryCollection;
13
use Brick\Geo\LineString;
14
use Brick\Geo\MultiLineString;
15
use Brick\Geo\MultiPoint;
16
use Brick\Geo\MultiPolygon;
17
use Brick\Geo\Point;
18
use Brick\Geo\Polygon;
19
use Brick\Geo\PolyhedralSurface;
20
use Brick\Geo\TIN;
21
use Brick\Geo\Triangle;
22

23
/**
24
 * Builds geometries out of Well-Known Binary strings.
25
 */
26
class WKBReader extends AbstractWKBReader
27
{
28
    /**
29
     * @param string $wkb  The WKB to read.
30
     * @param int    $srid The optional SRID of the geometry.
31
     *
32
     * @throws GeometryIOException
33
     */
34
    public function read(string $wkb, int $srid = 0) : Geometry
35
    {
36
        $buffer = new WKBBuffer($wkb);
529✔
37
        $geometry = $this->readGeometry($buffer, $srid);
529✔
38

39
        if (! $buffer->isEndOfStream()) {
529✔
40
            throw GeometryIOException::invalidWKB('unexpected data at end of stream');
×
41
        }
42

43
        return $geometry;
529✔
44
    }
45

46
    /**
47
     * Introspects the given WKB and returns a proxy of the corresponding class.
48
     *
49
     * This prevents having to fully hydrate the underlying geometry object graph, while still returning an instance
50
     * of the correct geometry class.
51
     *
52
     * @throws GeometryIOException
53
     */
54
    public function readAsProxy(string $wkb, int $srid = 0) : Geometry
55
    {
56
        $buffer = new WKBBuffer($wkb);
×
57
        $buffer->readByteOrder();
×
58
        $geometryHeader = $this->readGeometryHeader($buffer);
×
59

NEW
60
        $geometryClass = $this->getGeometryClass($geometryHeader->geometryType);
×
NEW
61
        $reflectionClass = new \ReflectionClass($geometryClass);
×
62

NEW
63
        return $reflectionClass->newLazyProxy(fn() => $geometryClass::fromBinary($wkb, $srid));
×
64
    }
65

66
    /**
67
     * @param Geometry::* $geometryType
68
     *
69
     * @return class-string<Geometry>
70
     *
71
     * @throws GeometryIOException
72
     */
73
    private function getGeometryClass(int $geometryType) : string
74
    {
NEW
75
        return match ($geometryType) {
×
NEW
76
            Geometry::POINT => Point::class,
×
NEW
77
            Geometry::LINESTRING => LineString::class,
×
NEW
78
            Geometry::CIRCULARSTRING => CircularString::class,
×
NEW
79
            Geometry::COMPOUNDCURVE => CompoundCurve::class,
×
NEW
80
            Geometry::POLYGON => Polygon::class,
×
NEW
81
            Geometry::CURVEPOLYGON => CurvePolygon::class,
×
NEW
82
            Geometry::MULTIPOINT => MultiPoint::class,
×
NEW
83
            Geometry::MULTILINESTRING => MultiLineString::class,
×
NEW
84
            Geometry::MULTIPOLYGON => MultiPolygon::class,
×
NEW
85
            Geometry::GEOMETRYCOLLECTION => GeometryCollection::class,
×
NEW
86
            Geometry::POLYHEDRALSURFACE => PolyhedralSurface::class,
×
NEW
87
            Geometry::TIN => TIN::class,
×
NEW
88
            Geometry::TRIANGLE => Triangle::class,
×
NEW
89
            default => throw GeometryIOException::unsupportedWKBType($geometryType),
×
UNCOV
90
        };
×
91
    }
92

93
    protected function readGeometryHeader(WKBBuffer $buffer) : WKBGeometryHeader
94
    {
95
        $wkbType = $buffer->readUnsignedLong();
529✔
96

97
        if ($wkbType < 0 || $wkbType >= 4000) {
529✔
98
            throw GeometryIOException::unsupportedWKBType($wkbType);
×
99
        }
100

101
        $geometryType = $wkbType % 1000;
529✔
102
        $dimension = ($wkbType - $geometryType) / 1000;
529✔
103

104
        $hasZ = ($dimension === 1 || $dimension === 3);
529✔
105
        $hasM = ($dimension === 2 || $dimension === 3);
529✔
106

107
        return new WKBGeometryHeader($geometryType, $hasZ, $hasM);
529✔
108
    }
109
}
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