Passed
Pull Request — master (#7)
by Jonathan
06:00
created

Language::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.2
cc 2
eloc 12
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
/**
17
 * Language validator
18
 *
19
 * @package TxTextControl\ReportingCloud
20
 * @author  Jonathan Maron (@JonathanMaron)
21
 */
22
class Language extends AbstractValidator
23
{
24
    const FILENAME = '../../resource/available-dictionaries.php';
25
    /**
26
     * Invalid syntax
27
     *
28
     * @const INVALID
29
     */
30
    const INVALID_LANGUAGE = 'invalidLanguage';
31
32
    /**
33
     * Message templates
34
     *
35
     * @var array
36
     */
37
    protected $messageTemplates = [
38
        self::INVALID_LANGUAGE => '',  // added dynamically
39
    ];
40
41
    /**
42
     * Returns true, if value is valid. False otherwise.
43
     *
44
     * @param mixed $value
45
     *
46
     * @return bool
47
     */
48 5
    public function isValid($value)
49
    {
50 5
        $this->setValue($value);
51
52 5
        $filename = realpath(__DIR__ . DIRECTORY_SEPARATOR . self::FILENAME);
53
54 5
        $values = include $filename;
55
56 5
        if (!in_array($value, $values)) {
57
58 1
            $message = sprintf("'%s' is not a valid language value. Valid values are: '%s'"
59 1
                , $value
60 1
                , implode("', '", $values));
61
62 1
            $this->setMessage($message, self::INVALID_LANGUAGE);
63 1
            $this->error(self::INVALID_LANGUAGE);
64
65 1
            return false;
66
        }
67
68 4
        return true;
69
    }
70
}