Skip to content

Commit 6d27a59

Browse files
committed
Implementing styles
1 parent 2eed7c3 commit 6d27a59

36 files changed

Lines changed: 672 additions & 130 deletions

features/read/kml/parse_style.feature

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Feature: Parse <Style>
44
I need <Style> tag to be parsed as a Style object
55

66
Scenario: Style with a Balloon
7-
Given a KML document with a Document in "tests/kml/document.kml"
7+
Given a KML document with a Document in "tests/kml/style.kml"
88
When I parse the KML document
99
Then I should get a KmlDocument object containing one feature 'LibKml\Domain\Feature\Container\Document'
1010
And the feature should contain a Style object with the following properties:
@@ -13,13 +13,15 @@ Feature: Parse <Style>
1313
| targetId | target-1 |
1414
And the Style should contain a BalloonStyle with the following properties:
1515
| property | value |
16-
| bgColor | ffffffbb |
17-
| textColor | ff00aabb |
1816
| text | $[name] |
1917
| displayMode | random |
18+
And the BalloonStyle should contain the following property colors:
19+
| property | value |
20+
| bgColor | ffffffbb |
21+
| textColor | ff00aabb |
2022

2123
Scenario: Style with a IconStyle
22-
Given a KML document with a Document in "tests/kml/document.kml"
24+
Given a KML document with a Document in "tests/kml/style.kml"
2325
When I parse the KML document
2426
Then I should get a KmlDocument object containing one feature 'LibKml\Domain\Feature\Container\Document'
2527
And the feature should contain a Style object with the following properties:
@@ -28,13 +30,15 @@ Feature: Parse <Style>
2830
| targetId | target-1 |
2931
And the Style should contain a IconStyle with the following properties:
3032
| property | value |
31-
| color | ff00bbcc |
3233
| colorMode | random |
33-
| scale | 0.5 |
34+
| scale | 1.39 |
3435
| heading | 2.56 |
36+
And the IconStyle should contain the following property colors:
37+
| property | value |
38+
| color | a1ff00ff |
3539

3640
Scenario: Style with a LabelStyle
37-
Given a KML document with a Document in "tests/kml/document.kml"
41+
Given a KML document with a Document in "tests/kml/style.kml"
3842
When I parse the KML document
3943
Then I should get a KmlDocument object containing one feature 'LibKml\Domain\Feature\Container\Document'
4044
And the feature should contain a Style object with the following properties:
@@ -43,13 +47,14 @@ Feature: Parse <Style>
4347
| targetId | target-1 |
4448
And the Style should contain a LabelStyle with the following properties:
4549
| property | value |
46-
| color | ffaabbcc |
4750
| colorMode | random |
48-
| scale | 0.75 |
49-
| heading | 56.42 |
51+
| scale | 1.5 |
52+
And the LabelStyle should contain the following property colors:
53+
| property | value |
54+
| color | 7fffaaff |
5055

5156
Scenario: Style with a LineStyle
52-
Given a KML document with a Document in "tests/kml/document.kml"
57+
Given a KML document with a Document in "tests/kml/style.kml"
5358
When I parse the KML document
5459
Then I should get a KmlDocument object containing one feature 'LibKml\Domain\Feature\Container\Document'
5560
And the feature should contain a Style object with the following properties:
@@ -58,12 +63,14 @@ Feature: Parse <Style>
5863
| targetId | target-1 |
5964
And the Style should contain a LineStyle with the following properties:
6065
| property | value |
61-
| color | ffddbbcc |
6266
| colorMode | random |
6367
| width | 2.75 |
68+
And the LineStyle should contain the following property colors:
69+
| property | value |
70+
| color | ffddbbcc |
6471

6572
Scenario: Style with a ListStyle
66-
Given a KML document with a Document in "tests/kml/document.kml"
73+
Given a KML document with a Document in "tests/kml/style.kml"
6774
When I parse the KML document
6875
Then I should get a KmlDocument object containing one feature 'LibKml\Domain\Feature\Container\Document'
6976
And the feature should contain a Style object with the following properties:
@@ -73,10 +80,12 @@ Feature: Parse <Style>
7380
And the Style should contain a ListStyle with the following properties:
7481
| property | value |
7582
| listItemType | radioFolder |
83+
And the ListStyle should contain the following property colors:
84+
| property | value |
7685
| bgColor | 55aabbcc |
7786

