Skip to content

Commit

Permalink
Add MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
tontonsb committed May 25, 2024
1 parent 5ebf187 commit b8258c9
Show file tree
Hide file tree
Showing 18 changed files with 514 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Changelog
- PHP 8.2 support
- MySQL support

## [0.1.0] - 2022-07-22
### Added
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<directory suffix="Test.php">./tests/Geometry</directory>
<directory suffix="Test.php">./tests/Feature</directory>
<directory suffix="Test.php">./tests/Constructors</directory>
<directory suffix="Test.php">./tests/MySQL</directory>
<directory suffix="Test.php">./tests/PostGIS</directory>
<directory suffix="Test.php">./tests/SpatiaLite</directory>
<directory suffix="Test.php">./tests/SQL</directory>
Expand Down
20 changes: 20 additions & 0 deletions src/MySQL/Curve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\Expression;
use GlaivePro\SF\OGC\Contracts\Curve as OGCCurve;

/**
* Curve model with MySQL-specific functions.
*/
class Curve extends Geometry implements OGCCurve
{
use \GlaivePro\SF\OGC\Traits\Curve;

public function isRing(): Expression // Boolean-valued expression
{
throw new MethodNotImplemented;
}
}
40 changes: 40 additions & 0 deletions src/MySQL/Geometry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\Expression;
use GlaivePro\SF\OGC\Geometry as OGCGeometry;

/**
* Geometry model with MySQL-specific functions.
*/
class Geometry extends OGCGeometry
{
protected const sfc = Sfc::class;

public function setSRID(int $srid): static
{
return static::fromMethod('ST_SRID', $this, $srid);
}

public function coordinateDimension(): Expression // Integer-valued expression
{
throw new MethodNotImplemented;
}

public function is3D(): Expression // Boolean-valued expression
{
throw new MethodNotImplemented;
}

public function isMeasured(): Expression // Boolean-valued expression
{
throw new MethodNotImplemented;
}

public function boundary(): \GlaivePro\SF\OGC\Contracts\Geometry
{
throw new MethodNotImplemented;
}
}
13 changes: 13 additions & 0 deletions src/MySQL/GeometryCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\OGC\Contracts\GeometryCollection as OGCGeometryCollection;

/**
* GeometryCollection model with MySQL-specific functions.
*/
class GeometryCollection extends Geometry implements OGCGeometryCollection
{
use \GlaivePro\SF\OGC\Traits\GeometryCollection;
}
13 changes: 13 additions & 0 deletions src/MySQL/LineString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\OGC\Contracts\LineString as OGCLineString;

/**
* LineString model with MySQL-specific functions.
*/
class LineString extends Curve implements OGCLineString
{
use \GlaivePro\SF\OGC\Traits\LineString;
}
13 changes: 13 additions & 0 deletions src/MySQL/MultiCurve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\OGC\Contracts\MultiCurve as OGCMultiCurve;

/**
* MultiCurve model with MySQL-specific functions.
*/
class MultiCurve extends GeometryCollection implements OGCMultiCurve
{
use \GlaivePro\SF\OGC\Traits\CurveBasic;
}
10 changes: 10 additions & 0 deletions src/MySQL/MultiLineString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\OGC\Contracts\MultiLineString as OGCMultiLineString;

/**
* MultiLineString model with MySQL-specific functions.
*/
class MultiLineString extends MultiCurve implements OGCMultiLineString {}
10 changes: 10 additions & 0 deletions src/MySQL/MultiPoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\OGC\Contracts\MultiPoint as OGCMultiPoint;

/**
* MultiPoint model with MySQL-specific functions.
*/
class MultiPoint extends GeometryCollection implements OGCMultiPoint {}
10 changes: 10 additions & 0 deletions src/MySQL/MultiPolygon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\OGC\Contracts\MultiPolygon as OGCMultiPolygon;

/**
* MultiPolygon model with MySQL-specific functions.
*/
class MultiPolygon extends MultiSurface implements OGCMultiPolygon {}
19 changes: 19 additions & 0 deletions src/MySQL/MultiSurface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\OGC\Contracts\MultiSurface as OGCMultiSurface;

