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\Exception\InvalidValue; |
16
|
|
|
use Phypes\Result\Failure; |
17
|
|
|
use Phypes\Validator\UsernameValidator; |
18
|
|
|
use Phypes\Validator\Validator; |
19
|
|
|
use function Phypes\getOptionalValue; |
20
|
|
|
|
21
|
|
|
class Username implements Type |
22
|
|
|
{ |
23
|
|
|
const OPT_MIN_LEN = 0; |
24
|
|
|
const OPT_MAX_LEN = 1; |
25
|
|
|
const OPT_ALLOWED_SPECIAL_CHARS = 2; |
26
|
|
|
/** |
27
|
|
|
* @var string $username |
28
|
|
|
*/ |
29
|
|
|
private $username; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Username constructor. |
33
|
|
|
* @param string $username |
34
|
|
|
* @param array $options |
35
|
|
|
* @param Validator|null $validator |
36
|
|
|
* @throws InvalidValue |
37
|
|
|
* @throws \Phypes\Exception\InvalidRule |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $username, $options = [], Validator $validator = null) |
40
|
|
|
{ |
41
|
|
|
if ($validator == null) { |
42
|
|
|
// use the default validator |
43
|
|
|
$validator = new UsernameValidator( |
44
|
|
|
getOptionalValue(self::OPT_MIN_LEN, $options, 4), |
45
|
|
|
getOptionalValue(self::OPT_MAX_LEN, $options, 12), |
46
|
|
|
getOptionalValue(self::OPT_ALLOWED_SPECIAL_CHARS, $options, ['-', '_'])); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$result = $validator->validate($username, $options); |
|
|
|
|
50
|
|
|
|
51
|
|
|
if (!$result->isValid()) { |
52
|
|
|
/** |
53
|
|
|
* @var Failure $result |
54
|
|
|
*/ |
55
|
|
|
throw new InvalidValue($result->getFirstError()->getMessage(), $result->getErrors()); |
56
|
|
|
} |
57
|
|
|
$this->username = $username; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function __toString(): string |
61
|
|
|
{ |
62
|
|
|
return $this->username; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getValue() |
66
|
|
|
{ |
67
|
|
|
return $this->username; |
68
|
|
|
} |
69
|
|
|
} |
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.