mirror of
https://github.com/netzmacht/contao-leaflet-maps.git
synced 2025-12-01 12:33:45 +01:00
Validate aliases.
This commit is contained in:
115
src/Netzmacht/Contao/Leaflet/Dca/Validator.php
Normal file
115
src/Netzmacht/Contao/Leaflet/Dca/Validator.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package netzmacht
|
||||
* @author David Molineus <david.molineus@netzmacht.de>
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user