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 #129 from mstaack/rework-tests
Browse files Browse the repository at this point in the history
cleanup and namespaces
  • Loading branch information
mstaack authored Mar 11, 2020
2 parents 9c417da + d065660 commit 98f9b19
Show file tree
Hide file tree
Showing 36 changed files with 215 additions and 304 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test Suite

on: [push, pull_request]
on: [push]

jobs:
tests:
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
Laravel postgis extension
=========================
Laravel Wrapper for PostgreSQL's Geo-Extension Postgis
======================================================

![Build Status](https://github.com/mstaack/laravel-postgis/workflows/Test%20Suite/badge.svg)

## Features

* Work with geometry classes instead of arrays. (`$myModel->myPoint = new Point(1,2)`)
* Adds helpers in migrations. (`$table->polygon('myColumn')`)
* Work with geometry classes instead of arrays.
```php
$model->myPoint = new Point(1,2); //lat, long
```

* Adds helpers in migrations.
```php
$table->polygon('myColumn');
```

## Versions
- Use 4.* for Laravel 5
- Use 5.* for Laravel 6/7

## Warning
This Package has been moved to a new owner and aims for Laravel 6/7 and PHP 7 support only soon!

Replace all your references to the new namespace: `MStaack\LaravelPostgis`

Thanks to :
- https://github.com/njbarrett
- https://github.com/phaza
- https://github.com/mirzap

Fluent in Laravel Packages and Postgres/Postgis? Consider contributing! We are looking for anyone that wants to help out!

## Installation

```bash
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
}
},
"autoload-dev": {
"classmap": [
"tests/BaseTestCase.php",
"tests/Stubs/"
]
"psr-4": {
"MStaack\\LaravelPostgis\\Tests\\": "tests/"
}
},
"license": "MIT",
"authors": [
Expand Down
6 changes: 2 additions & 4 deletions config/postgis.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

return [

'schema' => 'public' // Schema for the Postgis extension

];
'schema' => 'public' // Schema for the Postgis extension
];
21 changes: 10 additions & 11 deletions src/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?php namespace MStaack\LaravelPostgis;
<?php

namespace MStaack\LaravelPostgis;

use Bosnadev\Database\DatabaseServiceProvider as PostgresDatabaseServiceProvider;
use Illuminate\Database\DatabaseManager;
use MStaack\LaravelPostgis\Connectors\ConnectionFactory;
use Bosnadev\Database\DatabaseServiceProvider as PostgresDatabaseServiceProvider;

/**
* Class DatabaseServiceProvider
* @package MStaack\LaravelPostgis
*/
class DatabaseServiceProvider extends PostgresDatabaseServiceProvider
{
public function boot() {
// Load the config
$config_path = __DIR__ . '/../config/postgis.php';
$this->publishes([$config_path => config_path('postgis.php')], 'postgis');
$this->mergeConfigFrom($config_path, 'postgis');
public function boot()
{
// Load the config
$config_path = __DIR__ . '/../config/postgis.php';
$this->publishes([$config_path => config_path('postgis.php')], 'postgis');
$this->mergeConfigFrom($config_path, 'postgis');
}

/**
Expand Down
40 changes: 21 additions & 19 deletions src/Geometries/Factory.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

class Factory implements \GeoIO\Factory {
public function createPoint( $dimension, array $coordinates, $srid = null )
namespace MStaack\LaravelPostgis\Geometries;

class Factory implements \GeoIO\Factory
{
public function createPoint($dimension, array $coordinates, $srid = null)
{
return new Point( $coordinates['y'], $coordinates['x'], isset($coordinates['z']) ? $coordinates['z'] : null );
return new Point($coordinates['y'], $coordinates['x'], $coordinates['z'] ?? null);
}

public function createLineString( $dimension, array $points, $srid = null )
public function createLineString($dimension, array $points, $srid = null)
{
return new LineString( $points );
return new LineString($points);
}

public function createLinearRing( $dimension, array $points, $srid = null )
public function createLinearRing($dimension, array $points, $srid = null)
{
return new LineString( $points );
return new LineString($points);
}

public function createPolygon( $dimension, array $lineStrings, $srid = null )
public function createPolygon($dimension, array $lineStrings, $srid = null)
{
return new Polygon( $lineStrings );
return new Polygon($lineStrings);
}

public function createMultiPoint( $dimension, array $points, $srid = null )
public function createMultiPoint($dimension, array $points, $srid = null)
{
return new MultiPoint( $points );
return new MultiPoint($points);
}

public function createMultiLineString( $dimension, array $lineStrings, $srid = null )
public function createMultiLineString($dimension, array $lineStrings, $srid = null)
{
return new MultiLineString( $lineStrings );
return new MultiLineString($lineStrings);
}

public function createMultiPolygon( $dimension, array $polygons, $srid = null )
public function createMultiPolygon($dimension, array $polygons, $srid = null)
{
return new MultiPolygon( $polygons );
return new MultiPolygon($polygons);
}

public function createGeometryCollection( $dimension, array $geometries, $srid = null )
public function createGeometryCollection($dimension, array $geometries, $srid = null)
{
return new GeometryCollection( $geometries );
return new GeometryCollection($geometries);
}

}
4 changes: 3 additions & 1 deletion src/Geometries/Geometry.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

namespace MStaack\LaravelPostgis\Geometries;

use GeoIO\WKB\Parser\Parser;
use MStaack\LaravelPostgis\Exceptions\UnknownWKTTypeException;
Expand Down
5 changes: 3 additions & 2 deletions src/Geometries/GeometryCollection.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

namespace MStaack\LaravelPostgis\Geometries;

use Countable;
use GeoJson\GeoJson;
use InvalidArgumentException;

class GeometryCollection extends Geometry implements Countable
Expand Down
4 changes: 3 additions & 1 deletion src/Geometries/GeometryInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

namespace MStaack\LaravelPostgis\Geometries;

interface GeometryInterface
{
Expand Down
8 changes: 5 additions & 3 deletions src/Geometries/LineString.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

namespace MStaack\LaravelPostgis\Geometries;

class LineString extends PointCollection implements GeometryInterface
{
public function is3d()
{
if(count($this->points) === 0) return false;
if (count($this->points) === 0) return false;
return $this->points[0]->is3d();
}

public function toWKT()
{
$wktType = 'LINESTRING';
if($this->is3d()) $wktType .= ' Z';
if ($this->is3d()) $wktType .= ' Z';
return sprintf('%s(%s)', $wktType, $this->toPairList());
}

Expand Down
8 changes: 5 additions & 3 deletions src/Geometries/MultiLineString.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

namespace MStaack\LaravelPostgis\Geometries;

use Countable;
use InvalidArgumentException;
Expand Down Expand Up @@ -37,14 +39,14 @@ public function getLineStrings()

public function is3d()
{
if(count($this->linestrings) === 0) return false;
if (count($this->linestrings) === 0) return false;
return $this->linestrings[0]->is3d();
}

public function toWKT()
{
$wktType = 'MULTILINESTRING';
if($this->is3d()) $wktType .= ' Z';
if ($this->is3d()) $wktType .= ' Z';
return sprintf('%s(%s)', $wktType, (string)$this);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Geometries/MultiPoint.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

namespace MStaack\LaravelPostgis\Geometries;

class MultiPoint extends PointCollection implements GeometryInterface, \JsonSerializable
{
Expand All @@ -21,14 +23,14 @@ public function __construct(array $points)

public function is3d()
{
if(count($this->points) === 0) return false;
if (count($this->points) === 0) return false;
return $this->points[0]->is3d();
}

public function toWKT()
{
$wktType = 'MULTIPOINT';
if($this->is3d()) $wktType .= ' Z';
if ($this->is3d()) $wktType .= ' Z';
return sprintf('%s(%s)', $wktType, (string)$this);
}

Expand Down
16 changes: 9 additions & 7 deletions src/Geometries/MultiPolygon.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

namespace MStaack\LaravelPostgis\Geometries;

use Countable;
use InvalidArgumentException;
Expand Down Expand Up @@ -27,27 +29,27 @@ public function __construct(array $polygons)

public function is3d()
{
if(count($this->polygons) === 0) return false;
if (count($this->polygons) === 0) return false;
return $this->polygons[0]->is3d();
}

public function toWKT()
{
$wktType = 'MULTIPOLYGON';
if($this->is3d()) $wktType .= ' Z';
return sprintf('%s(%s)', $wktType, (string) $this);
if ($this->is3d()) $wktType .= ' Z';
return sprintf('%s(%s)', $wktType, (string)$this);
}

public function __toString()
{
return implode(',', array_map(function (Polygon $polygon) {
return sprintf('(%s)', (string) $polygon);
return sprintf('(%s)', (string)$polygon);
}, $this->polygons));
}

public static function fromString($wktArgument)
{
$parts = preg_split('/(\)\s*\)\s*,\s*\(\s*\()/', $wktArgument, -1, PREG_SPLIT_DELIM_CAPTURE);
$parts = preg_split('/(\)\s*\)\s*,\s*\(\s*\()/', $wktArgument, -1, PREG_SPLIT_DELIM_CAPTURE);
$polygons = static::assembleParts($parts);

return new static(array_map(function ($polygonString) {
Expand Down Expand Up @@ -99,7 +101,7 @@ public function getPolygons()
protected static function assembleParts(array $parts)
{
$polygons = [];
$count = count($parts);
$count = count($parts);

for ($i = 0; $i < $count; $i++) {
if ($i % 2 !== 0) {
Expand Down
12 changes: 6 additions & 6 deletions src/Geometries/Point.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

use GeoJson\GeoJson;
namespace MStaack\LaravelPostgis\Geometries;

class Point extends Geometry
{
Expand Down Expand Up @@ -53,7 +53,7 @@ public function is3d()
public function toPair()
{
$pair = self::stringifyFloat($this->getLng()) . ' ' . self::stringifyFloat($this->getLat());
if($this->is3d()) {
if ($this->is3d()) {
$pair .= ' ' . self::stringifyFloat($this->getAlt());
}
return $pair;
Expand All @@ -71,7 +71,7 @@ public static function fromPair($pair)
$splits = explode(' ', trim($pair));
$lng = $splits[0];
$lat = $splits[1];
if(count($splits) > 2) {
if (count($splits) > 2) {
$alt = $splits[2];
}

Expand All @@ -81,7 +81,7 @@ public static function fromPair($pair)
public function toWKT()
{
$wktType = 'POINT';
if($this->is3d()) $wktType .= ' Z';
if ($this->is3d()) $wktType .= ' Z';
return sprintf('%s(%s)', $wktType, (string)$this);
}

Expand All @@ -103,7 +103,7 @@ public function __toString()
public function jsonSerialize()
{
$position = [$this->getLng(), $this->getLat()];
if($this->is3d()) $position[] = $this->getAlt();
if ($this->is3d()) $position[] = $this->getAlt();
return new \GeoJson\Geometry\Point($position);
}
}
4 changes: 3 additions & 1 deletion src/Geometries/PointCollection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace MStaack\LaravelPostgis\Geometries;
<?php

namespace MStaack\LaravelPostgis\Geometries;

use ArrayAccess;
use ArrayIterator;
Expand Down
Loading

0 comments on commit 98f9b19

Please sign in to comment.