diff --git a/lib/CrEOF/Spatial/DBAL/Platform/MySql80.php b/lib/CrEOF/Spatial/DBAL/Platform/MySql80.php new file mode 100644 index 00000000..dfd12ff4 --- /dev/null +++ b/lib/CrEOF/Spatial/DBAL/Platform/MySql80.php @@ -0,0 +1,57 @@ + + * @license http://dlambert.mit-license.org MIT + */ +class MySql80 extends MySql +{ + /** + * @param AbstractSpatialType $type + * @param string $sqlExpr + * + * @return string + */ + public function convertToPHPValueSQL(AbstractSpatialType $type, $sqlExpr) + { + return sprintf('ST_AsBinary(%s)', $sqlExpr); + } + + /** + * @param AbstractSpatialType $type + * @param string $sqlExpr + * + * @return string + */ + public function convertToDatabaseValueSQL(AbstractSpatialType $type, $sqlExpr) + { + return sprintf('ST_GeomFromText(%s)', $sqlExpr); + } +} diff --git a/lib/CrEOF/Spatial/DBAL/Types/AbstractSpatialType.php b/lib/CrEOF/Spatial/DBAL/Types/AbstractSpatialType.php index fd36d15b..8e642521 100644 --- a/lib/CrEOF/Spatial/DBAL/Types/AbstractSpatialType.php +++ b/lib/CrEOF/Spatial/DBAL/Types/AbstractSpatialType.php @@ -23,12 +23,18 @@ namespace CrEOF\Spatial\DBAL\Types; +use CrEOF\Spatial\DBAL\Platform\MySql; +use CrEOF\Spatial\DBAL\Platform\MySql80; +use CrEOF\Spatial\DBAL\Platform\PlatformInterface; +use CrEOF\Spatial\DBAL\Platform\PostgreSql; use CrEOF\Spatial\Exception\InvalidValueException; use CrEOF\Spatial\Exception\UnsupportedPlatformException; -use CrEOF\Spatial\DBAL\Platform\PlatformInterface; use CrEOF\Spatial\PHP\Types\Geography\GeographyInterface; use CrEOF\Spatial\PHP\Types\Geometry\GeometryInterface; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Platforms\MySQL80Platform; +use Doctrine\DBAL\Platforms\MySQLPlatform; +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Types\Type; /** @@ -39,9 +45,6 @@ */ abstract class AbstractSpatialType extends Type { - const PLATFORM_MYSQL = 'MySql'; - const PLATFORM_POSTGRESQL = 'PostgreSql'; - /** * @return string */ @@ -201,13 +204,19 @@ public function requiresSQLCommentHint(AbstractPlatform $platform) */ private function getSpatialPlatform(AbstractPlatform $platform) { - $const = sprintf('self::PLATFORM_%s', strtoupper($platform->getName())); - - if (! defined($const)) { - throw new UnsupportedPlatformException(sprintf('DBAL platform "%s" is not currently supported.', $platform->getName())); + $class = null; + + if ($platform instanceof MySQL80Platform) { + $class = MySql80::class; + } elseif ($platform instanceof MySQLPlatform) { + $class = MySql::class; + } elseif ($platform instanceof PostgreSQLPlatform) { + $class = PostgreSql::class; } - $class = sprintf('CrEOF\Spatial\DBAL\Platform\%s', constant($const)); + if (!$class) { + throw new UnsupportedPlatformException(sprintf('DBAL platform "%s" is not currently supported.', get_class($platform))); + } return new $class; }