1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus\Wrappers; |
4
|
|
|
|
5
|
|
|
class StringLengthWrapper extends Base |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $stringLengthExceeded = null; |
9
|
|
|
|
10
|
|
|
public function process() |
11
|
|
|
{ |
12
|
|
|
$this->initWrapper($this->setLocalName()); |
13
|
|
|
$this->detect(); |
14
|
|
|
|
15
|
|
|
if ($this->stringLengthExceeded) { |
16
|
|
|
$this->setScore($this->score); |
17
|
|
|
$this->setResult(); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
private function setLocalName() |
23
|
|
|
{ |
24
|
|
|
$name = str_replace(__NAMESPACE__ . '\\', '', __CLASS__); |
25
|
|
|
return str_replace('Wrapper', '', $name); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function detect() |
29
|
|
|
{ |
30
|
|
|
$params = $this->getParams(); |
31
|
|
|
|
32
|
|
|
if (empty($params)) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
foreach ($params as $param) { |
37
|
|
|
if ($param != '') { |
38
|
|
|
$data = explode("|", $param); |
39
|
|
|
if (strpos(strtolower($this->sanitizeReference()), strtolower($data[0])) !== false) { |
40
|
|
|
|
41
|
|
|
$this->setReference($data[0]); |
42
|
|
|
$this->getStringLength($data); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $data |
50
|
|
|
*/ |
51
|
|
|
private function getStringLength($data = []) |
52
|
|
|
{ |
53
|
|
|
$sanitise = $this->soteria->sanitise(); |
54
|
|
|
$length = 0; |
55
|
|
|
|
56
|
|
|
if (isset($data[1])) { |
57
|
|
|
$sanitise->disinfect($data[1], 'int'); |
58
|
|
|
$length = (int)$sanitise->result()->getOutput(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->checkStringLength($length); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param int $length |
66
|
|
|
*/ |
67
|
|
|
private function checkStringLength($length = -1) |
68
|
|
|
{ |
69
|
|
|
if ($length > 0) { |
70
|
|
|
if (mb_strlen($this->getReference()) > $length) { |
71
|
|
|
$this->stringLengthExceeded = true; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |