Completed
Push — master ( abc4c4...ed817b )
by Rob
01:54
created

Strings::clean()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 9.2248
c 0
b 0
f 0
cc 5
nc 16
nop 1
crap 5
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 2
    public function stringLength($data, $length = -1)
39
    {
40 2
        if ($length > 0) {
41
            if (mb_strlen($data) > $length) {
42
                $data = substr($data, 0, $length);
43
            }
44
        }
45 2
        return $data;
46
    }
47
48
    /**
49
     * @param $string
50
     * @return string
51
     */
52 2
    public function clean($string)
53
    {
54
55 2
        $string = implode("", explode("\\", $string));
56
57 2
        if ($this->getOption('stripslashes') == 1) {
58 2
            $string = stripslashes($string);
59 2
        }
60
61
62 2
        if ($this->getOption('trim') == 1) {
63 2
            $string = trim($string);
64 2
        }
65
66 2
        if ($this->getOption('trimControl') == 1) {
67 2
            $string =  preg_replace( '/[[:cntrl:]]/', '',$string);
68 2
        }
69
70 2
        if ($this->getOption('striptags') == 1) {
71 2
            $string = strip_tags($string);
72 2
        }
73
74 2
        return $string;
75
    }
76
77
78
    /**
79
     * @param $name
80
     * @return mixed|null
81
     */
82 2
    private function getOption($name)
83
    {
84 2
        if (!$this->hasOption($name)) {
85
            return null;
86
        }
87
88 2
        return $this->options[$name];
89
    }
90
91
    /**
92
     * @param $name
93
     * @return bool
94
     */
95 2
    private function hasOption($name)
96
    {
97 2
        return isset($this->options[$name]);
98
    }
99
}