|
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 BooleanArgumentType extends ArgumentType { |
|
17
|
|
|
/** |
|
18
|
|
|
* Truthy values. |
|
19
|
|
|
* @var string[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $truthy = array('true', 't', 'yes', 'y', 'on', 'enable', 'enabled', '1', '+'); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Falsey values. |
|
25
|
|
|
* @var string[] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $falsey = array('false', 'f', 'no', 'n', 'off', 'disable', 'disabled', '0', '-'); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @internal |
|
31
|
|
|
*/ |
|
32
|
|
|
function __construct(\CharlotteDunois\Livia\Client $client) { |
|
33
|
|
|
parent::__construct($client, 'boolean'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
* @return bool|string|\React\Promise\ExtendedPromiseInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
function validate(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) { |
|
41
|
|
|
$value = \mb_strtolower($value); |
|
42
|
|
|
return (\in_array($value, $this->truthy) || \in_array($value, $this->falsey)); |
|
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
|
|
|
$value = \mb_strtolower($value); |
|
51
|
|
|
if(\in_array($value, $this->truthy)) { |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if(\in_array($value, $this->falsey)) { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|