Test Failed
Push — master ( fb34f5...d8e6f2 )
by Adam
02:11
created

Modifier::setNullOnEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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