Detect if empty values are supported.

This commit is contained in:
David Molineus
2017-10-09 17:02:25 +02:00
parent 95046b65e0
commit 6b646243e1
2 changed files with 52 additions and 10 deletions

View File

@@ -12,6 +12,8 @@
namespace Netzmacht\Contao\Leaflet\Listener\Dca;
use Contao\DataContainer;
use Netzmacht\Contao\Toolkit\Dca\Manager;
use Netzmacht\LeafletPHP\Value\LatLng;
use Symfony\Component\Translation\TranslatorInterface as Translator;
@@ -29,26 +31,41 @@ class Validator
*/
private $translator;
/**
* Data container manager.
*
* @var Manager
*/
private $dcaManager;
/**
* Validator constructor.
*
* @param Manager $dcaManager Data container manager.
* @param Translator $translator Translator.
*/
public function __construct(Translator $translator)
public function __construct(Manager $dcaManager, Translator $translator)
{
$this->translator = $translator;
$this->dcaManager = $dcaManager;
}
/**
* Validate coordinates.
*
* @param mixed $value Given value.
* @param mixed $value Given value.
* @param DataContainer $dataContainer Data container driver.
*
* @return mixed
* @throws \InvalidArgumentException When invalid coordinates give.
*
* @throws \InvalidArgumentException When invalid coordinates given.
*/
public function validateCoordinates($value)
public function validateCoordinates($value, $dataContainer)
{
if (!$value && !$this->isRequired($dataContainer)) {
return $value;
}
try {
LatLng::fromString($value);
} catch (\Exception $e) {
@@ -65,11 +82,14 @@ class Validator
/**
* Validate multiple coordinates.
*
* @param mixed $values Given value.
* @param mixed $values Given value.
* @param DataContainer $dataContainer Data container driver.
*
* @return mixed
*
* @throws \InvalidArgumentException When invalid coordinates given.
*/
public function validateMultipleCoordinates($values)
public function validateMultipleCoordinates($values, $dataContainer)
{
if (!is_array($values)) {
$lines = explode("\n", $values);
@@ -78,7 +98,7 @@ class Validator
}
foreach ($lines as $coordinate) {
$this->validateCoordinates($coordinate);
$this->validateCoordinates($coordinate, $dataContainer);
}
return $values;
@@ -87,15 +107,18 @@ class Validator
/**
* Validate multiple coordinate sets.
*
* @param mixed $values Given value.
* @param mixed $values Given value.
* @param DataContainer $dataContainer Data container driver.
*
* @return mixed
*
* @throws \InvalidArgumentException When invalid coordinates given.
*/
public function validateMultipleCoordinateSets($values)
public function validateMultipleCoordinateSets($values, $dataContainer)
{
$sets = deserialize($values, true);
foreach ($sets as $lines) {
$this->validateMultipleCoordinates($lines);
$this->validateMultipleCoordinates($lines, $dataContainer);
}
return $values;
@@ -119,4 +142,22 @@ class Validator
return $value;
}
/**
* Check if value is required.
*
* @param DataContainer $dataContainer Data container driver.
*
* @return bool
*/
private function isRequired($dataContainer): bool
{
$definition = $this->dcaManager->getDefinition($dataContainer->table);
if ($definition->get(['fields', $dataContainer->field, 'eval', 'mandatory'])) {
return true;
}
return false;
}
}

View File

@@ -8,6 +8,7 @@ services:
netzmacht.contao_leaflet_maps.listeners.dca.validator:
class: Netzmacht\Contao\Leaflet\Listener\Dca\Validator
arguments:
- '@netzmacht.contao_toolkit.dca.manager'
- '@translator'
netzmacht.contao_leaflet_maps.listeners.dca.frontend_integration: