Passed
Push — master ( 77e4b7...db2920 )
by Jonathan
03:29
created

AbstractResourceValidator::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2017 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud\Validator;
15
16
use Zend\Validator\AbstractValidator as ZendAbstractValidator;
17
use Zend\Validator\ValidatorInterface as ZendValidatorInterface;
18
19
/**
20
 * Abstract Resource validator
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
abstract class AbstractResourceValidator extends ZendAbstractValidator implements ZendValidatorInterface
26
{
27
    /**
28
     * Invalid syntax
29
     *
30
     * @const INVALID
31
     */
32
    const INVALID_VALUE = 'invalidValue';
33
34
    /**
35
     * Message templates
36
     *
37
     * @var array
38
     */
39
    protected $messageTemplates = [
40
        self::INVALID_VALUE => '',  // added dynamically
41
    ];
42
43
    /**
44
     * Filename of the resource file.
45
     *
46
     * @var string
47
     */
48
    protected $filename;
49
50
    /**
51
     * Returns true, if value is valid. False otherwise.
52
     *
53
     * @param mixed $value
54
     *
55
     * @return bool
56
     */
57 9
    public function isValid($value)
58
    {
59 9
        $this->setValue($value);
60
61 9
        $values = include $this->getFilename();
62
63 9
        if (!in_array($value, $values)) {
64
65 3
            $message = sprintf("'%s' is not a valid value. Valid values are: '%s'"
66 3
                , $value
67 3
                , implode("', '", $values));
68
69 3
            $this->setMessage($message, self::INVALID_VALUE);
70 3
            $this->error(self::INVALID_VALUE);
71
72 3
            return false;
73
        }
74
75 6
        return true;
76
    }
77
78
    /**
79
     * Set the filename of the resource file.
80
     *
81
     * @return string
82
     */
83 9
    public function getFilename()
84
    {
85 9
        return $this->filename;
86
    }
87
88
    /**
89
     * Get the filename of the resource file.
90
     *
91
     * @param mixed $filename
92
     * @return AbstractResourceValidator
93
     */
94 9
    public function setFilename($filename)
95
    {
96 9
        $this->filename = $filename;
97
98 9
        return $this;
99
    }
100
}
101