BaseRule::isNotRequiredAndEmpty()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 8.8571
cc 5
eloc 8
nc 8
nop 1
1
<?php
2
3
/**
4
 * @author: Abdul Qureshi. <[email protected]>
5
 * 
6
 * This file has been modified from the original source.
7
 * See original here:
8
 *
9
 * @link: https://github.com/progsmile/request-validator
10
 */
11
namespace TheSupportGroup\Common\Validator\Rules;
12
13
use TheSupportGroup\Common\ValidationInterop\ValidationProviderInterface;
14
15
abstract class BaseRule
16
{
17
    const CONFIG_ALL = 'all';
18
    const CONFIG_DATA = 'data';
19
    const CONFIG_FIELD_RULES = 'fieldRules';
20
21
    /**
22
     * @var array $config
23
     */
24
    private $config;
25
26
    /**
27
     * @var array $params
28
     */
29
    private $params;
30
31
    /**
32
     * @var ValidationProviderInterface $validationProvider
33
     */
34
    private $validationProvider;
35
36
    /**
37
     * BaseRule constructor.
38
     *
39
     * @param $config
40
     */
41
    public function __construct(
42
        $config,
43
        ValidationProviderInterface $validationProvider
44
    ) {
45
        $this->config = $config;
46
        $this->validationProvider = $validationProvider;
47
    }
48
49
    /**
50
     * Validator calls captured and remapped here.
51
     *
52
     * @return BaseRule Validator rule object.
53
     */
54
    public function __call($method, array $params = array())
55
    {
56
        // Get the mapping out of the mapper file.
57
        $validatorMethod = $this->validationProvider->getMappedMethod($method);
58
59
        // Try running rule against params.
60
        $this->buildRule($validatorMethod, $params);
61
62
        // Return self for next call to be made via the base Rule.
63
        return $this;
64
    }
65
66
    /**
67
     * Run rule against validator.
68
     */
69
    private function buildRule($ruleName, $arguments = [])
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
70
    {
71
        return $this->validationProvider->rule($ruleName, $arguments);
72
    }
73
74
    /**
75
     * Validate rule against value provided.
76
     * We can control which method is called on the external library.
77
     */
78
    public function validate($value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
79
    {
80
        return $this->validationProvider->validate($value);
81
    }
82
83
    /**
84
     * Returns all config array, or specific one.
85
     *
86
     * @param string $type
87
     *
88
     * @return string
89
     */
90
    public function getConfig($type = self::CONFIG_ALL)
91
    {
92
        if ($type == self::CONFIG_ALL) {
93
            return $this->config;
94
        }
95
96
        return isset($this->config[$type]) ? $this->config[$type] : [];
97
    }
98
99
    /**
100
     * If field has specific rule.
101
     *
102
     * @param string $rule
103
     *
104
     * @return bool
105
     */
106
    public function hasRule($rule)
107
    {
108
        return strpos($this->getConfig(self::CONFIG_FIELD_RULES), $rule) !== false;
109
    }
110
111
    /**
112
     * Check if variable is not required - to prevent error messages from another validators.
113
     *
114
     * @param string $type | 'var' or 'file'
115
     *
116
     * @return bool
117
     */
118
    protected function isNotRequiredAndEmpty($type = 'var')
0 ignored issues
show
Coding Style introduced by
isNotRequiredAndEmpty uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
119
    {
120
        $condition = false;
121
122
        if ($type == 'var') {
123
            $condition = strlen($this->params[1]) == 0;
124
        } elseif ($type == 'file') {
125
            $fieldsName = $this->params[0];
126
127
            //when file field is not required and empty
128
            $condition = isset($_FILES[$fieldsName]['name']) && $_FILES[$fieldsName]['name'] == '';
129
        }
130
131
        return !$this->hasRule('required') && $condition;
132
    }
133
134
    /**
135
     * Set params to validator.
136
     *
137
     * @param $params
138
     *
139
     * @return $this
140
     */
141
    public function setParams($params)
142
    {
143
        $this->params = $params;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Returns params.
150
     *
151
     * @return mixed
152
     */
153
    public function getParams()
154
    {
155
        return $this->params;
156
    }
157
158
    /**
159
     * Returns pure class name.
160
     *
161
     * @return string
162
     */
163
    public function getRuleName()
164
    {
165
        $classPath = explode('\\', get_class($this));
166
167
        return array_pop($classPath);
168
    }
169
170
    /**
171
     * Returns error message from rule.
172
     *
173
     * @return string
174
     */
175
    abstract public function getMessage();
176
177
    /**
178
     * The main function that validates the inputted value against
179
     * an existing one or similar.
180
     *
181
     * @return bool Return a if values were valid/matched or not
182
     */
183
    abstract public function isValid();
184
}
185