From ca4e4e0d2d50d8bc203110a686870cb013523ad9 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Thu, 6 Oct 2016 09:42:41 +0200 Subject: [PATCH] Validate aliases. --- module/config/services.php | 14 +++ module/dca/tl_leaflet_control.php | 1 + module/dca/tl_leaflet_icon.php | 1 + module/dca/tl_leaflet_layer.php | 1 + module/dca/tl_leaflet_map.php | 1 + module/dca/tl_leaflet_marker.php | 1 + module/dca/tl_leaflet_popup.php | 1 + module/dca/tl_leaflet_style.php | 1 + module/dca/tl_leaflet_vector.php | 1 + module/languages/en/leaflet.php | 2 + .../Contao/Leaflet/Dca/Validator.php | 115 ++++++++++++++++++ 11 files changed, 139 insertions(+) create mode 100644 src/Netzmacht/Contao/Leaflet/Dca/Validator.php diff --git a/module/config/services.php b/module/config/services.php index ba823f5..638189e 100644 --- a/module/config/services.php +++ b/module/config/services.php @@ -17,6 +17,7 @@ use Netzmacht\Contao\Leaflet\Dca\ControlCallbacks; use Netzmacht\Contao\Leaflet\Dca\FrontendIntegration; use Netzmacht\Contao\Leaflet\Dca\LayerCallbacks; use Netzmacht\Contao\Leaflet\Dca\MapCallbacks; +use Netzmacht\Contao\Leaflet\Dca\Validator; use Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices; use Netzmacht\Contao\Leaflet\Frontend\MapElement; use Netzmacht\Contao\Leaflet\Frontend\MapModule; @@ -226,6 +227,19 @@ $container['leaflet.dca.frontend-integration'] = $container->share( } ); +/** + * Validator helper class. + * + * @return Validator + */ +$container['leaflet.dca.validator'] = $container->share( + function ($container) { + return new Validator( + $container[Services::TRANSLATOR] + ); + } +); + /** * Component factory for content element. * diff --git a/module/dca/tl_leaflet_control.php b/module/dca/tl_leaflet_control.php index e8ea7f5..0f2655d 100644 --- a/module/dca/tl_leaflet_control.php +++ b/module/dca/tl_leaflet_control.php @@ -169,6 +169,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_control'] = array ['title'], \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), 'sql' => "varchar(255) NOT NULL default ''" diff --git a/module/dca/tl_leaflet_icon.php b/module/dca/tl_leaflet_icon.php index c2cecaf..3b30655 100644 --- a/module/dca/tl_leaflet_icon.php +++ b/module/dca/tl_leaflet_icon.php @@ -180,6 +180,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_icon'] = array ['title'], \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), 'sql' => "varchar(255) NOT NULL default ''" diff --git a/module/dca/tl_leaflet_layer.php b/module/dca/tl_leaflet_layer.php index e9c9cf7..3e4a747 100644 --- a/module/dca/tl_leaflet_layer.php +++ b/module/dca/tl_leaflet_layer.php @@ -267,6 +267,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_layer'] = array ['title'], \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), 'sql' => "varchar(255) NOT NULL default ''" diff --git a/module/dca/tl_leaflet_map.php b/module/dca/tl_leaflet_map.php index ea63030..c41838e 100644 --- a/module/dca/tl_leaflet_map.php +++ b/module/dca/tl_leaflet_map.php @@ -169,6 +169,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array ['title'], \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias') ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), 'sql' => "varchar(255) NOT NULL default ''" diff --git a/module/dca/tl_leaflet_marker.php b/module/dca/tl_leaflet_marker.php index f2d9dac..4af7254 100644 --- a/module/dca/tl_leaflet_marker.php +++ b/module/dca/tl_leaflet_marker.php @@ -178,6 +178,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array ['title'], \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), 'sql' => "varchar(255) NOT NULL default ''" diff --git a/module/dca/tl_leaflet_popup.php b/module/dca/tl_leaflet_popup.php index 25deb2d..b8a47db 100644 --- a/module/dca/tl_leaflet_popup.php +++ b/module/dca/tl_leaflet_popup.php @@ -166,6 +166,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_popup'] = array ['title'], \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), 'sql' => "varchar(255) NOT NULL default ''" diff --git a/module/dca/tl_leaflet_style.php b/module/dca/tl_leaflet_style.php index 967dc05..5106f2e 100644 --- a/module/dca/tl_leaflet_style.php +++ b/module/dca/tl_leaflet_style.php @@ -159,6 +159,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_style'] = array ['title'], \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), 'sql' => "varchar(255) NOT NULL default ''" diff --git a/module/dca/tl_leaflet_vector.php b/module/dca/tl_leaflet_vector.php index 207123b..182e701 100644 --- a/module/dca/tl_leaflet_vector.php +++ b/module/dca/tl_leaflet_vector.php @@ -204,6 +204,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array ['title'], \Netzmacht\Contao\Leaflet\DependencyInjection\LeafletServices::ALIAS_GENERATOR ), + \Netzmacht\Contao\Leaflet\Dca\Validator::callback('validateAlias'), ), 'eval' => array('mandatory' => false, 'maxlength' => 255, 'tl_class' => 'w50', 'unique' => true), 'sql' => "varchar(255) NOT NULL default ''" diff --git a/module/languages/en/leaflet.php b/module/languages/en/leaflet.php index 6270998..9cc5aec 100644 --- a/module/languages/en/leaflet.php +++ b/module/languages/en/leaflet.php @@ -45,3 +45,5 @@ $GLOBALS['TL_LANG']['leaflet_vector']['rectangle'][1] = 'Rectangle overlay. For $GLOBALS['TL_LANG']['leaflet']['searchPosition'] = 'Search location'; $GLOBALS['TL_LANG']['leaflet']['applyPosition'] = 'Apply position'; + +$GLOBALS['TL_LANG']['leaflet']['invalidAlias'] = 'Invalid alias given. Alias may not be empty, begin with a numeric value nor contain any special chars (underscore allowed).'; diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Validator.php b/src/Netzmacht/Contao/Leaflet/Dca/Validator.php new file mode 100644 index 0000000..b4d4bd9 --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Dca/Validator.php @@ -0,0 +1,115 @@ + + * @copyright 2016 netzmacht David Molineus. All rights reserved. + * @filesource + * + */ + +namespace Netzmacht\Contao\Leaflet\Dca; + +use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator; +use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory; +use Netzmacht\LeafletPHP\Value\LatLng; + +/** + * Class Validator. + * + * @package Netzmacht\Contao\Leaflet\Dca + */ +class Validator +{ + /** + * Translator. + * + * @var Translator; + */ + private $translator; + + /** + * Validator constructor. + * + * @param Translator $translator Translator. + */ + public function __construct(Translator $translator) + { + $this->translator = $translator; + } + + /** + * Generate the callback definition. + * + * @param string $methodName Callback method name. + * + * @return callable + */ + public static function callback($methodName) + { + return CallbackFactory::service('leaflet.dca.validator', $methodName); + } + + /** + * Validate coordinates. + * + * @param mixed $value Given value. + * + * @return mixed + */ + public function validateCoordinates($value) + { + try { + LatLng::fromString($value); + } catch (\Exception $e) { + throw new \InvalidArgumentException( + $this->translator->translate('invalidCoordinates', 'leaflet', [$value]), + 0, + $e + ); + } + + return $value; + } + + /** + * Validate multiple coordinates. + * + * @param mixed $values Given value. + * + * @return mixed + */ + public function validateMultipleCoordinates($values) + { + if (!is_array($values)) { + $lines = explode("\n", $values); + } else { + $lines = $values; + } + + foreach ($lines as $coordinate) { + LatLng::fromString($coordinate); + } + + return $values; + } + + /** + * Validate an alias. + * + * @param string $value Given value + * + * @return string + * @throws \InvalidArgumentException When invalid value given. + */ + public function validateAlias($value) + { + if (preg_match('/[A-Za-z_][A-Za-z0-9_]+/', $value) !== 1) { + throw new \InvalidArgumentException( + $this->translator->translate('invalidAlias', 'leaflet') + ); + } + + return $value; + } +}