Completed
Push — master ( e71512...aa852e )
by Dmytro
03:40
created

Tools::toFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Asymptix\core;
4
5
/**
6
 * Common tools methods.
7
 *
8
 * @category Asymptix PHP Framework
9
 * @author Dmytro Zarezenko <[email protected]>
10
 * @copyright (c) 2009 - 2016, Dmytro Zarezenko
11
 *
12
 * @git https://github.com/Asymptix/Framework
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
class Tools {
16
17
    /**
18
     * Returns TRUE if filter exists or FALSE otherwise.
19
     *
20
     * @param string $filterName Name of the filter field.
21
     * @return bool
22
     */
23
    public static function isFilterExists($filterName) {
24
        return isset($_FILTER[$filterName]);
0 ignored issues
show
Bug introduced by
The variable $_FILTER seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
25
    }
26
27
    /**
28
     * Returns global filter value if exists.
29
     *
30
     * @global array $_FILTER Global filters array.
31
     * @param string $filterName Name of the filter field.
32
     * @param mixed $defaultValue Default value if value doesn't exist.
33
     *
34
     * @return mixed
35
     */
36
    public static function getFilterValue($filterName, $defaultValue = null) {
37
        global $_FILTER;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
38
39
        return isset($_FILTER[$filterName]) ? $_FILTER[$filterName] : $defaultValue;
40
    }
41
42
    /**
43
     * Checks if value is an integer or integer string.
44
     *
45
     * @param mixed $value Data.
46
     * @return bool Returns TRUE if $input is a integer, FALSE otherwise.
47
     */
48
    public static function isInteger($value) {
49
        if (is_array($value)) {
50
            return false;
51
        }
52
53
        $strVal = trim(strval($value));
54
        if (strlen($strVal) && $strVal[0] == '-') {
55
            return ctype_digit(substr($strVal, 1));
56
        }
57
58
        return ctype_digit($strVal);
59
    }
60
61
    /**
62
     * Check data for double.
63
     *
64
     * @param mixed $value Data.
65
     * @return bool Returns TRUE if $input is a double value, FALSE otherwise.
66
     */
67
    public static function isDouble($value) {
68
        return is_float($value);
69
    }
70
71
    /**
72
     * Check data for float.
73
     *
74
     * @param mixed $value Data.
75
     * @return bool Returns TRUE if $input is a float value, FALSE otherwise.
76
     */
77
    public static function isFloat($value) {
78
        return self::isDouble($value);
79
    }
80
81
    /**
82
     * Verify if some string is string representation of some double value.
83
     * Decimal point may be `.` and `,`.
84
     *
85
     * @param string $value
86
     * @return bool
87
     */
88
    public static function isDoubleString($value) {
89
        $doubleValue = (float)$value;
90
        $stringValue = str_replace(",", ".", (string)$value);
91
92
        if (is_numeric($stringValue)) {
93
            return true;
94
        }
95
96
        if ($stringValue === (string)$doubleValue) {
97
            return true;
98
        }
99
100
        return false;
101
    }
102
103
    /**
104
     * Convert string representation of some double value to double.
105
     *
106
     * @param string $value
107
     * @return float
108
     */
109
    public static function toDouble($value) {
110
        return (float)str_replace(",", ".", (string)$value);
111
    }
112
113
    /**
114
     * Convert string representation of some double value to float.
115
     *
116
     * @param string $value
117
     * @return float
118
     */
119
    public static function toFloat($value) {
120
        return self::toDouble($value);
121
    }
122
123
    /**
124
     * Finds whether a variable is a number or a numeric string.
125
     *
126
     * @param mixed $value The variable being evaluated.
127
     *
128
     * @return bool Returns TRUE if $input is a number or a numeric string,
129
     *           FALSE otherwise.
130
     */
131
    public static function isNumeric($value) {
132
        return is_numeric(strval($value));
133
    }
134
135
    /**
136
     * Find whether a variable is a boolean value.
137
     *
138
     * @param mixed $value The variable being evaluated.
139
     *
140
     * @return bool bool Returns TRUE if $input is a number or a numeric string,
141
     *           FALSE otherwise.
142
     */
143
    public static function isBoolean($value) {
144
        return is_bool($value);
145
    }
146
147
    /**
148
     * Find whether a variable is a string value.
149
     *
150
     * @param mixed $value The variable being evaluated.
151
     *
152
     * @return bool bool Returns TRUE if $input is a number or a numeric string,
153
     *           FALSE otherwise.
154
     */
155
    public static function isString($value) {
156
        return is_string($value);
157
    }
158
159
    /**
160
     * Find whether a variable is an object.
161
     *
162
     * @param mixed $object The variable being evaluated.
163
     *
164
     * @return bool Returns TRUE if $object is an object, FALSE otherwise.
165
     */
166
    public static function isObject(&$object) {
167
        if (isset($object) && is_object($object)) {
168
            return !empty($object);
169
        }
170
171
        return false;
172
    }
173
174
    /**
175
     * Find whether a $object is an instance of class $className.
176
     *
177
     * @param mixed $object The object(variable) being evaluated.
178
     * @param string $className The name of the class.
179
     *
180
     * @return bool
181
     */
182
    public static function isInstanceOf(&$object, $className) {
183
        if (is_object($className)) {
184
            $className = get_class($className);
185
        }
186
187
        return (self::isObject($object) && ($object instanceof $className));
188
    }
189
190
}
191