From 4f3e112de7de9b8e7c687892b47e45d4906747a3 Mon Sep 17 00:00:00 2001 From: gpedro Date: Mon, 2 Mar 2020 23:34:52 -0400 Subject: [PATCH 1/8] fix: prevent wrong formatting float values by locale --- composer.json | 1 + src/Types/Point.php | 11 ++- tests/Unit/Types/PointLocaleTest.php | 105 +++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Types/PointLocaleTest.php diff --git a/composer.json b/composer.json index 3c22d1c2..49fec7f8 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "php": ">=5.5", "ext-pdo": "*", "ext-json": "*", + "ext-intl": "*", "illuminate/database": "^5.2|^6.0", "geo-io/wkb-parser": "^1.0", "jmikola/geojson": "^1.0" diff --git a/src/Types/Point.php b/src/Types/Point.php index 40719af4..707ca732 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -2,6 +2,7 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +use NumberFormatter; use GeoJson\GeoJson; use GeoJson\Geometry\Point as GeoJsonPoint; use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; @@ -40,7 +41,13 @@ public function setLng($lng) public function toPair() { - return $this->getLng().' '.$this->getLat(); + $formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL); + $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, -1); + + $lng = $formatter->format($this->getLng()); + $lat = $formatter->format($this->getLat()); + + return $lng . ' ' . $lat; } public static function fromPair($pair) @@ -62,7 +69,7 @@ public static function fromString($wktArgument) public function __toString() { - return $this->getLng().' '.$this->getLat(); + return $this->toPair(); } /** diff --git a/tests/Unit/Types/PointLocaleTest.php b/tests/Unit/Types/PointLocaleTest.php new file mode 100644 index 00000000..e62aa70a --- /dev/null +++ b/tests/Unit/Types/PointLocaleTest.php @@ -0,0 +1,105 @@ +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)); + } +} From 0f0ec2673b1a9433e746059f1002de08fa3b692d Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Tue, 3 Mar 2020 02:31:43 -0500 Subject: [PATCH 2/8] Fix build --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9973c293..22ee6ce7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,7 +19,7 @@ services: - docker before_install: - - echo "memory_limit=2G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini + - echo "memory_limit=3G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - sudo /etc/init.d/mysql stop - make start_db V=$MYSQL_VERSION From b063f8cf50ed157f6dc51cc6991a70633a05d918 Mon Sep 17 00:00:00 2001 From: Gabriel Pedro Date: Tue, 3 Mar 2020 07:12:29 -0400 Subject: [PATCH 3/8] fix: travis --- src/Types/Point.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Types/Point.php b/src/Types/Point.php index 707ca732..2a186d83 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -42,7 +42,7 @@ public function setLng($lng) public function toPair() { $formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL); - $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, -1); + $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 12); $lng = $formatter->format($this->getLng()); $lat = $formatter->format($this->getLat()); From 7f37555314a16a01be72bab68d26726f0a2961d4 Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Fri, 6 Mar 2020 13:19:41 -0500 Subject: [PATCH 4/8] Replaced ext-intl with symfony/polyfill-intl-icu --- .travis.yml | 1 + composer.json | 5 ++--- src/Types/Point.php | 16 +++++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 22ee6ce7..95a08c02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,7 @@ before_install: install: composer install before_script: + - phpenv config-rm intl.ini - 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 diff --git a/composer.json b/composer.json index 49fec7f8..83df2aa3 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,11 @@ ], "require": { "php": ">=5.5", - "ext-pdo": "*", "ext-json": "*", - "ext-intl": "*", "illuminate/database": "^5.2|^6.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", diff --git a/src/Types/Point.php b/src/Types/Point.php index 2a186d83..3172575c 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -13,10 +13,15 @@ class Point extends Geometry 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() @@ -41,11 +46,8 @@ public function setLng($lng) public function toPair() { - $formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL); - $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 12); - - $lng = $formatter->format($this->getLng()); - $lat = $formatter->format($this->getLat()); + $lng = $this->rtrimCoordinate($this->formatter->format($this->getLng())); + $lat = $this->rtrimCoordinate($this->formatter->format($this->getLat())); return $lng . ' ' . $lat; } @@ -101,4 +103,8 @@ public function jsonSerialize() { return new GeoJsonPoint([$this->getLng(), $this->getLat()]); } + + private function rtrimCoordinate($coordinate) { + return rtrim(rtrim($coordinate, '0'), '.'); + } } From 671c5e311a802a5d009617a7eb76d745d79f26c3 Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Fri, 6 Mar 2020 18:20:16 +0000 Subject: [PATCH 5/8] Apply fixes from StyleCI [ci skip] [skip ci] --- src/Eloquent/SpatialTrait.php | 4 +++- src/Types/Point.php | 7 ++++--- tests/Unit/Types/PointLocaleTest.php | 4 +--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Eloquent/SpatialTrait.php b/src/Eloquent/SpatialTrait.php index c1bf101c..e08a476d 100755 --- a/src/Eloquent/SpatialTrait.php +++ b/src/Eloquent/SpatialTrait.php @@ -75,7 +75,9 @@ protected function newBaseQueryBuilder() $connection = $this->getConnection(); return new BaseBuilder( - $connection, $connection->getQueryGrammar(), $connection->getPostProcessor() + $connection, + $connection->getQueryGrammar(), + $connection->getPostProcessor() ); } diff --git a/src/Types/Point.php b/src/Types/Point.php index 3172575c..450c9ffc 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -2,10 +2,10 @@ namespace Grimzy\LaravelMysqlSpatial\Types; -use NumberFormatter; use GeoJson\GeoJson; use GeoJson\Geometry\Point as GeoJsonPoint; use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; +use NumberFormatter; class Point extends Geometry { @@ -49,7 +49,7 @@ public function toPair() $lng = $this->rtrimCoordinate($this->formatter->format($this->getLng())); $lat = $this->rtrimCoordinate($this->formatter->format($this->getLat())); - return $lng . ' ' . $lat; + return $lng.' '.$lat; } public static function fromPair($pair) @@ -104,7 +104,8 @@ public function jsonSerialize() return new GeoJsonPoint([$this->getLng(), $this->getLat()]); } - private function rtrimCoordinate($coordinate) { + private function rtrimCoordinate($coordinate) + { return rtrim(rtrim($coordinate, '0'), '.'); } } diff --git a/tests/Unit/Types/PointLocaleTest.php b/tests/Unit/Types/PointLocaleTest.php index e62aa70a..e6ed802a 100644 --- a/tests/Unit/Types/PointLocaleTest.php +++ b/tests/Unit/Types/PointLocaleTest.php @@ -4,7 +4,6 @@ class PointLocaleTest extends BaseTestCase { - public function setUp() { parent::setUp(); @@ -19,7 +18,6 @@ public function tearDown() setlocale(LC_ALL, 'en_US.UTF-8'); } - public function testFromWKT() { $point = Point::fromWKT('POINT(1 2)'); @@ -58,7 +56,7 @@ public function testPairLocale() $this->assertSame(2.0, $point->getLat()); $this->assertSame('1.5 2', $point->toPair()); - setlocale(LC_ALL, "en-US.UTF-8"); + setlocale(LC_ALL, 'en-US.UTF-8'); } public function testPair() From ede1c58b19de70931a9fd529cd0cce9b9f2d159e Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Sun, 8 Mar 2020 21:17:46 -0400 Subject: [PATCH 6/8] Fix build + debug steps --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 95a08c02..901facf7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,9 @@ before_install: install: composer install before_script: - - phpenv config-rm intl.ini + - php -m + - phpenv config-rm intl.ini || "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 From 66653cdd12c3133d1c969cd379508b8d1a771b07 Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Sun, 8 Mar 2020 21:44:24 -0400 Subject: [PATCH 7/8] Build fix --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 901facf7..66c21290 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,8 +26,9 @@ before_install: install: composer install before_script: - - php -m - - phpenv config-rm intl.ini || "intl PHP extension not available" + - 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 From 424a97fff754b0cef7323ac63861de00a1b068a0 Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Sun, 8 Mar 2020 21:45:20 -0400 Subject: [PATCH 8/8] Added colon to rtrimCoordinate filter --- .travis.yml | 2 +- src/Types/Point.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 66c21290..978c87e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,7 @@ before_install: install: composer install before_script: - - php --m + - php -m - php --ini - phpenv config-rm intl.ini || echo "intl PHP extension not available" - php -m diff --git a/src/Types/Point.php b/src/Types/Point.php index 450c9ffc..49652ee5 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -106,6 +106,6 @@ public function jsonSerialize() private function rtrimCoordinate($coordinate) { - return rtrim(rtrim($coordinate, '0'), '.'); + return rtrim(rtrim($coordinate, '0'), '.,'); } }