-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContainsUppercaseCharacters.php
More file actions
83 lines (74 loc) · 2.03 KB
/
Copy pathContainsUppercaseCharacters.php
File metadata and controls
83 lines (74 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Morebec\Validator\Rule;
use InvalidArgumentException;
use Morebec\Validator\ValidationRuleInterface;
class ContainsUppercaseCharacters implements ValidationRuleInterface
{
/**
* @var int
*/
private $numberCharacters;
/**
* @var bool
*/
private $strict;
/**
* @var string|null
*/
private $message;
/**
* ContainsUppercaseCharacters constructor.
* @param int $numberCharacters
* @param bool $strict
* @param string|null $message
*/
public function __construct(
int $numberCharacters,
bool $strict,
?string $message = null
)
{
if($numberCharacters<0)
throw new InvalidArgumentException();
$this->numberCharacters = $numberCharacters;
$this->strict = $strict;
$this->message = $message;
}
/**
* Validates a value according to this rule and returns if it is valid or not
* @param mixed $v
* @return bool true if valid, otherwise false
*/
public function validate($v): bool
{
if($this->strict){
return $this->countUpperCase($v)<=$this->numberCharacters;
}
return $this->countUpperCase($v)>=$this->numberCharacters;
}
/**
* Returns the message to be used in case the validation did not pass
* @param mixed $v the value that did not pass the validation
* @return string
*/
public function getMessage($v): string
{
if($this->message){
return $this->message;
}
if($this->strict){
return "Number of uppercase characters exceeds ".${$this->numberCharacters};
}
return "Number of uppercase characters should exceed ".${$this->numberCharacters};
}
/**
* @param string $message
* @return int
*/
private function countUpperCase(string $message): int
{
$lowerCase = strtolower($message);
$similar = similar_text($message, $lowerCase);
return strlen($message)-$similar;
}
}