Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit b751206

Browse files
authored
Merge pull request #92 from LuckeyHomes/locales
Float formating under locale
2 parents 5d80666 + e86060d commit b751206

File tree

2 files changed

+160
-3
lines changed

2 files changed

+160
-3
lines changed

src/Geometries/Point.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ public function setLng($lng)
3535

3636
public function toPair()
3737
{
38-
return $this->getLng() . ' ' . $this->getLat();
38+
return self::stringifyFloat($this->getLng()) . ' ' . self::stringifyFloat($this->getLat());
3939
}
40-
40+
41+
private static function stringifyFloat($float)
42+
{
43+
// normalized output among locales
44+
return rtrim(rtrim(sprintf('%F', $float), '0'), '.');
45+
}
46+
4147
public static function fromPair($pair)
4248
{
4349
$pair = preg_replace('/^[a-zA-Z\(\)]+/', '', trim($pair));
@@ -58,7 +64,7 @@ public static function fromString($wktArgument)
5864

5965
public function __toString()
6066
{
61-
return $this->getLng() . ' ' . $this->getLat();
67+
return $this->toPair();
6268
}
6369

6470
/**

tests/Geometries/UnderLocaleTest.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
use Phaza\LaravelPostgis\Geometries\Point;
4+
use Phaza\LaravelPostgis\Geometries\MultiPoint;
5+
use Phaza\LaravelPostgis\Geometries\LineString;
6+
use Phaza\LaravelPostgis\Geometries\MultiLineString;
7+
use Phaza\LaravelPostgis\Geometries\Polygon;
8+
use Phaza\LaravelPostgis\Geometries\MultiPolygon;
9+
use Phaza\LaravelPostgis\Geometries\GeometryCollection;
10+
11+
class UnderLocaleTest extends BaseTestCase
12+
{
13+
14+
public static function setUpBeforeClass()
15+
{
16+
setlocale(LC_NUMERIC, 'fr_FR.utf-8');
17+
}
18+
19+
public static function tearDownAfterClass()
20+
{
21+
setlocale(LC_NUMERIC, null);
22+
}
23+
24+
public function setUp()
25+
{
26+
if(localeconv()['decimal_point'] == '.') {
27+
$this->markTestSkipped('The locale is not available for testing float output formatting');
28+
}
29+
}
30+
31+
public function testPointToWKT()
32+
{
33+
$point = new Point(1.5, 2.5);
34+
$this->assertEquals('POINT(2.5 1.5)', $point->toWKT());
35+
}
36+
37+
public function testMultiPointToWKT()
38+
{
39+
$multipoint = new MultiPoint([new Point(1.5, 1.5), new Point(1.5, 2.5), new Point(2.5, 2.5)]);
40+
41+
$this->assertEquals('MULTIPOINT((1.5 1.5),(2.5 1.5),(2.5 2.5))', $multipoint->toWKT());
42+
}
43+
44+
public function testLineStringToWKT()
45+
{
46+
$linestring = new LineString([new Point(1.5, 1.5), new Point(2.5, 2.5), new Point(3.5, 3.5)]);
47+
48+
$this->assertEquals('LINESTRING(1.5 1.5,2.5 2.5,3.5 3.5)', $linestring->toWKT());
49+
}
50+
51+
public function testMultiLineStringToWKT()
52+
{
53+
$collection = new LineString(
54+
[
55+
new Point(1.5, 1.5),
56+
new Point(1.5, 2.5),
57+
new Point(2.5, 2.5),
58+
new Point(2.5, 1.5),
59+
new Point(1.5, 1.5)
60+
]
61+
);
62+
63+
$multilinestring = new MultiLineString([$collection]);
64+
65+
$this->assertSame('MULTILINESTRING((1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5))', $multilinestring->toWKT());
66+
}
67+
68+
public function testPolygonToWKT()
69+
{
70+
$collection = new LineString(
71+
[
72+
new Point(1.5, 1.5),
73+
new Point(1.5, 2.5),
74+
new Point(2.5, 2.5),
75+
new Point(2.5, 1.5),
76+
new Point(1.5, 1.5)
77+
]
78+
);
79+
80+
$polygon = new Polygon([$collection]);
81+
82+
$this->assertEquals('POLYGON((1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5))', $polygon->toWKT());
83+
}
84+
85+
public function testMultiPolygonToWKT()
86+
{
87+
$collection1 = new LineString(
88+
[
89+
new Point(1.5, 1.5),
90+
new Point(1.5, 2.5),
91+
new Point(2.5, 2.5),
92+
new Point(2.5, 1.5),
93+
new Point(1.5, 1.5)
94+
]
95+
);
96+
97+
$collection2 = new LineString(
98+
[
99+
new Point(10.5, 10.5),
100+
new Point(10.5, 20.5),
101+
new Point(20.5, 20.5),
102+
new Point(20.5, 10.5),
103+
new Point(10.5, 10.5)
104+
]
105+
);
106+
107+
$polygon1 = new Polygon([$collection1, $collection2]);
108+
109+
$collection3 = new LineString(
110+
[
111+
new Point(100.5, 100.5),
112+
new Point(100.5, 200.5),
113+
new Point(200.5, 200.5),
114+
new Point(200.5, 100.5),
115+
new Point(100.5, 100.5)
116+
]
117+
);
118+
119+
$polygon2 = new Polygon([$collection3]);
120+
121+
$multiPolygon = new MultiPolygon([$polygon1, $polygon2]);
122+
123+
$this->assertEquals(
124+
'MULTIPOLYGON(((1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5),(10.5 10.5,20.5 10.5,20.5 20.5,10.5 20.5,10.5 10.5)),((100.5 100.5,200.5 100.5,200.5 200.5,100.5 200.5,100.5 100.5)))',
125+
$multiPolygon->toWKT()
126+
);
127+
}
128+
129+
public function testGeometryCollectionToWKT()
130+
{
131+
$collection = new LineString(
132+
[
133+
new Point(1.5, 1.5),
134+
new Point(1.5, 2.5),
135+
new Point(2.5, 2.5),
136+
new Point(2.5, 1.5),
137+
new Point(1.5, 1.5)
138+
]
139+
);
140+
141+
$point = new Point(100.5, 200.5);
142+
143+
$geo_collection = new GeometryCollection([$collection, $point]);
144+
145+
$this->assertEquals(
146+
'GEOMETRYCOLLECTION(LINESTRING(1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5),POINT(200.5 100.5))',
147+
$geo_collection->toWKT()
148+
);
149+
}
150+
151+
}

0 commit comments

Comments
 (0)