Validator::isArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 2
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 9/16/14
5
 * Time: 9:04 PM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Validator;
12
13
use NilPortugues\Validator\Attribute\Collection\CollectionAttribute;
14
use NilPortugues\Validator\Attribute\DateTime\DateTimeAttribute;
15
use NilPortugues\Validator\Attribute\FileUpload\FileUploadAttribute;
16
use NilPortugues\Validator\Attribute\Numeric\FloatAttribute;
17
use NilPortugues\Validator\Attribute\Numeric\IntegerAttribute;
18
use NilPortugues\Validator\Attribute\Object\ObjectAttribute;
19
use NilPortugues\Validator\Attribute\String\StringAttribute;
20
21
/**
22
 * Class Validator
23
 * @package NilPortugues\Validator
24
 */
25
class Validator
26
{
27
    /**
28
     * @var string
29
     */
30
    private $propertyName;
0 ignored issues
show
Unused Code introduced by
The property $propertyName is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
    /**
33
     * @var string
34
     */
35
    private static $language = 'en_GB';
36
37
    /**
38
     * @var string
39
     */
40
    private static $errorDir = '/Errors';
41
42
    /**
43
     * @var string
44
     */
45
    private $functionMapFile = 'FunctionMap.php';
46
47
    /**
48
     * @var array
49
     */
50
    private static $functionMap = [];
51
52
    /**
53
     * @var array
54
     */
55
    private static $errorMessages = [];
56
57
    /**
58
     * @var string
59
     */
60
    private static $errorMessageFile = '';
61
62
    /**
63
     * @var self
64
     */
65
    private static $instances = [];
66
67
    /**
68
     * @param string $errorMessageFile
69
     *
70
     * @return \NilPortugues\Validator\Validator
71
     */
72
    public static function create($errorMessageFile = '')
73
    {
74
        if (!isset(self::$instances[$errorMessageFile])) {
75
            self::$instances[$errorMessageFile] = new self($errorMessageFile);
76
        }
77
        return self::$instances[$errorMessageFile];
78
    }
79
80
    /**
81
     * @param string $errorMessageFile
82
     */
83
    private function __construct($errorMessageFile = '')
84
    {
85
        self::buildErrorMessages($errorMessageFile);
86
        self::buildFunctionMap();
87
    }
88
89
    /**
90
     * @param string $errorMessageFile
91
     *
92
     * @throws \InvalidArgumentException
93
     */
94
    private static function buildErrorMessages($errorMessageFile)
95
    {
96
        $filePath = $errorMessageFile;
97
98
        if ('' == $filePath) {
99
            $filePath = \realpath(\dirname(__FILE__))
100
                .DIRECTORY_SEPARATOR.self::$errorDir
101
                .DIRECTORY_SEPARATOR.self::$language.".php";
102
        }
103
104
        if (false === \file_exists($filePath)) {
105
            throw new \InvalidArgumentException("Language not found.");
106
        }
107
108
        self::$errorMessages = include $filePath;
109
        self::$errorMessageFile = $filePath;
110
    }
111
112
    /**
113
     * @throws \RuntimeException
114
     */
115
    private function buildFunctionMap()
116
    {
117
        $functionMap = \realpath(\dirname(__FILE__)).DIRECTORY_SEPARATOR.$this->functionMapFile;
118
119
        if (!file_exists($functionMap)) {
120
            throw new \RuntimeException('FunctionMap not found');
121
        }
122
123
        self::$functionMap = include $functionMap;
124
    }
125
126
    /**
127
     * @param string $propertyName
128
     *
129
     * @return CollectionAttribute
130
     */
131
    public function isArray($propertyName)
132
    {
133
        return new CollectionAttribute($propertyName, $this, self::$errorMessages, self::$functionMap);
134
    }
135
136
    /**
137
     * @param string $propertyName
138
     *
139
     * @return \NilPortugues\Validator\Attribute\Numeric\IntegerAttribute
140
     */
141
    public function isInteger($propertyName)
142
    {
143
        return new IntegerAttribute($propertyName, $this, self::$errorMessages, self::$functionMap);
144
    }
145
146
    /**
147
     * @param string $propertyName
148
     *
149
     * @return \NilPortugues\Validator\Attribute\Numeric\FloatAttribute
150
     */
151
    public function isFloat($propertyName)
152
    {
153
        return new FloatAttribute($propertyName, $this, self::$errorMessages, self::$functionMap);
154
    }
155
156
    /**
157
     * @param string $propertyName
158
     *
159
     * @return \NilPortugues\Validator\Attribute\Object\ObjectAttribute
160
     */
161
    public function isObject($propertyName)
162
    {
163
        return new ObjectAttribute($propertyName, $this, self::$errorMessages, self::$functionMap);
164
    }
165
166
    /**
167
     * @param string $propertyName
168
     *
169
     * @return \NilPortugues\Validator\Attribute\String\StringAttribute
170
     */
171
    public function isString($propertyName)
172
    {
173
        return new StringAttribute($propertyName, $this, self::$errorMessages, self::$functionMap);
174
    }
175
176
    /**
177
     * @param string $propertyName
178
     *
179
     * @return \NilPortugues\Validator\Attribute\DateTime\DateTimeAttribute
180
     */
181
    public function isDateTime($propertyName)
182
    {
183
        return new DateTimeAttribute($propertyName, $this, self::$errorMessages, self::$functionMap);
184
    }
185
186
    /**
187
     * @param string $propertyName
188
     *
189
     * @return \NilPortugues\Validator\Attribute\FileUpload\FileUploadAttribute
190
     */
191
    public function isFileUpload($propertyName)
192
    {
193
        return new FileUploadAttribute($propertyName, $this, self::$errorMessages, self::$functionMap);
194
    }
195
}
196