ExactLength   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 13 3
A __construct() 0 3 1
1
<?php
2
/*
3
 * This file is part of Phypes <https://github.com/2DSharp/Phypes>.
4
 *
5
 * (c) Dedipyaman Das <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Phypes\Rule\String;
13
14
15
use Phypes\Error\RuleError;
16
use Phypes\Error\RuleErrorCode;
17
use Phypes\Result\Failure;
18
use Phypes\Result\Result;
19
use Phypes\Result\Success;
20
use Phypes\Rule\Primitive\StringType;
21
use Phypes\Rule\Rule;
22
23
class ExactLength implements Rule
24
{
25
    /**
26
     * @var int $length
27
     */
28
    private $length;
29
30
    public function __construct(int $length)
31
    {
32
        $this->length = $length;
33
    }
34
    public function validate($data) : Result
35
    {
36
        $result = (new StringType())->validate($data);
37
38
        if (!$result->isValid())
39
            return new $result;
40
41
        if (mb_strlen($data, 'UTF-8') == $this->length) {
42
            return new Success();
43
        }
44
        else return new Failure(
45
            new RuleError(RuleErrorCode::LENGTH_ERROR,
46
                'The supplied string is not of correct length'));
47
    }
48
}