IntegerArgumentType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Livia
4
 * Copyright 2017-2019 Charlotte Dunois, All Rights Reserved
5
 *
6
 * Website: https://charuru.moe
7
 * License: https://github.com/CharlotteDunois/Livia/blob/master/LICENSE
8
*/
9
10
namespace CharlotteDunois\Livia\Types;
11
12
/**
13
 * {@inheritdoc}
14
 * @internal
15
 */
16
class IntegerArgumentType extends ArgumentType {
17
    /**
18
     * @internal
19
     */
20
    function __construct(\CharlotteDunois\Livia\Client $client) {
21
        parent::__construct($client, 'integer');
22
    }
23
    
24
    /**
25
     * {@inheritdoc}
26
     * @return bool|string|\React\Promise\ExtendedPromiseInterface
27
     */
28
    function validate(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) {
29
        $value = \filter_var($value, \FILTER_VALIDATE_INT);
30
        if($value === false) {
31
            return false;
32
        }
33
        
34
        if($arg->min !== null && $value < $arg->min) {
35
            return 'Please enter a number above or exactly '.$arg->min.'.';
36
        }
37
        
38
        if($arg->max !== null && $value > $arg->max) {
39
            return 'Please enter a number below or exactly '.$arg->max.'.';
40
        }
41
        
42
        return true;
43
    }
44
    
45
    /**
46
     * {@inheritdoc}
47
     * @return mixed|null|\React\Promise\ExtendedPromiseInterface
48
     */
49
    function parse(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) {
50
        return ((int) $value);
51
    }
52
}
53