MaximumLength::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
}