7887
Scenario: Style with a PolyStyle
79-
Given a KML document with a Document in "tests/kml/document.kml"
88+
Given a KML document with a Document in "tests/kml/style.kml"
8089
When I parse the KML document
8190
Then I should get a KmlDocument object containing one feature 'LibKml\Domain\Feature\Container\Document'
8291
And the feature should contain a Style object with the following properties:
@@ -85,7 +94,9 @@ Feature: Parse <Style>
8594
| targetId | target-1 |
8695
And the Style should contain a PolyStyle with the following properties:
8796
| property | value |
88-
| color | ffddaa00 |
8997
| colorMode | random |
9098
| fill | 0 |
9199
| outline | 0 |
100+
And the PolyStyle should contain the following property colors:
101+
| property | value |
102+
| color | ffddaa00 |

src/Domain/FieldType/DisplayModeEnum.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace LibKml\Domain\FieldType;
44

55
class DisplayModeEnum {
6+
const DEFAULT = 'default';
7+
const HIDE = 'hide';
68
const NORMAL = 'normal';
79
const RANDOM = 'random';
810
}

src/Domain/SubStyle/BalloonStyle.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LibKml\Domain\SubStyle;
44

55
use LibKml\Domain\FieldType\Color;
6+
use LibKml\Domain\FieldType\DisplayModeEnum;
67
use LibKml\Domain\KmlObjectVisitorInterface;
78

