2017-10-02 08:45:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Form\Validate;
|
|
|
|
|
2018-02-20 13:29:33 +01:00
|
|
|
use Icinga\Module\Director\Restriction\MatchingFilter;
|
2017-10-02 08:45:32 +02:00
|
|
|
use Zend_Validate_Abstract;
|
|
|
|
|
|
|
|
class NamePattern extends Zend_Validate_Abstract
|
|
|
|
{
|
|
|
|
const INVALID = 'intInvalid';
|
|
|
|
|
|
|
|
private $filter;
|
|
|
|
|
|
|
|
public function __construct($pattern)
|
|
|
|
{
|
2018-02-20 13:29:33 +01:00
|
|
|
if (! is_array($pattern)) {
|
|
|
|
$pattern = [$pattern];
|
2017-12-12 15:49:22 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 13:29:33 +01:00
|
|
|
$this->filter = MatchingFilter::forPatterns($pattern, 'value');
|
2017-12-12 15:55:15 +01:00
|
|
|
|
2017-10-02 08:45:32 +02:00
|
|
|
$this->_messageTemplates[self::INVALID] = sprintf(
|
|
|
|
'Does not match %s',
|
2018-02-20 13:29:33 +01:00
|
|
|
(string) $this->filter
|
2017-10-02 08:45:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isValid($value)
|
|
|
|
{
|
2018-02-20 13:29:33 +01:00
|
|
|
if ($this->filter->matches((object) ['value' => $value])) {
|
2017-10-02 08:45:32 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$this->_error(self::INVALID, $value);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|