MaxLengthConstraint::keywords()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Constraint;
11
12
use JVal\Context;
13
use JVal\Types;
14
use JVal\Walker;
15
use stdClass;
16
17
/**
18
 * Constraint for the "maxLength" keyword.
19
 */
20 View Code Duplication
class MaxLengthConstraint extends AbstractCountConstraint
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 378
    public function keywords()
26
    {
27 378
        return ['maxLength'];
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 357
    public function supports($type)
34
    {
35 357
        return $type === Types::TYPE_STRING;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 11
    public function apply($instance, stdClass $schema, Context $context, Walker $walker)
42
    {
43 11
        $length = extension_loaded('mbstring') ?
44 11
            mb_strlen($instance, mb_detect_encoding($instance)) :
45 11
            strlen($instance);
46
47 11
        if ($length > $schema->maxLength) {
48 5
            $context->addViolation(
49 5
                'should be lesser than or equal to %s characters',
50 5
                [$schema->maxLength]
51 5
            );
52 5
        }
53 11
    }
54
}
55