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\Type; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use Phypes\Result\Failure; |
16
|
|
|
use Phypes\Validator\UsernameValidator; |
17
|
|
|
use Phypes\Validator\Validator; |
18
|
|
|
use function Phypes\getOptionalValue; |
19
|
|
|
|
20
|
|
|
class Username implements Type |
21
|
|
|
{ |
22
|
|
|
const OPT_MIN_LEN = 0; |
23
|
|
|
const OPT_MAX_LEN = 1; |
24
|
|
|
const OPT_ALLOWED_SPECIAL_CHARS = 2; |
25
|
|
|
/** |
26
|
|
|
* @var string $username |
27
|
|
|
*/ |
28
|
|
|
private $username; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function __construct(string $username, $options = [], Validator $validator = null) |
32
|
|
|
{ |
33
|
|
|
if ($validator == null) { |
34
|
|
|
// use the default validator |
35
|
|
|
$validator = new UsernameValidator( |
36
|
|
|
getOptionalValue(self::OPT_MIN_LEN, $options, 4), |
37
|
|
|
getOptionalValue(self::OPT_MAX_LEN, $options, 12), |
38
|
|
|
getOptionalValue(self::OPT_ALLOWED_SPECIAL_CHARS, $options, ['-', '_'])); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$result = $validator->validate($username, $options); |
|
|
|
|
42
|
|
|
|
43
|
|
|
if (!$result->isValid()) { |
44
|
|
|
/** |
45
|
|
|
* @var Failure $result |
46
|
|
|
*/ |
47
|
|
|
$error = $result->getFirstError(); |
48
|
|
|
throw new \InvalidArgumentException($error->getMessage(), $error->getCode()); |
49
|
|
|
} |
50
|
|
|
$this->username = $username; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function __toString(): string |
54
|
|
|
{ |
55
|
|
|
return $this->username; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getValue() |
59
|
|
|
{ |
60
|
|
|
return $this->username; |
61
|
|
|
} |
62
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.