2016-03-04 18:08:37 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Form\Element;
|
|
|
|
|
2019-04-30 13:02:00 +02:00
|
|
|
use InvalidArgumentException;
|
2016-12-15 11:42:54 +01:00
|
|
|
|
2016-03-04 18:08:37 +01:00
|
|
|
/**
|
|
|
|
* Input control for extensible sets
|
|
|
|
*/
|
|
|
|
class ExtensibleSet extends FormElement
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Default form view helper to use for rendering
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-06-18 14:08:37 +02:00
|
|
|
public $helper = 'formIplExtensibleSet';
|
2016-03-04 18:08:37 +01:00
|
|
|
|
|
|
|
// private $multiOptions;
|
|
|
|
|
2016-05-19 15:02:30 +02:00
|
|
|
public function getValue()
|
|
|
|
{
|
|
|
|
$value = parent::getValue();
|
2019-05-20 17:44:55 +02:00
|
|
|
if (is_string($value) || is_numeric($value)) {
|
2019-04-30 13:02:00 +02:00
|
|
|
$value = [$value];
|
2016-12-15 11:42:54 +01:00
|
|
|
} elseif ($value === null) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
if (! is_array($value)) {
|
2019-05-20 11:04:56 +02:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2016-12-15 11:42:54 +01:00
|
|
|
'ExtensibleSet expects to work with Arrays, got %s',
|
|
|
|
var_export($value, 1)
|
2019-05-20 11:04:56 +02:00
|
|
|
));
|
2016-05-19 15:02:30 +02:00
|
|
|
}
|
|
|
|
$value = array_filter($value, 'strlen');
|
|
|
|
|
|
|
|
if (empty($value)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2016-10-14 15:35:30 +02:00
|
|
|
/**
|
|
|
|
* We do not want one message per entry
|
|
|
|
*
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
*/
|
|
|
|
protected function _getErrorMessages()
|
|
|
|
{
|
|
|
|
return $this->_errorMessages;
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
}
|
|
|
|
|
2016-06-12 11:14:12 +02:00
|
|
|
/**
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
*/
|
2016-05-20 08:13:13 +02:00
|
|
|
protected function _filterValue(&$value, &$key)
|
|
|
|
{
|
2016-06-12 11:14:12 +02:00
|
|
|
// @codingStandardsIgnoreEnd
|
2016-05-20 10:46:01 +02:00
|
|
|
if (is_array($value)) {
|
2016-05-20 10:40:58 +02:00
|
|
|
$value = array_filter($value, 'strlen');
|
2016-05-20 10:46:01 +02:00
|
|
|
} elseif (is_string($value) && !strlen($value)) {
|
|
|
|
$value = null;
|
2016-05-20 10:40:58 +02:00
|
|
|
}
|
2016-05-20 10:46:01 +02:00
|
|
|
|
2017-06-18 14:08:37 +02:00
|
|
|
parent::_filterValue($value, $key);
|
2016-05-20 08:13:13 +02:00
|
|
|
}
|
|
|
|
|
2016-03-04 18:08:37 +01:00
|
|
|
public function isValid($value, $context = null)
|
|
|
|
{
|
|
|
|
if ($value === null) {
|
2019-04-30 13:02:00 +02:00
|
|
|
$value = [];
|
2016-03-04 18:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$value = array_filter($value, 'strlen');
|
|
|
|
$this->setValue($value);
|
|
|
|
if ($this->isRequired() && empty($value)) {
|
|
|
|
// TODO: translate
|
|
|
|
$this->addError('You are required to choose at least one element');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:43:25 +02:00
|
|
|
if ($this->hasErrors()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-30 13:02:00 +02:00
|
|
|
return parent::isValid($value, $context);
|
2016-03-04 18:08:37 +01:00
|
|
|
}
|
|
|
|
}
|