StringArgumentType::validate()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 7
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 14
rs 9.2222
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 StringArgumentType extends ArgumentType {
17
    /**
18
     * @internal
19
     */
20
    function __construct(\CharlotteDunois\Livia\Client $client) {
21
        parent::__construct($client, 'string');
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
        if(\mb_strlen($value) === 0) {
30
            return false;
31
        }
32
        
33
        if($arg->min !== null && \mb_strlen($value) < $arg->min) {
34
            return 'Please enter something above or exactly '.$arg->min.' characters in length.';
35
        }
36
        
37
        if($arg->max !== null && \mb_strlen($value) > $arg->max) {
38
            return 'Please enter a number below or exactly '.$arg->max.' characters in length.';
39
        }
40
        
41
        return true;
42
    }
43
    
44
    /**
45
     * {@inheritdoc}
46
     * @return mixed|null|\React\Promise\ExtendedPromiseInterface
47
     */
48
    function parse(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) {
49
        return $value;
50
    }
51
}
52