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

Commit

Permalink
Merge pull request #92 from LuckeyHomes/locales
Browse files Browse the repository at this point in the history
Float formating under locale
  • Loading branch information
njbarrett authored Nov 20, 2017
2 parents 5d80666 + e86060d commit b751206
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Geometries/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ public function setLng($lng)

public function toPair()
{
return $this->getLng() . ' ' . $this->getLat();
return self::stringifyFloat($this->getLng()) . ' ' . self::stringifyFloat($this->getLat());
}


private static function stringifyFloat($float)
{
// normalized output among locales
return rtrim(rtrim(sprintf('%F', $float), '0'), '.');
}

public static function fromPair($pair)
{
$pair = preg_replace('/^[a-zA-Z\(\)]+/', '', trim($pair));
Expand All @@ -58,7 +64,7 @@ public static function fromString($wktArgument)

public function __toString()
{
return $this->getLng() . ' ' . $this->getLat();
return $this->toPair();
}

/**
Expand Down
151 changes: 151 additions & 0 deletions tests/Geometries/UnderLocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

use Phaza\LaravelPostgis\Geometries\Point;
use Phaza\LaravelPostgis\Geometries\MultiPoint;
use Phaza\LaravelPostgis\Geometries\LineString;
use Phaza\LaravelPostgis\Geometries\MultiLineString;
use Phaza\LaravelPostgis\Geometries\Polygon;
use Phaza\LaravelPostgis\Geometries\MultiPolygon;
use Phaza\LaravelPostgis\Geometries\GeometryCollection;

class UnderLocaleTest extends BaseTestCase
{

public static function setUpBeforeClass()
{
setlocale(LC_NUMERIC, 'fr_FR.utf-8');
}

public static function tearDownAfterClass()
{
setlocale(LC_NUMERIC, null);
}

public function setUp()
{
if(localeconv()['decimal_point'] == '.') {
$this->markTestSkipped('The locale is not available for testing float output formatting');
}
}

public function testPointToWKT()
{
$point = new Point(1.5, 2.5);
$this->assertEquals('POINT(2.5 1.5)', $point->toWKT());
}

public function testMultiPointToWKT()
{
$multipoint = new MultiPoint([new Point(1.5, 1.5), new Point(1.5, 2.5), new Point(2.5, 2.5)]);

$this->assertEquals('MULTIPOINT((1.5 1.5),(2.5 1.5),(2.5 2.5))', $multipoint->toWKT());
}

public function testLineStringToWKT()
{
$linestring = new LineString([new Point(1.5, 1.5), new Point(2.5, 2.5), new Point(3.5, 3.5)]);

$this->assertEquals('LINESTRING(1.5 1.5,2.5 2.5,3.5 3.5)', $linestring->toWKT());
}

public function testMultiLineStringToWKT()
{
$collection = new LineString(
[
new Point(1.5, 1.5),
new Point(1.5, 2.5),
new Point(2.5, 2.5),
new Point(2.5, 1.5),
new Point(1.5, 1.5)
]
);

$multilinestring = new MultiLineString([$collection]);

$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());
}

public function testPolygonToWKT()
{
$collection = new LineString(
[
new Point(1.5, 1.5),
new Point(1.5, 2.5),
new Point(2.5, 2.5),
new Point(2.5, 1.5),
new Point(1.5, 1.5)
]
);

$polygon = new Polygon([$collection]);

$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());
}

public function testMultiPolygonToWKT()
{
$collection1 = new LineString(
[
new Point(1.5, 1.5),
new Point(1.5, 2.5),
new Point(2.5, 2.5),
new Point(2.5, 1.5),
new Point(1.5, 1.5)
]
);

$collection2 = new LineString(
[
new Point(10.5, 10.5),
new Point(10.5, 20.5),
new Point(20.5, 20.5),
new Point(20.5, 10.5),
new Point(10.5, 10.5)
]
);

$polygon1 = new Polygon([$collection1, $collection2]);

$collection3 = new LineString(
[
new Point(100.5, 100.5),
new Point(100.5, 200.5),
new Point(200.5, 200.5),
new Point(200.5, 100.5),
new Point(100.5, 100.5)
]
);

$polygon2 = new Polygon([$collection3]);

$multiPolygon = new MultiPolygon([$polygon1, $polygon2]);

$this->assertEquals(
'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)))',
$multiPolygon->toWKT()
);
}

public function testGeometryCollectionToWKT()
{
$collection = new LineString(
[
new Point(1.5, 1.5),
new Point(1.5, 2.5),
new Point(2.5, 2.5),
new Point(2.5, 1.5),
new Point(1.5, 1.5)
]
);

$point = new Point(100.5, 200.5);

$geo_collection = new GeometryCollection([$collection, $point]);

$this->assertEquals(
'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))',
$geo_collection->toWKT()
);
}

}

0 comments on commit b751206

Please sign in to comment.