Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/locale polyfill #126

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ before_install:
install: composer install

before_script:
- php -m
- php --ini
- phpenv config-rm intl.ini || echo "intl PHP extension not available"
- php -m
- mkdir -p build/logs
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"ext-json": "*",
"illuminate/database": "^5.2|^6.0|^7.0",
"geo-io/wkb-parser": "^1.0",
"jmikola/geojson": "^1.0"
"jmikola/geojson": "^1.0",
"symfony/polyfill-intl-icu": "^1.14"
},
"require-dev": {
"phpunit/phpunit": "~4.8|~5.7",
Expand Down
18 changes: 16 additions & 2 deletions src/Types/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
use GeoJson\GeoJson;
use GeoJson\Geometry\Point as GeoJsonPoint;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
use NumberFormatter;

class Point extends Geometry
{
protected $lat;

protected $lng;

private $formatter;

public function __construct($lat, $lng)
{
$this->lat = (float) $lat;
$this->lng = (float) $lng;

$this->formatter = new NumberFormatter('en', NumberFormatter::DECIMAL);
$this->formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 12);
}

public function getLat()
Expand All @@ -40,7 +46,10 @@ public function setLng($lng)

public function toPair()
{
return $this->getLng().' '.$this->getLat();
$lng = $this->rtrimCoordinate($this->formatter->format($this->getLng()));
$lat = $this->rtrimCoordinate($this->formatter->format($this->getLat()));

return $lng.' '.$lat;
}

public static function fromPair($pair)
Expand All @@ -62,7 +71,7 @@ public static function fromString($wktArgument)

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

/**
Expand Down Expand Up @@ -94,4 +103,9 @@ public function jsonSerialize()
{
return new GeoJsonPoint([$this->getLng(), $this->getLat()]);
}

private function rtrimCoordinate($coordinate)
{
return rtrim(rtrim($coordinate, '0'), '.,');
}
}
103 changes: 103 additions & 0 deletions tests/Unit/Types/PointLocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

use Grimzy\LaravelMysqlSpatial\Types\Point;

class PointLocaleTest extends BaseTestCase
{
public function setUp()
{
parent::setUp();

setlocale(LC_ALL, 'pt_BR.UTF-8');
}

public function tearDown()
{
parent::setUp();

setlocale(LC_ALL, 'en_US.UTF-8');
}

public function testFromWKT()
{
$point = Point::fromWKT('POINT(1 2)');

$this->assertInstanceOf(Point::class, $point);
$this->assertEquals(2, $point->getLat());
$this->assertEquals(1, $point->getLng());
}

public function testToWKT()
{
$point = new Point(1, 2);

$this->assertEquals('POINT(2 1)', $point->toWKT());
}

public function testGettersAndSetters()
{
$point = new Point(1, 2);
$this->assertSame(1.0, $point->getLat());
$this->assertSame(2.0, $point->getLng());

$point->setLat('3');
$point->setLng('4');

$this->assertSame(3.0, $point->getLat());
$this->assertSame(4.0, $point->getLng());
}

public function testPairLocale()
{
setlocale(LC_ALL, 'pt_BR.UTF-8');
$point = Point::fromPair('1.5 2');

$this->assertSame(1.5, $point->getLng());
$this->assertSame(2.0, $point->getLat());

$this->assertSame('1.5 2', $point->toPair());
setlocale(LC_ALL, 'en-US.UTF-8');
}

public function testPair()
{
$point = Point::fromPair('1.5 2');

$this->assertSame(1.5, $point->getLng());
$this->assertSame(2.0, $point->getLat());

$this->assertSame('1.5 2', $point->toPair());
}

public function testToString()
{
$point = Point::fromString('1.3 2');

$this->assertSame(1.3, $point->getLng());
$this->assertSame(2.0, $point->getLat());

$this->assertEquals('1.3 2', (string) $point);
}

public function testFromJson()
{
$point = Point::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
$this->assertInstanceOf(Point::class, $point);
$this->assertEquals(1.2, $point->getLat());
$this->assertEquals(3.4, $point->getLng());
}

public function testInvalidGeoJsonException()
{
$this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class);
Point::fromJson('{"type": "LineString","coordinates":[[1,1],[2,2]]}');
}

public function testJsonSerialize()
{
$point = new Point(1.2, 3.4);

$this->assertInstanceOf(\GeoJson\Geometry\Point::class, $point->jsonSerialize());
$this->assertSame('{"type":"Point","coordinates":[3.4,1.2]}', json_encode($point));
}
}