Completed
Pull Request — master (#108)
by Matt
19:10
created

MinLength::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 3
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraints\DraftFour;
4
5
use League\JsonGuard;
6
use League\JsonGuard\Assert;
7
use League\JsonGuard\Constraint;
8
use League\JsonGuard\Validator;
9
use function League\JsonGuard\error;
10
11 View Code Duplication
class MinLength implements Constraint
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...
12
{
13
    const KEYWORD = 'minLength';
14
15
    /**
16
     * @var string
17
     */
18
    private $charset;
19
20
    /**
21
     * @param string $charset
22
     */
23
    public function __construct($charset = 'UTF-8')
24
    {
25
        $this->charset = $charset;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function validate($value, $parameter, Validator $validator)
32
    {
33
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath());
34
        Assert::nonNegative($parameter, self::KEYWORD, $validator->getSchemaPath());
35
36
        if (!is_string($value) || JsonGuard\strlen($value, $this->charset) >= $parameter) {
37
            return null;
38
        }
39
40
        return error('The string must be at least {parameter} characters long.', $validator);
41
    }
42
}
43