Created
April 2, 2014 14:03
-
-
Save vespakoen/9934819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Spatial; | |
class Extension extends \Bolt\BaseExtension | |
{ | |
function info() | |
{ | |
$data = array( | |
'name' => "Add spatial support", | |
'description' => "Adds spatial support to bolt", | |
'keywords' => "Spatial", | |
'author' => "Koen Schmeets", | |
'link' => "http://www.koenschmeets.nl", | |
'version' => "0.1", | |
'required_bolt_version' => "1.0.2", | |
'highest_bolt_version' => "1.2", | |
'type' => "General", | |
'first_releasedate' => "2014-03-30", | |
'latest_releasedate' => "2014-03-30", | |
'dependencies' => "", | |
'priority' => 1 | |
); | |
return $data; | |
} | |
function initialize() | |
{ | |
$this->registerAutoloader(); | |
$this->registerFields(); | |
$this->registerViewPath(); | |
// $this->registerAssets(); | |
} | |
function registerFields() | |
{ | |
$this->addField('linestring', 'Spatial\Fields\GeometryField'); | |
$this->addField('point', 'Spatial\Fields\GeometryField'); | |
} | |
// Somehow, addCSS and addJavascript don't seem to work | |
// | |
// function registerAssets() | |
// { | |
// $this->addCSS('assets/leaflet/dist/leaflet.css'); | |
// $this->addJavascript('assets/leaflet/dist/leaflet.js'); | |
// $this->addCSS('assets/leaflet-draw/dist/leaflet.draw.css'); | |
// $this->addJavascript('assets/leaflet-draw/dist/leaflet.draw.js'); | |
// } | |
function registerAutoloader() | |
{ | |
// NOTE THAT I PUT THE COMPOSER AUTOLOADER ON THE CONTAINER | |
$this->app['autoloader']->add('Spatial\\', __DIR__ . '/src/'); | |
} | |
function registerViewPath() | |
{ | |
$this->app['twig.loader.filesystem']->addPath(__DIR__.'/views/'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Spatial\Fields; | |
use Spatial\GeometryParser; | |
class GeometryField extends \Bolt\Field { | |
protected $doctrineType = 'CrEOF\Spatial\DBAL\Types\GeometryType'; | |
public function serialize($value) | |
{ | |
$parser = GeometryParser::fromGeoJSON($value); | |
return $parser->asSql(); | |
} | |
public function unserialize($value) | |
{ | |
if(empty($value) || substr($value, 0, 1) == "{") | |
{ | |
return $value; | |
} | |
$parser = GeometryParser::fromBinary($value); | |
return $parser->asGeoJSON(); | |
} | |
public function validate($value) | |
{ | |
$flashBag = $this->app['session']->getFlashBag(); | |
// if( ! $data = @json_decode($value)) | |
// $flashBag->set('error', 'Incorrect data: Unable to parse JSON ('.$value.')'); | |
// return false; | |
// } | |
if( ! $data->type) | |
{ | |
$flashBag->set('error', 'Incorrect data: No \'type\' found in GeoJSON ('.json_encode($data).')'); | |
return false; | |
} | |
if( ! $data->coordinates) | |
{ | |
$flashBag->set('error', 'Incorrect data: No \'coordinates\' found in GeoJSON ('.json_encode($data).')'); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment