Completed
Push — master ( acc636...10741d )
by Daniel
13:51 queued 02:58
created

InputSanitizer   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 98.15%

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 0
dl 0
loc 109
ccs 53
cts 54
cp 0.9815
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C sanitize() 0 51 14
A sanitizeAll() 0 16 3
A isJson() 0 14 3
1
<?php
2
3
/**
4
 * Part of the InputSanitizer package.
5
 *
6
 * @package    InputSanitizer
7
 * @version    1.0.0
8
 * @author     Arthur Lorent <[email protected]>, Daniel Lucas <[email protected]>
9
 * @license    MIT
10
 * @copyright  (c) 2006-2016, Acid Solutions SARL
11
 * @link       https://acid.fr
12
 */
13
14
namespace AcidSolutions\InputSanitizer;
15
16
class InputSanitizer
17
{
18
    /**
19
     * Cleans given input and returns cleaned data.
20
     *
21
     * @param mixed $entry           The data to clean
22
     * @param mixed $default         Value to return by default if the input data is falsy
23
     * @param bool  $jsonDecodeAssoc Should json_decode return an associative array instead of StdClass?
24
     *
25
     * @return mixed
26
     */
27 44
    public function sanitize($entry, $default = null, $jsonDecodeAssoc = false)
28
    {
29
        // we remove useless spaces
30 44
        if (is_string($entry)) {
31 36
            $entry = trim($entry);
32 18
        }
33
34
        // we sanitize the value
35 22
        switch (true) {
36 44
            case $entry === '':
37 42
            case $entry === 'null':
38 20
                $return = null;
39 20
                break;
40 36
            case $entry === 'false':
41 12
                $return = false;
42 12
                break;
43 36
            case $entry === 'true':
44 34
            case $entry === 'on':
45 16
                $return = true;
46 16
                break;
47 32
            case is_numeric($entry):
48 12
                if (((int)$entry != $entry)) {
49 4
                    $return = doubleval($entry);
50 2
                } else {
51 12
                    $return = intval($entry);
52
                }
53 12
                break;
54 28
            case $this->isJson($entry):
55 16
                $return = json_decode($entry, $jsonDecodeAssoc);
56
57 16
                if (is_array($return)) {
58 4
                    $return = (array) $this->sanitizeAll($return);
59 2
                }
60
61 16
                break;
62 14
            case is_array($entry):
63 4
                $return = $this->sanitizeAll($entry);
64 4
                break;
65 4
            default:
66 14
                $return = $entry;
67 14
                break;
68 20
        };
69
70
        // if the value is null or false, return the default value
71 40
        if (isset($default) && !$return) {
72 8
            return $default;
73
        }
74
75
        // we return the sanitized value
76 36
        return $return;
77
    }
78
79
    /**
80
     * Recursive method to sanitize deep array of data.
81
     *
82
     * @param array $entries Array of data to sanitize
83
     *
84
     * @return array
85
     */
86 8
    private function sanitizeAll(array $entries)
87
    {
88 8
        $return = [];
89
        // for each entry contained into the array
90 8
        foreach ($entries as $key => $entry) {
91
            // we sanitize it
92 8
            if (is_array($entry)) {
93 8
                $return[$key] = $this->sanitizeAll($entry);
94 4
            } else {
95 8
                $return[$key] = $this->sanitize($entry);
96
            }
97 4
        }
98
99
        // we return the sanitized data
100 8
        return $return;
101
    }
102
103
    /**
104
     * Check if an item is JSON.
105
     *
106
     * @param $string Item to check
107
     *
108
     * @return bool True if the given item is JSON
109
     */
110 28
    private function isJson($string)
111
    {
112 28
        if (is_array($string)) {
113 4
            return false;
114
        }
115
116
        try {
117 28
            json_decode($string);
118 16
        } catch (\ErrorException $e) {
119
            return false;
120
        }
121
        
122 24
        return (json_last_error() == JSON_ERROR_NONE);
123
    }
124
}
125