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
|
|
|
* An argument type that can be used for argument collecting. |
14
|
|
|
* |
15
|
|
|
* @property \CharlotteDunois\Livia\Client $client The client which initiated the instance. |
16
|
|
|
* @property string $id The argument type ID. |
17
|
|
|
*/ |
18
|
|
|
abstract class ArgumentType implements \Serializable { |
19
|
|
|
/** |
20
|
|
|
* The client which initiated the instance. |
21
|
|
|
* @var \CharlotteDunois\Livia\Client |
22
|
|
|
*/ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The argument type ID. |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @internal |
33
|
|
|
*/ |
34
|
|
|
function __construct(\CharlotteDunois\Livia\Client $client, string $id) { |
35
|
|
|
$this->client = $client; |
36
|
|
|
$this->id = $id; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @return bool |
42
|
|
|
* @throws \Exception |
43
|
|
|
* @internal |
44
|
|
|
*/ |
45
|
|
|
function __isset($name) { |
46
|
|
|
try { |
47
|
|
|
return $this->$name !== null; |
48
|
|
|
} catch (\RuntimeException $e) { |
49
|
|
|
if($e->getTrace()[0]['function'] === '__get') { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw $e; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $name |
59
|
|
|
* @return mixed |
60
|
|
|
* @throws \Exception |
61
|
|
|
* @internal |
62
|
|
|
*/ |
63
|
|
|
function __get($name) { |
64
|
|
|
if(\property_exists($this, $name)) { |
65
|
|
|
return $this->$name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw new \RuntimeException('Unknown property '.\get_class($this).'::$'.$name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
* @internal |
74
|
|
|
*/ |
75
|
|
|
function serialize() { |
76
|
|
|
$vars = \get_object_vars($this); |
77
|
|
|
|
78
|
|
|
unset($vars['client']); |
79
|
|
|
|
80
|
|
|
return \serialize($vars); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
* @internal |
86
|
|
|
*/ |
87
|
|
|
function unserialize($vars) { |
88
|
|
|
if(\CharlotteDunois\Yasmin\Models\ClientBase::$serializeClient === null) { |
89
|
|
|
throw new \Exception('Unable to unserialize a class without ClientBase::$serializeClient being set'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$vars = \unserialize($vars); |
93
|
|
|
|
94
|
|
|
foreach($vars as $name => $val) { |
95
|
|
|
$this->$name = $val; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->client = \CharlotteDunois\Yasmin\Models\ClientBase::$serializeClient; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Validates a value against the type. If the return is a promise, the promise has to resolve with one of the other return types. |
103
|
|
|
* @param string $value Value to validate. |
104
|
|
|
* @param \CharlotteDunois\Livia\Commands\Context $context Message the value was obtained from. |
105
|
|
|
* @param \CharlotteDunois\Livia\Arguments\Argument|null $arg Argument the value obtained from. |
106
|
|
|
* @return bool|string|\React\Promise\ExtendedPromiseInterface |
107
|
|
|
*/ |
108
|
|
|
abstract function validate(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null); |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Parses a value into an usable value. If the return is a promise, the promise has to resolve with one of the other return types. |
112
|
|
|
* @param string $value Value to parse. |
113
|
|
|
* @param \CharlotteDunois\Livia\Commands\Context $context Message the value was obtained from. |
114
|
|
|
* @param \CharlotteDunois\Livia\Arguments\Argument|null $arg Argument the value obtained from. |
115
|
|
|
* @return mixed|null|\React\Promise\ExtendedPromiseInterface |
116
|
|
|
* @throws \RangeException |
117
|
|
|
*/ |
118
|
|
|
abstract function parse(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null); |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Checks whether a value is considered to be empty. This determines whether the default value for an argument should be used and changes the response to the user under certain circumstances. |
122
|
|
|
* @param mixed $value Value to check. |
123
|
|
|
* @param \CharlotteDunois\Livia\Commands\Context $context Message the value was obtained from. |
124
|
|
|
* @param \CharlotteDunois\Livia\Arguments\Argument|null $arg Argument the value obtained from. |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
function isEmpty($value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) { |
128
|
|
|
if(\is_array($value) || \is_object($value)) { |
129
|
|
|
return (empty($value)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return (\mb_strlen(\trim(((string) $value))) === 0); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|