Passed
Push — master ( aff02c...3db3b7 )
by Dedipyaman
01:35
created

ExactLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Rule;
21
22
class ExactLength implements Rule
23
{
24
    /**
25
     * @var int $length
26
     */
27
    private $length;
28
29
    public function __construct(int $length)
30
    {
31
        $this->length = $length;
32
    }
33
    public function validate($data) : Result
34
    {
35
        if (mb_strlen($data, 'UTF-8') >= $this->length) {
36
            return new Success();
37
        }
38
        else return new Failure(
39
            new RuleError(RuleErrorCode::LENGTH_ERROR,
40
                'The supplied string is not of correct length'));
41
    }
42
}