MaximumLength::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Phypes\Rule\String;
4
5
use Phypes\Error\RuleError;
6
use Phypes\Error\RuleErrorCode;
7
use Phypes\Result\Failure;
8
use Phypes\Result\Result;
9
use Phypes\Result\Success;
10
use Phypes\Rule\Primitive\StringType;
11
use Phypes\Rule\Rule;
12
13
class MaximumLength implements Rule
14
{
15
    private $maxLength;
16
17
    public function __construct(int $maxLength)
18
    {
19
        $this->maxLength = $maxLength;
20
    }
21
    public function validate($data) : Result
22
    {
23
        $result = (new StringType())->validate($data);
24
25
        if (!$result->isValid())
26
            return new $result;
27
28
        if (mb_strlen($data, 'UTF-8') <= $this->maxLength) {
29
            return new Success();
30
        }
31
        else return new Failure(
32
            new RuleError(RuleErrorCode::LENGTH_ERROR,
33
                'The supplied string is too long'));
34
    }
35
}