1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\soteria\classes; |
4
|
|
|
|
5
|
|
|
use devtoolboxuk\utilitybundle\UtilityService; |
6
|
|
|
|
7
|
|
|
class Strings |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private $arrayUtility; |
11
|
|
|
|
12
|
|
|
private $options = [ |
13
|
|
|
'stripslashes' => 1, |
14
|
|
|
'trim' => 1, |
15
|
|
|
'trimControl' => 1, |
16
|
|
|
'striptags' => 1, |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$utility = new UtilityService(); |
22
|
|
|
$this->arrayUtility = $utility->arrays(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $options |
27
|
|
|
*/ |
28
|
|
|
public function setOptions($options = []) |
29
|
|
|
{ |
30
|
|
|
$this->options = $this->arrayUtility->arrayMergeRecursiveDistinct($this->options, $options); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $data |
35
|
|
|
* @param int $length |
36
|
|
|
* @return bool|string |
37
|
|
|
*/ |
38
|
1 |
|
public function stringLength($data, $length = -1) |
39
|
|
|
{ |
40
|
1 |
|
if ($length > 0) { |
41
|
|
|
if (mb_strlen($data) > $length) { |
42
|
|
|
$data = substr($data, 0, $length); |
43
|
|
|
} |
44
|
|
|
} |
45
|
1 |
|
return $data; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $string |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
1 |
|
public function clean($string) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
|
55
|
1 |
|
$string = implode("", explode("\\", $string)); |
56
|
|
|
|
57
|
1 |
|
if ($this->getOption('stripslashes') == 1) { |
58
|
1 |
|
$string = stripslashes($string); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if ($this->getOption('trim') == 1) { |
62
|
1 |
|
$string = trim($string); |
63
|
|
|
} |
64
|
1 |
|
if ($this->getOption('trimControl') == 1) { |
65
|
1 |
|
$characters = "\\x00-\\x1F"; |
66
|
1 |
|
$string = trim( preg_replace( "/".$characters."]+/" , '' , $string ) , $characters ); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
if ($this->getOption('striptags') == 1) { |
70
|
1 |
|
$string = strip_tags($string); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return $string; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $name |
79
|
|
|
* @return mixed|null |
80
|
|
|
*/ |
81
|
1 |
|
private function getOption($name) |
82
|
|
|
{ |
83
|
1 |
|
if (!$this->hasOption($name)) { |
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $this->options[$name]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $name |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
1 |
|
private function hasOption($name) |
95
|
|
|
{ |
96
|
1 |
|
return isset($this->options[$name]); |
97
|
|
|
} |
98
|
|
|
} |
A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.
You can also find more information in the “Code” section of your repository.