Max::isValid()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 8.8571
cc 5
eloc 9
nc 4
nop 0
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
12
namespace TheSupportGroup\Common\Validator\Rules;
13
14
use TheSupportGroup\Common\Validator\Contracts\Rules\RuleInterface;
15
16
class Max extends BaseRule implements RuleInterface
17
{
18
    private $isNumeric = false;
19
20
    public function isValid()
21
    {
22
        if ($this->isNotRequiredAndEmpty()) {
23
            return true;
24
        }
25
26
        $input = trim($this->getParams()[1]);
27
        $value = trim($this->getParams()[2]);
28
29
        if ($this->hasRule('numeric') !== false && is_numeric($input)) {
30
            $this->isNumeric = true;
31
32
            return $this->Max($value, true)->validate($input);
0 ignored issues
show
Documentation Bug introduced by
The method Max does not exist on object<TheSupportGroup\C...on\Validator\Rules\Max>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
33
        }
34
35
        // there is no way respect/validator supports string for rule 'Max'
36
        return is_string($input) && strlen($input) <= $value;
37
    }
38
39
    public function getMessage()
40
    {
41
        if ($this->isNumeric) {
42
            return 'Field :field: must be less than or equal to :param:';
43
        }
44
45
        return 'Field :field: should be maximum of :param: characters';
46
    }
47
}
48