Passed
Push — develop ( 1226dd...59b4e3 )
by Andrew
02:36
created

FieldsValidator::validateArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
4
namespace ddlzz\AmoAPI\Validators;
5
6
use ddlzz\AmoAPI\Exceptions\EntityFieldsException;
7
8
9
/**
10
 * Class FieldsValidator
11
 * @package ddlzz\AmoAPI\Validators
12
 * @author ddlzz
13
 */
14
class FieldsValidator
15
{
16
    /** @var array */
17
    private $fieldsParams;
18
19
    /** @var string */
20
    private $action = '';
21
22
    /**
23
     * FieldsValidator constructor.
24
     * @param array $fieldsParams
25
     */
26 33
    public function __construct(array $fieldsParams)
27
    {
28 33
        $this->fieldsParams = $fieldsParams;
29 33
    }
30
31
    /**
32
     * @param string $action
33
     */
34 5
    public function setAction($action)
35
    {
36 5
        $this->action = $action;
37 5
    }
38
39
    /**
40
     * @param string $key
41
     * @param mixed $value
42
     * @return bool
43
     * @throws EntityFieldsException
44
     */
45 33
    public function isValid($key, $value)
46
    {
47 33
        $this->validateRequired($key, $value);
48
49 32
        if (isset($value)) {
50 31
            switch ($this->fieldsParams[$key]['type']) {
51 31
                case 'int':
52 9
                    self::validateInt($key, $value);
53 3
                    break;
54 22
                case 'string':
55 5
                    self::validateString($key, $value);
56 2
                    break;
57 17
                case 'bool':
58 6
                    self::validateBool($key, $value);
59 2
                    break;
60 11
                case 'array':
61 5
                    self::validateArray($key, $value);
62 2
                    break;
63 6
                case 'array|string':
64 5
                    self::validateArrayString($key, $value);
65 3
                    break;
66
                default:
67 1
                    throw new EntityFieldsException(
68 1
                        "Internal error: the field \"$key\" doesn't match any of the entity predefined fields"
69
                    );
70
            }
71
        }
72
73 13
        return true;
74
    }
75
76
    /**
77
     * @param $key
78
     * @param $value
79
     * @return bool
80
     * @throws EntityFieldsException
81
     */
82 33
    private function validateRequired($key, $value)
83
    {
84 33
        switch ($this->action) {
85 33 View Code Duplication
            case 'add':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86 5
                if (!isset($value) && (true === $this->fieldsParams[$key]['required_add'])) {
87 1
                    throw new EntityFieldsException("Adding error: the required field \"$key\" is missing or empty");
88
                }
89 4
                break;
90 31 View Code Duplication
            case 'update':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91 3
                if (!isset($value) && (true === $this->fieldsParams[$key]['required_update'])) {
92 1
                    throw new EntityFieldsException("Updating error: the required field \"$key\" is missing or empty");
93
                }
94 2
                break;
95
        }
96
97 32
        return true;
98
    }
99
100
    /**
101
     * @param string $key
102
     * @param int $value
103
     * @return bool
104
     * @throws EntityFieldsException
105
     */
106 9
    private static function validateInt($key, $value)
107
    {
108 9
        if (!is_int($value) || !preg_match('/^\d+$/', (string)$value)) {
109 6
            throw new EntityFieldsException("The field \"$key\" must contain digits only");
110
        }
111
112 3
        return true;
113
    }
114
115
    /**
116
     * @param string $key
117
     * @param string $value
118
     * @return bool
119
     * @throws EntityFieldsException
120
     */
121 5 View Code Duplication
    private static function validateString($key, $value) // todo_ddlzz test this
0 ignored issues
show
Duplication introduced by
This method 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...
122
    {
123 5
        if (!is_string($value) && !is_numeric($value)) {
124 3
            throw new EntityFieldsException("The field \"$key\" must be string");
125
        }
126
127 2
        return true;
128
    }
129
130
    /**
131
     * @param string $key
132
     * @param bool $value
133
     * @return bool
134
     * @throws EntityFieldsException
135
     */
136 6
    private static function validateBool($key, $value)
137
    {
138 6
        if (is_null(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) {
139 4
            throw new EntityFieldsException("The field \"$key\" must contain boolean values only");
140
        }
141
142 2
        return true;
143
    }
144
145
    /**
146
     * @param string $key
147
     * @param array $value
148
     * @return bool
149
     * @throws EntityFieldsException
150
     */
151 5
    private static function validateArray($key, $value)
152
    {
153 5
        if (!is_array($value)) {
154 3
            throw new EntityFieldsException("The field \"$key\" must be an array");
155
        }
156
157 2
        return true;
158
    }
159
160
    /**
161
     * Because some fields must be either strings during entity creation or arrays during it's obtaining from server,
162
     * we create this check
163
     * @param string $key
164
     * @param array $value
165
     * @return bool
166
     * @throws EntityFieldsException
167
     */
168 5 View Code Duplication
    private static function validateArrayString($key, $value) // todo_ddlzz test this and delete if possible
0 ignored issues
show
Duplication introduced by
This method 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...
169
    {
170 5
        if ((!is_array($value)) && (!is_string($value) && !is_numeric($value))) {
171 2
            throw new EntityFieldsException("The field \"$key\" must be an array or string");
172
        }
173
174 3
        return true;
175
    }
176
}