Skip to content

Commit 816cf73

Browse files
committed
Style parsing
1 parent 313369a commit 816cf73

23 files changed

Lines changed: 444 additions & 106 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Feature: Parse <Style>
2+
In order to manipulate a KML document
3+
As an user
4+
I need <Style> tag to be parsed as a Style object
5+
6+
Scenario:
7+
Given a KML document with a Document in "tests/kml/style.kml"
8+
When I parse the KML document
9+
Then I should get a KmlDocument object containing one Style
10+
And the Style object should contain a IconStyle object with the following properties:
11+
| property | value |
12+
| color | ffffffff |
13+
| colorMode | 2021-09-12T16:14:18.765 |

features/read/kml/parse_time_stamp.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Feature: Parse <TimeStamp>
44
I need <TimeStamp> tag to be parsed as a TimeStamp object
55

66
Scenario:
7-
Given a KML document with a NetworkLinkControl in "tests/kml/time-span.kml"
7+
Given a KML document with a NetworkLinkControl in "tests/kml/time-stamp.kml"
88
When I parse the KML document
99
Then I should get a KmlDocument object containing one NetworkLinkControl
1010
And the NetworkLinkControl should contain a Camera with a 'TimeStamp' TimePrimitive property

src/Domain/FieldType/Color.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ public static function fromRGBA(int $red, int $green, int $blue, float $alpha) {
2020
return $color;
2121
}
2222

23+
public static function fromWhite() {
24+
$color = new Color();
25+
26+
$color->red = 0xFF;
27+
$color->green = 0xFF;
28+
$color->blue = 0xFF;
29+
$color->alpha = 1;
30+
31+
return $color;
32+
}
33+
34+
public static function fromBlack() {
35+
$color = new Color();
36+
37+
$color->red = 0x00;
38+
$color->green = 0x00;
39+
$color->blue = 0x00;
40+
$color->alpha = 1;
41+
42+
return $color;
43+
}
44+
2345
public function getRed(): int {
2446
return $this->red;
2547
}

src/Domain/FieldType/ColorMode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* Applies effects to a base color.
77
*/
88
class ColorMode {
9-
const NORMAL = 0;
10-
const RANDOM = 1;
9+
const NORMAL = 'normal';
10+
const RANDOM = 'random';
1111
}

