summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Form/Validate/IsDataListEntry.php
blob: 5762d2ea3f1c2d40f379e6a021310ccb430723d0 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

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

use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorDatalistEntry;
use Zend_Validate_Abstract;

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

    /** @var Db */
    private $db;

    /** @var int */
    private $dataListId;

    public function __construct($dataListId, Db $db)
    {
        $this->db = $db;
        $this->dataListId = (int) $dataListId;
    }

    public function isValid($value)
    {
        if (is_array($value)) {
            foreach ($value as $name) {
                if (! $this->isListEntry($name)) {
                    $this->_error(self::INVALID, $value);

                    return false;
                }
            }

            return true;
        }

        if ($this->isListEntry($value)) {
            return true;
        } else {
            $this->_error(self::INVALID, $value);

            return false;
        }
    }

    protected function isListEntry($name)
    {
        return DirectorDatalistEntry::exists([
            'list_id' => $this->dataListId,
            'entry_name' => $name,
        ], $this->db);
    }
}