89
/**
@@ -15,6 +16,13 @@ class BalloonStyle extends SubStyle {
1516
private $text;
1617
private $displayMode;
1718

19+
public function __construct() {
20+
$this->bgColor = Color::fromWhite();
21+
$this->textColor = Color::fromBlack();
22+
$this->displayMode = DisplayModeEnum::DEFAULT;
23+
$this->text = '';
24+
}
25+
1826
public function accept(KmlObjectVisitorInterface $visitor): void {
1927
$visitor->visitBalloonStyle($this);
2028
}

src/Domain/SubStyle/ColorStyle/LabelStyle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class LabelStyle extends ColorStyle {
1111

12-
private $scale;
12+
private $scale = 1;
1313

1414
public function accept(KmlObjectVisitorInterface $visitor): void {
1515
$visitor->visitLabelStyle($this);

src/Domain/SubStyle/ColorStyle/LineStyle.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
*/
1010
class LineStyle extends ColorStyle {
1111

12-
private $width;
12+
private $width = 1;
1313

1414
public function accept(KmlObjectVisitorInterface $visitor): void {
1515
$visitor->visitLineStyle($this);
1616
}
1717

18-
public function getWidth(): int {
18+
public function getWidth(): float {
1919
return $this->width;
2020
}
2121

22-
public function setWidth(int $width): void {
22+
public function setWidth(float $width): void {
2323
$this->width = $width;
2424
}
2525

src/Domain/SubStyle/ColorStyle/PolyStyle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*/
1010
class PolyStyle extends ColorStyle {
1111

12-
private $fill;
13-
private $outline;
12+
private $fill = TRUE;
13+
private $outline = TRUE;
1414

1515
public function accept(KmlObjectVisitorInterface $visitor): void {
1616
$visitor->visitPolyStyle($this);

src/Domain/SubStyle/ItemIcon.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace LibKml\Domain\SubStyle;
44

5+
use LibKml\Domain\FieldType\ItemIconState;
6+
57
class ItemIcon {
68

79
private $href;
8-
private $state;
10+
private $state = ItemIconState::OPEN;
911

1012
public function getHref(): string {
1113
return $this->href;

src/Reader/Kml/Feature/FeatureParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use LibKml\Domain\KmlObject;
66
use LibKml\Reader\Kml\AbstractView\AbstractViewExtractor;
77
use LibKml\Reader\Kml\KmlObjectParser;
8+
use LibKml\Reader\Kml\StyleSelector\StyleSelectorExtractor;
89
use SimpleXMLElement;
910

1011
abstract class FeatureParser extends KmlObjectParser {
@@ -45,6 +46,8 @@ protected function loadValues(KmlObject &$kmlObject, SimpleXMLElement $element):
4546
}
4647

4748
$kmlObject->setAbstractView(AbstractViewExtractor::extract($element));
49+
50+
$kmlObject->setStyleSelectors(StyleSelectorExtractor::extractStyleSelectors($element));
4851
}
4952

5053
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace LibKml\Reader\Kml\FieldType;
4+
5+
use LibKml\Domain\FieldType\Vec2;
6+
use SimpleXMLElement;
7+
8+
/**
9+
* Parses a KML Vec2 field element.
10+
*
11+
* @package LibKml\Tests\Reader\Kml\FieldType
12+
*/
13+
class Vec2Parser {
14+
15+
public static function parse(SimpleXMLElement $xmlElement): Vec2 {
16+
$vec2 = new Vec2();
17+
18+
if (isset($xmlElement['x'])) {
19+
$vec2->setX(floatval($xmlElement['x']));
20+
}
21+
22+
if (isset($xmlElement['y'])) {
23+
$vec2->setY(floatval($xmlElement['y']));
24+
}
25+
26+
if (isset($xmlElement['xunits'])) {
27+
$vec2->setXUnits(trim($xmlElement['xunits']));
28+
}
29+
30+
if (isset($xmlElement['yunits'])) {
31+
$vec2->setYUnits(trim($xmlElement['yunits']));
32+
}
33+
34+
return $vec2;
35+
}
36+
37+
}

src/Reader/Kml/KmlElementParserFactory.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
use LibKml\Reader\Kml\Geometry\MultiGeometryParser;
1818
use LibKml\Reader\Kml\Geometry\PointParser;
1919
use LibKml\Reader\Kml\Geometry\PolygonParser;
20+
use LibKml\Reader\Kml\StyleSelector\StyleParser;
21+
use LibKml\Reader\Kml\SubStyle\BalloonStyleParser;
22+
use LibKml\Reader\Kml\SubStyle\ColorStyle\IconStyleParser;
23+
use LibKml\Reader\Kml\SubStyle\ColorStyle\LabelStyleParser;
24+
use LibKml\Reader\Kml\SubStyle\ColorStyle\LineStyleParser;
25+
use LibKml\Reader\Kml\SubStyle\ColorStyle\PolyStyleParser;
26+
use LibKml\Reader\Kml\SubStyle\ListStyleParser;
2027
use LibKml\Reader\Kml\TimePrimitive\TimeSpanParser;
2128
use LibKml\Reader\Kml\TimePrimitive\TimeStampParser;
2229

@@ -46,6 +53,14 @@ class KmlElementParserFactory {
4653
const TIME_SPAN = "TimeSpan";
4754
const TIME_STAMP = "TimeStamp";
4855

56+
const STYLE = "Style";
57+
const BALLOON_STYLE = "BalloonStyle";
58+
const LIST_STYLE = "ListStyle";
59+
const ICON_STYLE = "IconStyle";
60+
const LABEL_STYLE = "LabelStyle";
61+
const LINE_STYLE = "LineStyle";
62+
const POLY_STYLE = "PolyStyle";
63+
4964
private static $instance;
5065

5166
private $parsers = [];
@@ -60,6 +75,11 @@ public static function getInstance(): KmlElementParserFactory {
6075
return self::$instance;
6176
}
6277

78+
/**
79+
* @param string $name
80+
* @return KmlElementParserInterface
81+
* @throws UnsupportedTagException
82+
*/
6383
public function getParserByElementName(string $name): KmlElementParserInterface {
6484

6585
if (!array_key_exists($name, $this->parsers)) {
@@ -140,6 +160,34 @@ public function getParserByElementName(string $name): KmlElementParserInterface
140160
$this->parsers[$name] = new TimeStampParser();
141161
break;
142162

163+
case self::STYLE:
164+
$this->parsers[$name] = new StyleParser();
165+
break;
166+
167+
case self::BALLOON_STYLE:
168+
$this->parsers[$name] = new BalloonStyleParser();
169+
break;
170+
171+
case self::LIST_STYLE:
172+
$this->parsers[$name] = new ListStyleParser();
173+
break;
174+
175+
case self::ICON_STYLE:
176+
$this->parsers[$name] = new IconStyleParser();
177+
break;
178+
179+
case self::LABEL_STYLE:
180+
$this->parsers[$name] = new LabelStyleParser();
181+
break;
182+
183+
case self::LINE_STYLE:
184+
$this->parsers[$name] = new LineStyleParser();
185+
break;
186+
187+
case self::POLY_STYLE:
188+
$this->parsers[$name] = new PolyStyleParser();
189+
break;
190+
143191
default:
144192
throw new UnsupportedTagException("Tag $name not supported");
145193
}

0 commit comments

Comments
 (0)