src/Domain/FieldType/Vec2.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ class Vec2 {
1212
private $xUnits;
1313
private $yUnits;
1414

15+
public static function fromValues($x, $y, $xUnits, $yUnits) {
16+
$vec2 = new Vec2();
17+
$vec2->x = $x;
18+
$vec2->y = $y;
19+
$vec2->xUnits = $xUnits;
20+
$vec2->yUnits = $yUnits;
21+
return $vec2;
22+
}
23+
1524
public function getX(): float {
1625
return $this->x;
1726
}

src/Domain/StyleSelector/Style.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,51 @@ public function accept(KmlObjectVisitorInterface $visitor): void {
2626
$visitor->visitStyle($this);
2727
}
2828

29-
public function getIconStyle(): IconStyle {
29+
public function getIconStyle(): ?IconStyle {
3030
return $this->iconStyle;
3131
}
3232

33-
public function setIconStyle(IconStyle $iconStyle): void {
33+
public function setIconStyle(?IconStyle $iconStyle): void {
3434
$this->iconStyle = $iconStyle;
3535
}
3636

37-
public function getLabelStyle(): LabelStyle {
37+
public function getLabelStyle(): ?LabelStyle {
3838
return $this->labelStyle;
3939
}
4040

41-
public function setLabelStyle(LabelStyle $labelStyle): void {
41+
public function setLabelStyle(?LabelStyle $labelStyle): void {
4242
$this->labelStyle = $labelStyle;
4343
}
4444

45-
public function getLineStyle(): LineStyle {
45+
public function getLineStyle(): ?LineStyle {
4646
return $this->lineStyle;
4747
}
4848

49-
public function setLineStyle(LineStyle $lineStyle): void {
49+
public function setLineStyle(?LineStyle $lineStyle): void {
5050
$this->lineStyle = $lineStyle;
5151
}
5252

53-
public function getPolyStyle(): PolyStyle {
53+
public function getPolyStyle(): ?PolyStyle {
5454
return $this->polyStyle;
5555
}
5656

57-
public function setPolyStyle(PolyStyle $polyStyle): void {
57+
public function setPolyStyle(?PolyStyle $polyStyle): void {
5858
$this->polyStyle = $polyStyle;
5959
}
6060

61-
public function getBalloonStyle(): BalloonStyle {
61+
public function getBalloonStyle(): ?BalloonStyle {
6262
return $this->balloonStyle;
6363
}
6464

65-
public function setBalloonStyle(BalloonStyle $balloonStyle): void {
65+
public function setBalloonStyle(?BalloonStyle $balloonStyle): void {
6666
$this->balloonStyle = $balloonStyle;
6767
}
6868

69-
public function getListStyle(): ListStyle {
69+
public function getListStyle(): ?ListStyle {
7070
return $this->listStyle;
7171
}
7272

73-
public function setListStyle(ListStyle $listStyle): void {
73+
public function setListStyle(?ListStyle $listStyle): void {
7474
$this->listStyle = $listStyle;
7575
}
7676

src/Domain/SubStyle/BalloonStyle.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,35 @@ public function accept(KmlObjectVisitorInterface $visitor): void {
1919
$visitor->visitBalloonStyle($this);
2020
}
2121

22-
public function getBgColor(): Color {
22+
public function getBgColor(): ?Color {
2323
return $this->bgColor;
2424
}
2525

26-
public function setBgColor(Color $bgColor): void {
26+
public function setBgColor(?Color $bgColor): void {
2727
$this->bgColor = $bgColor;
2828
}
2929

30-
public function getTextColor(): Color {
30+
public function getTextColor(): ?Color {
3131
return $this->textColor;
3232
}
3333

34-
public function setTextColor(Color $textColor): void {
34+
public function setTextColor(?Color $textColor): void {
3535
$this->textColor = $textColor;
3636
}
3737

38-
public function getText(): string {
38+
public function getText(): ?string {
3939
return $this->text;
4040
}
4141

42-
public function setText(string $text): void {
42+
public function setText(?string $text): void {
4343
$this->text = $text;
4444
}
4545

46-
public function getDisplayMode(): string {
46+
public function getDisplayMode(): ?string {
4747
return $this->displayMode;
4848
}
4949

50-
public function setDisplayMode(string $displayMode): void {
50+
public function setDisplayMode(?string $displayMode): void {
5151
$this->displayMode = $displayMode;
5252
}
5353

src/Domain/SubStyle/ColorStyle/ColorStyle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ abstract class ColorStyle extends SubStyle {
1313
protected $color;
1414
protected $colorMode = "normal";
1515

16+
public function __construct() {
17+
$this->color = Color::fromWhite();
18+
}
19+
1620
public function getColor(): Color {
1721
return $this->color;
1822
}

src/Domain/SubStyle/ColorStyle/IconStyle.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
*/
1212
class IconStyle extends ColorStyle {
1313

14-
private $scale;
15-
private $heading;
14+
private $scale = 1;
15+
private $heading = 0;
1616
private $icon;
1717
private $hotSpot;
1818

19+
public function __construct() {
20+
parent::__construct();
21+
$this->hotSpot = Vec2::fromValues(0.5, 0.5, 'fraction', 'fraction');
22+
}
23+
1924
public function accept(KmlObjectVisitorInterface $visitor): void {
2025
$visitor->visitIconStyle($this);
2126
}
@@ -36,19 +41,19 @@ public function setHeading(float $heading): void {
3641
$this->heading = $heading;
3742
}
3843

39-
public function getIcon(): Icon {
44+
public function getIcon(): ?Icon {
4045
return $this->icon;
4146
}
4247

43-
public function setIcon(Icon $icon): void {
48+
public function setIcon(?Icon $icon): void {
4449
$this->icon = $icon;
4550
}
4651

47-
public function getHotSpot(): Vec2 {
52+
public function getHotSpot(): ?Vec2 {
4853
return $this->hotSpot;
4954
}
5055

51-
public function setHotSpot(Vec2 $hotSpot): void {
56+
public function setHotSpot(?Vec2 $hotSpot): void {
5257
$this->hotSpot = $hotSpot;
5358
}
5459

src/Reader/Kml/FieldType/ColorParser.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@
77
class ColorParser {
88

99
public static function parse(string $rgba): ?Color {
10-
if (strlen($rgba) <= 8 && strlen($rgba) >= 6) {
11-
$colorComponents = str_split($rgba, 2);
12-
13-
$red = hexdec($colorComponents[0]);
14-
$green = hexdec($colorComponents[1]);
15-
$blue = hexdec($colorComponents[2]);
16-
$alpha = 1;
17-
if (count($colorComponents) === 4) {
18-
$alpha = hexdec($colorComponents[3]) / 0xFF;
19-
}
20-
21-
return Color::fromRGBA($red, $green, $blue, $alpha);
10+
if (strlen($rgba) != 8) {
11+
return NULL;
2212
}
23-
return NULL;
13+
14+
$colorComponents = str_split($rgba, 2);
15+
16+
$red = hexdec($colorComponents[3]);
17+
$green = hexdec($colorComponents[2]);
18+
$blue = hexdec($colorComponents[1]);
19+
$alpha = hexdec($colorComponents[0]) / 0xFF;
20+
21+
return Color::fromRGBA($red, $green, $blue, $alpha);
2422
}
2523

2624
}

0 commit comments

Comments
 (0)