/**
* MultiSurface model with MySQL-specific functions.
*/
class MultiSurface extends GeometryCollection implements OGCMultiSurface
{
use \GlaivePro\SF\OGC\Traits\SurfaceBasic;

public function pointOnSurface(): \GlaivePro\SF\OGC\Contracts\Point
{
throw new MethodNotImplemented();
}
}
25 changes: 25 additions & 0 deletions src/MySQL/Point.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\Expression;
use GlaivePro\SF\OGC\Contracts\Point as OGCPoint;

/**
* Point model with MySQL-specific functions.
*/
class Point extends Geometry implements OGCPoint
{
use \GlaivePro\SF\OGC\Traits\Point;

public function Z(): Expression
{
throw new MethodNotImplemented;
}

public function M(): Expression
{
throw new MethodNotImplemented();
}
}
14 changes: 14 additions & 0 deletions src/MySQL/Polygon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\Expression;
use GlaivePro\SF\OGC\Contracts\Polygon as OGCPolygon;

/**
* Polygon model with MySQL-specific functions.
*/
class Polygon extends Surface implements OGCPolygon
{
use \GlaivePro\SF\OGC\Traits\Polygon;
}
84 changes: 84 additions & 0 deletions src/MySQL/Sfc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\Expression;
use GlaivePro\SF\OGC\Sfc as OGCSfc;

/**
* Implements constructors available in MySQL.
*/
class Sfc extends OGCSfc
{
/**
* Calls `fromMethod` on the desired class.
*
* We have to specify PostGIS-specific classes here.
*/
protected static function callFromMethod(string $method, array $args): Geometry
{
$class = match ($method) {
'geometry' => Geometry::class,
'geometryCollection' => GeometryCollection::class,
'lineString' => LineString::class,
'multiCurve' => MultiCurve::class,
'multiLineString' => MultiLineString::class,
'multiPoint' => MultiPoint::class,
'multiPolygon' => MultiPolygon::class,
'point' => Point::class,
'polygon' => Polygon::class,
};

return $class::fromMethod(...$args);
}

/**
* Create a Point.
*
* Unlike POINT in MySQL, this also accepts POINT(x, y, srid)
* and will set the SRID on the created POINT in such case.
*/
public static function point(float $x, float $y, int $srid = null): Point
{
$point = Point::fromMethod('Point', $x, $y);

if (\is_null($srid))
return $point;

return $point->setSRID($srid);
}

/**
* Not implemented in MySQL.
*/
public static function bdPolyFromWKB(string $WKBMultiLineString, int $SRID = null): Polygon
{
throw new MethodNotImplemented;
}

/**
* Not implemented in MySQL.
*/
public static function bdPolyFromText(string $multiLineStringTaggedText, int $SRID = null): Polygon
{
throw new MethodNotImplemented;
}

/**
* Not implemented in MySQL.
*/
public static function bdMPolyFromWKB(string $WKBMultiLineString, int $SRID = null): MultiPolygon
{
throw new MethodNotImplemented;
}

/**
* Not implemented in MySQL.
*/
public static function bdMPolyFromText(string $multiLineStringTaggedText, int $SRID = null): MultiPolygon
{
throw new MethodNotImplemented;
}

}
19 changes: 19 additions & 0 deletions src/MySQL/Surface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace GlaivePro\SF\MySQL;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\OGC\Contracts\Surface as OGCSurface;

/**
* Surface model with MySQL-specific functions.
*/
class Surface extends Geometry implements OGCSurface
{
use \GlaivePro\SF\OGC\Traits\Surface;

public function pointOnSurface(): \GlaivePro\SF\OGC\Contracts\Point
{
throw new MethodNotImplemented;
}
}
26 changes: 26 additions & 0 deletions tests/MySQL/ConstructorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace GlaivePro\SF\Tests\MySQL;

use PHPUnit\Framework\TestCase;
use GlaivePro\SF\MySQL\Sfc;

class ConstructorTest extends TestCase
{
public function testPointConstructors(): void
{
$point = Sfc::point(1, 3);
$this->assertSame(
'Point(?, ?)',
(string) $point,
);
$this->assertEquals([1, 3], $point->bindings);

$pointWithSrid = Sfc::point(1, 3, 4326);
$this->assertSame(
'ST_SRID(Point(?, ?), ?)',
(string) $pointWithSrid,
);
$this->assertEquals([1, 3, 4326], $pointWithSrid->bindings);
}
}
Loading

0 comments on commit b8258c9

Please sign in to comment.