IntegerNumber::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
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\Number;
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\CharType\CharType;
21
use Phypes\Rule\Primitive\NumericType;
22
use Phypes\Rule\Rule;
23
24
class IntegerNumber extends CharType implements Rule
25
{
26
    public function validate($value): Result
27
    {
28
        $numericResult = (new NumericType())->validate($value);
29
30
        if (!$numericResult->isValid())
31
            return new $numericResult;
32
33
        if (filter_var(str_replace($this->allowedSpecialChars, '', $value), FILTER_VALIDATE_INT) !== false) {
34
            return new Success();
35
        }
36
        return new Failure(new RuleError(RuleErrorCode::NOT_INTEGER, 'Value is not an integer'));
37
    }
38
}