Passed
Push — master ( 1a3693...31c150 )
by Jonathan
10:52
created

Culture::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
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
 * Culture validator
18
 *
19
 * @package TxTextControl\ReportingCloud
20
 * @author  Jonathan Maron (@JonathanMaron)
21
 */
22 View Code Duplication
class Culture extends AbstractValidator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    const FILENAME = '../../resource/cultures.php';
25
    /**
26
     * Invalid syntax
27
     *
28
     * @const INVALID
29
     */
30
    const INVALID_CULTURE = 'invalidCulture';
31
32
    /**
33
     * Message templates
34
     *
35
     * @var array
36
     */
37
    protected $messageTemplates = [
38
        self::INVALID_CULTURE => '',  // 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 3
    public function isValid($value)
49
    {
50 3
        $this->setValue($value);
51
52 3
        $filename = realpath(__DIR__ . DIRECTORY_SEPARATOR . self::FILENAME);
53
54 3
        $values = include $filename;
55
56 3
        if (!in_array($value, $values)) {
57
58 1
            $message = sprintf("'%s' is not a valid culture value. Valid values are: '%s'"
59 1
                , $value
60 1
                , implode("', '", $values));
61
62 1
            $this->setMessage($message, self::INVALID_CULTURE);
63 1
            $this->error(self::INVALID_CULTURE);
64
65 1
            return false;
66
        }
67
68 2
        return true;
69
    }
70
}