Passed
Push — master ( 470ad5...955b74 )
by Dedipyaman
02:14
created

AlphaNumeric::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
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\CharType;
13
14
use Phypes\Error\RuleError;
15
use Phypes\Error\RuleErrorCode;
16
use Phypes\Result\Failure;
17
use Phypes\Result\Result;
18
use Phypes\Result\Success;
19
use Phypes\Rule\Primitive\StringType;
20
use Phypes\Rule\Rule;
21
22
class AlphaNumeric extends CharType implements Rule
23
{
24
    // TODO: Add AlphaNumeric test
25
26
    public function validate($data): Result
27
    {
28
        $result = (new StringType())->validate($data);
29
30
        if (!$result->isValid())
31
            return new $result;
32
33
        if (ctype_alnum(str_replace($this->allowedSpecialChars, '', $data)))
34
            return new Success();
35
        else
36
            return new Failure(new RuleError(RuleErrorCode::NOT_ALNUM, 'String is not Alphanumeric.'));
37
    }
38
}