Passed
Push — master ( e83d2f...c5d235 )
by Dedipyaman
02:10
created

MaximumLength   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 8 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dedipyaman
5
 * Date: 1/25/19
6
 * Time: 3:46 PM
7
 */
8
9
namespace Phypes\Rule\String;
10
11
12
use Phypes\Error\RuleError\RuleError;
13
use Phypes\Error\RuleError\RuleErrorCode;
14
use Phypes\Result;
15
use Phypes\Rule\Rule;
16
17
class MaximumLength implements Rule
18
{
19
    private $maxLength;
20
21
    public function __construct(int $maxLength)
22
    {
23
        $this->maxLength = $maxLength;
24
    }
25
    public function validate($data) : Result
26
    {
27
        if (mb_strlen($data, 'UTF-8') <= $this->maxLength) {
28
            return new Result(true);
29
        }
30
        else return new Result(false,
31
            new RuleError(RuleErrorCode::TOO_LONG,
32
                'The supplied string is too long'));
33
    }
34
}