Modifier::arrayMustContainFields()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 8.8333
cc 7
nc 5
nop 2
1
<?php
2
3
namespace DBAL\Modifiers;
4
5
class Modifier
6
{
7
    /**
8
     * Set value to null if value is empty
9
     * @param mixed $variable This should be the variable you are checking if it is empty
10
     * @return mixed Returns either NULL or the original variable
11
     */
12
    public static function setNullOnEmpty($variable)
13
    {
14
        if (empty(trim($variable))) {
15
            return null;
16
        }
17
        return $variable;
18
    }
19
    
20
    /**
21
     * Set value to 0 if value is empty
22
     * @param mixed $variable This should be the variable you are checking if it is empty
23
     * @return mixed Returns either 0 or the original variable
24
     */
25
    public static function setZeroOnEmpty($variable)
26
    {
27
        if (empty(trim($variable)) || (is_numeric($variable) && floatval($variable) == 0)) {
28
            return 0;
29
        }
30
        return $variable;
31
    }
32
    
33
    /**
34
     * Checks to see if required filed have been filled in and are not empty
35
     * @param mixed $variable This should be the variable you are checking
36
     * @return boolean Will return true if not empty and contains at least minimum number of characters else returns false
37
     */
38
    public static function isRequiredString($variable, $minStringLength = 2)
39
    {
40
        if (!empty(trim($variable)) && strlen(trim($variable)) >= $minStringLength) {
41
            return true;
42
        }
43
        return false;
44
    }
45
    
46
    /**
47
     * Checks to see if the variable is numeric or not
48
     * @param mixed $variable This should be the variable you are testing if it is a number or not
49
     * @return boolean Returns true if numeric else returns false
50
     */
51 View Code Duplication
    public static function isRequiredNumeric($variable)
52
    {
53
        if (!empty(trim($variable)) && is_numeric($variable)) {
54
            return true;
55
        }
56
        return false;
57
    }
58
    
59
    /**
60
     * Checks to see if the variable is non numeric or not
61
     * @param mixed $variable This should be the variable you are testing if it is a number or not
62
     * @return boolean Returns true if non numeric else returns false
63
     */
64 View Code Duplication
    public static function isRequiredNonNumeric($variable)
65
    {
66
        if (!empty(trim($variable)) && !is_numeric($variable)) {
67
            return true;
68
        }
69
        return false;
70
    }
71
    
72
    /**
73
     * Remove anything that isn't a number from a string
74
     * @param string $string This should be the string that you want to remove any none numerical characters from
75
     * @return string The filtered string is returned
76
     */
77
    public static function removeNoneNumeric($string)
78
    {
79
        return preg_replace("/[^0-9 ]+/", "", $string);
80
    }
81
    
82
    /**
83
     * Removes anything that isn't in the alphabet A-Z
84
     * @param string $string This should be the string that you want to remove all none alphabetical characters
85
     * @return string The filtered string is returned
86
     */
87
    public static function removeNoneAlpha($string)
88
    {
89
        return preg_replace("/[^a-zA-Z ]+/", "", $string);
90
    }
91
    
92
    /**
93
     * Removes all non Alpha-numeric characters from a string
94
     * @param string $string The string that you want to remove any non alpha-numerical characters from
95
     * @return string The filtered string is returned
96
     */
97
    public static function removeNoneAlphaNumeric($string)
98
    {
99
        return preg_replace("/[^a-zA-Z0-9 ]+/", "", $string);
100
    }
101
    
102
    /**
103
     * Checks to see if array contains all of the required fields and are not empty
104
     * @param array $mustContain an array of the values that must be included in the array
105
     * @param array $valueArray The array you are checking for the values
106
     * @return boolean If all of the fields exist and are not empty will return true else returns false
107
     */
108
    public static function arrayMustContainFields($mustContain, $valueArray)
109
    {
110
        if (is_array($mustContain) && !empty($mustContain)) {
111
            if (is_array($valueArray)) {
112
                foreach ($mustContain as $essential) {
113
                    if (!array_key_exists($essential, $valueArray) || !self::isRequiredString($valueArray[$essential])) {
114
                        return false;
115
                    }
116
                }
117
                return true;
118
            }
119
            return false;
120
        }
121
        return true;
122
    }
123
}
124