MinLengthConstraint::apply()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 4
crap 3
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 "minLength" keyword.
19
 */
20 View Code Duplication
class MinLengthConstraint extends AbstractCountConstraint
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 378
    public function keywords()
26
    {
27 378
        return ['minLength'];
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 12
    public function apply($instance, stdClass $schema, Context $context, Walker $walker)
42
    {
43 12
        $length = extension_loaded('mbstring') ?
44 12
            mb_strlen($instance, mb_detect_encoding($instance)) :
45 12
            strlen($instance);
46
47 12
        if ($length < $schema->minLength) {
48 4
            $context->addViolation(
49 4
                'should be greater than or equal to %s characters',
50 4
                [$schema->minLength]
51 4
            );
52 4
        }
53 12
    }
54
}
55