Passed
Push — main ( 08642f...10294e )
by Frank
02:24 queued 21s
created

ElementFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
eloc 45
dl 0
loc 116
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForLineString() 0 4 1
A buildForGeometryCollection() 0 12 2
A buildForMultiPoint() 0 8 2
A buildForMultiLineString() 0 8 2
A buildForMultiPolygon() 0 8 2
A buildForPosition() 0 7 1
A buildForPolygon() 0 9 2
A buildForPoint() 0 3 1
A buildForGeometryObject() 0 21 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PrinsFrank\PhpGeoSVG\Html\Factory;
6
7
use PrinsFrank\PhpGeoSVG\Coordinator\Coordinator;
8
use PrinsFrank\PhpGeoSVG\Exception\NotImplementedException;
9
use PrinsFrank\PhpGeoSVG\Geometry\GeometryCollection;
10
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\GeometryObject;
11
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\LineString;
12
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\MultiLineString;
13
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\MultiPoint;
14
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\MultiPolygon;
15
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\Point;
16
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\Polygon;
17
use PrinsFrank\PhpGeoSVG\Geometry\Position\Position;
18
use PrinsFrank\PhpGeoSVG\Html\Elements\CircleElement;
19
use PrinsFrank\PhpGeoSVG\Html\Elements\Element;
20
use PrinsFrank\PhpGeoSVG\Html\Elements\GroupElement;
21
use PrinsFrank\PhpGeoSVG\Html\Elements\PathElement;
22
use PrinsFrank\PhpGeoSVG\Html\Elements\SvgElement;
23
use PrinsFrank\PhpGeoSVG\Html\Elements\Text\TextContent;
24
use PrinsFrank\PhpGeoSVG\Html\Elements\TitleElement;
25
use PrinsFrank\PhpGeoSVG\Html\Rendering\PathShapeRenderer;
26
27
class ElementFactory
28
{
29
    /**
30
     * @throws NotImplementedException
31
     */
32 2
    public static function buildForGeometryCollection(GeometryCollection $geometryCollection, Coordinator $coordinator): Element
33
    {
34 2
        $svgElement = (new SvgElement())
35 2
            ->setAttribute('width', $coordinator->getWidth())
36 2
            ->setAttribute('height', $coordinator->getHeight())
37 2
            ->setAttribute('viewbox', '0 0 ' . $coordinator->getWidth() . ' ' . $coordinator->getHeight());
38
39 2
        foreach ($geometryCollection->getGeometryObjects() as $geometryObject) {
40 1
            $svgElement->addChildElement(self::buildForGeometryObject($geometryObject, $coordinator));
41
        }
42
43 2
        return $svgElement;
44
    }
45
46
    /**
47
     * @throws NotImplementedException
48
     */
49 4
    public static function buildForGeometryObject(GeometryObject $geometryObject, Coordinator $coordinator): Element
50
    {
51 4
        $element = match (get_class($geometryObject)) {
52 3
            LineString::class => self::buildForLineString($geometryObject, $coordinator),
53 1
            MultiPoint::class => self::buildForMultiPoint($geometryObject, $coordinator),
54 1
            MultiPolygon::class => self::buildForMultiPolygon($geometryObject, $coordinator),
55 1
            MultiLineString::class => self::buildForMultiLineString($geometryObject, $coordinator),
56 1
            Polygon::class => self::buildForPolygon($geometryObject, $coordinator),
57 1
            Point::class => self::buildForPoint($geometryObject, $coordinator),
58 1
            default => throw new NotImplementedException('GeometryObject with class "' . get_class($geometryObject) . '" can\'t be built yet.')
59
        };
60
61 3
        if (null !== $geometryObject->getTitle()) {
62 1
            $element->addChildElement((new TitleElement())->setTextContent(new TextContent($geometryObject->getTitle())));
63
        }
64
65 3
        if (null !== $geometryObject->getFeatureClass()) {
66 1
            $element->setAttribute('data-feature-class', $geometryObject->getFeatureClass());
67
        }
68
69 3
        return $element;
70
    }
71
72 1
    public static function buildForLineString(LineString $lineString, Coordinator $coordinator): PathElement
73
    {
74 1
        return (new PathElement())
75 1
            ->setAttribute('d', PathShapeRenderer::renderLineStringPath($lineString, $coordinator));
76
    }
77
78
    /**
79
     * @throws NotImplementedException
80
     */
81 2
    public static function buildForMultiPoint(MultiPoint $multiPoint, Coordinator $coordinator): GroupElement
82
    {
83 2
        $element = new GroupElement();
84 2
        foreach ($multiPoint->getPoints() as $point) {
85 1
            $element->addChildElement(self::buildForGeometryObject($point, $coordinator));
86
        }
87
88 2
        return $element;
89
    }
90
91
    /**
92
     * @throws NotImplementedException
93
     */
94 2
    public static function buildForMultiPolygon(MultiPolygon $multiPolygon, Coordinator $coordinator): GroupElement
95
    {
96 2
        $element = new GroupElement();
97 2
        foreach ($multiPolygon->getPolygons() as $polygon) {
98 1
            $element->addChildElement(self::buildForGeometryObject($polygon, $coordinator));
99
        }
100
101 2
        return $element;
102
    }
103
104
    /**
105
     * @throws NotImplementedException
106
     */
107 2
    public static function buildForMultiLineString(MultiLineString $multiLineString, Coordinator $coordinator): GroupElement
108
    {
109 2
        $element = new GroupElement();
110 2
        foreach ($multiLineString->getLineStrings() as $lineString) {
111 1
            $element->addChildElement(self::buildForGeometryObject($lineString, $coordinator));
112
        }
113
114 2
        return $element;
115
    }
116
117
    /**
118
     * @throws NotImplementedException
119
     */
120 2
    public static function buildForPolygon(Polygon $polygon, Coordinator $coordinator): Element
121
    {
122 2
        $element = new GroupElement();
123 2
        $element->addChildElement(self::buildForGeometryObject($polygon->getExteriorRing(), $coordinator));
124 2
        foreach ($polygon->getInteriorRings() as $interiorRing) {
125 1
            $element->addChildElement(self::buildForGeometryObject($interiorRing, $coordinator));
126
        }
127
128 2
        return $element;
129
    }
130
131 1
    public static function buildForPoint(Point $point, Coordinator $coordinator): CircleElement
132
    {
133 1
        return self::buildForPosition($point->getPosition(), $coordinator);
134
    }
135
136 1
    public static function buildForPosition(Position $position, Coordinator $coordinator): CircleElement
137
    {
138 1
        $element = new CircleElement();
139 1
        $element->setAttribute('cy', $coordinator->getY($position));
140 1
        $element->setAttribute('cx', $coordinator->getX($position));
141
142 1
        return $element;
143
    }
144
}
145