MaximumLength   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 13 3
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
}