summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Form/Validate/NamePattern.php
blob: fac44d972cefdf44f58ae1d947ed4b1c9b277242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

namespace Icinga\Module\Director\Web\Form\Validate;

use Icinga\Module\Director\Restriction\MatchingFilter;
use Zend_Validate_Abstract;

class NamePattern extends Zend_Validate_Abstract
{
    const INVALID = 'intInvalid';

    private $filter;

    public function __construct($pattern)
    {
        if (! is_array($pattern)) {
            $pattern = [$pattern];
        }

        $this->filter = MatchingFilter::forPatterns($pattern, 'value');

        $this->_messageTemplates[self::INVALID] = sprintf(
            'Does not match %s',
            (string) $this->filter
        );
    }

    public function isValid($value)
    {
        if ($this->filter->matches((object) ['value' => $value])) {
            return true;
        } else {
            $this->_error(self::INVALID, $value);

            return false;
        }
    }
}