Completed
Push — master ( 25512e...e66e4c )
by Matt
02:57
created

src/Constraint/DraftFour/MaxLength.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard;
6
use League\JsonGuard\Assert;
7
use League\JsonGuard\ConstraintInterface;
8
use League\JsonGuard\Validator;
9
use function League\JsonGuard\error;
10
11 View Code Duplication
final class MaxLength implements ConstraintInterface
0 ignored issues
show
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 = 'maxLength';
14
15
    /**
16
     * @var string
17
     */
18
    private $charset;
19
20
    /**
21
     * @param string $charset
22
     */
23 10
    public function __construct($charset = 'UTF-8')
24
    {
25 10
        $this->charset = $charset;
26 10
    }
27
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 10
    public function validate($value, $parameter, Validator $validator)
33
    {
34 10
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath());
35 8
        Assert::nonNegative($parameter, self::KEYWORD, $validator->getSchemaPath());
36
37 6
        if (!is_string($value) || JsonGuard\strlen($value, $this->charset) <= $parameter) {
38 6
            return null;
39
        }
40
41 6
        return error('The string must be less than {parameter} characters long.', $validator);
42
    }
43
}
44