MinimumLength   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 22
rs 10
c 0
b 0
f 0
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;
8
use Phypes\Rule\Primitive\StringType;
9
use Phypes\Rule\Rule;
10
11
class MinimumLength implements Rule
12
{
13
    private $minLength;
14
15
    public function __construct(int $minLength)
16
    {
17
        $this->minLength = $minLength;
18
    }
19
20
    public function validate($data) : Result\Result
21
    {
22
        $result = (new StringType())->validate($data);
23
24
        if (!$result->isValid())
25
            return new $result;
26
27
        if (mb_strlen($data, 'UTF-8') >= $this->minLength) {
28
            return new Result\Success();
29
        }
30
        else return new Result\Failure(
31
            new RuleError(RuleErrorCode::LENGTH_ERROR,
32
            'The supplied string is too short'));
33
    }
34
}