Passed
Push — develop ( ca1430...6549ae )
by nguereza
02:15
created

NumberFilter::numberToString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Template
5
 *
6
 * Platine Template is a template engine that has taken a lot of inspiration from Django.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Template
11
 * Copyright (c) 2014 Guz Alexander, http://guzalexander.com
12
 * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com
13
 * Copyright (c) 2006 Mateo Murphy
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to deal
17
 * in the Software without restriction, including without limitation the rights
18
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
 * copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in all
23
 * copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
 * SOFTWARE.
32
 */
33
34
/**
35
 *  @file NumberFilter.php
36
 *
37
 *  The Number Filter class
38
 *
39
 *  @package    Platine\Template\Filter
40
 *  @author Platine Developers Team
41
 *  @copyright  Copyright (c) 2020
42
 *  @license    http://opensource.org/licenses/MIT  MIT License
43
 *  @link   https://www.platine-php.com
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template\Filter;
51
52
use Platine\Template\Parser\AbstractFilter;
53
54
/**
55
 * Class NumberFilter
56
 * @package Platine\Template\Filter
57
 */
58
class NumberFilter extends AbstractFilter
59
{
60
    /**
61
     * Addition
62
     * @param mixed $variable
63
     * @param mixed $operand
64
     * @return float|int|mixed
65
     */
66
    public static function plus($variable, $operand)
67
    {
68
        if (!is_numeric($variable) || !is_numeric($operand)) {
69
            return $variable;
70
        }
71
72
        if (is_float($operand) || is_float($variable)) {
73
            return (float) $variable + (float) $operand;
74
        }
75
76
        return (int) $variable + (int) $operand;
77
    }
78
79
    /**
80
     * subtraction
81
     * @param mixed $variable
82
     * @param mixed $operand
83
     * @return int|float|mixed
84
     */
85
    public static function minus($variable, $operand)
86
    {
87
        if (!is_numeric($variable) || !is_numeric($operand)) {
88
            return $variable;
89
        }
90
91
        if (is_float($operand) || is_float($variable)) {
92
            return (float) $variable - (float) $operand;
93
        }
94
95
        return (int) $variable - (int) $operand;
96
    }
97
98
    /**
99
     * Times
100
     * @param mixed $variable
101
     * @param mixed $operand
102
     * @return int|float|mixed
103
     */
104
    public static function times($variable, $operand)
105
    {
106
        if (!is_numeric($variable) || !is_numeric($operand)) {
107
            return $variable;
108
        }
109
110
        if (is_float($operand) || is_float($variable)) {
111
            return (float) $variable * (float) $operand;
112
        }
113
114
        return (int) $variable * (int) $operand;
115
    }
116
117
    /**
118
     * Modulo
119
     * @param mixed $variable
120
     * @param mixed $operand
121
     * @return int|float|mixed
122
     */
123
    public static function modulo($variable, $operand)
124
    {
125
        if (!is_numeric($variable) || !is_numeric($operand)) {
126
            return $variable;
127
        }
128
129
        if (is_float($operand) || is_float($variable)) {
130
            return fmod((float) $variable, (float) $operand);
131
        }
132
133
        return fmod((int) $variable, (int) $operand);
134
    }
135
136
    /**
137
     * Division filter
138
     * @param mixed $variable
139
     * @param mixed $operand
140
     * @return int|float|mixed
141
     */
142
    public static function div($variable, $operand)
143
    {
144
        if (!is_numeric($variable) || !is_numeric($operand) || $operand == 0) {
145
            return $variable;
146
        }
147
148
        if (is_float($operand) || is_float($variable)) {
149
            return (float) ($variable / $operand);
150
        }
151
152
        return (int) ($variable / $operand);
153
    }
154
155
    /**
156
     * Round the number
157
     * @param mixed $variable
158
     * @param mixed $number
159
     * @return float|mixed
160
     */
161
    public static function round($variable, $number = 0)
162
    {
163
        if (!is_numeric($variable) || !is_numeric($number)) {
164
            return $variable;
165
        }
166
167
        return round((float) $variable, (int) $number);
168
    }
169
170
    /**
171
     * Number format
172
     * @param mixed $variable
173
     * @param mixed $decimal
174
     * @param string $decimalPoint
175
     * @param string $separator
176
     * @return float|mixed
177
     */
178
    public static function format(
179
        $variable,
180
        $decimal = 0,
181
        $decimalPoint = '.',
182
        $separator = ','
183
    ) {
184
        if (!is_numeric($variable)) {
185
            return $variable;
186
        }
187
188
        return number_format(
189
            (float) $variable,
190
            (int) $decimal,
191
            $decimalPoint,
192
            $separator
193
        );
194
    }
195
196
    /**
197
     * Number format for money
198
     * @param mixed $variable
199
     * @param mixed $decimal
200
     * @param string $decimalPoint
201
     * @param string $separator
202
     * @return float|mixed
203
     */
204
    public static function formatMoney(
205
        $variable,
206
        $decimal = 0,
207
        $decimalPoint = '.',
208
        $separator = ','
209
    ) {
210
        if (!is_numeric($variable)) {
211
            return $variable;
212
        }
213
214
        $number = (string) $variable;
215
        if (strpos($number, '.') === false && strpos($number, ',') === false) {
216
            $decimal = 0;
217
        }
218
219
        return number_format(
220
            (float) $variable,
221
            (int) $decimal,
222
            $decimalPoint,
223
            $separator
224
        );
225
    }
226
227
    /**
228
     * Return the given number to string
229
     * @param mixed $variable
230
     * @return string|mixed
231
     */
232
    public static function numberToString($variable)
233
    {
234
        $value = (string) $variable;
235
        if (stripos($value, 'e') !== false) {
236
            // PHP use scientific notation if decimal has 4 zeros
237
            // after dot. so use number format instead of
238
            list($base, $decimal) = explode('E', $value);
239
240
            // Some system use "," instead of "."
241
            if (strpos($value, ',') !== false) {
242
                $arr = explode(',', $base);
243
            } else {
244
                $arr = explode('.', $base);
245
            }
246
            $separator = '%.' . (string)(strlen($arr[1]) + (abs($decimal) - 1)) . 'f';
0 ignored issues
show
Bug introduced by
$decimal of type string is incompatible with the type double|integer expected by parameter $num of abs(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

246
            $separator = '%.' . (string)(strlen($arr[1]) + (abs(/** @scrutinizer ignore-type */ $decimal) - 1)) . 'f';
Loading history...
247
248
            $value = sprintf($separator, $variable);
249
        }
250
251
        return str_replace(',', '.', $value);
252
    }
253
254
    /**
255
     * Units format
256
     * @param mixed $variable
257
     * @param mixed $precision
258
     * @return string
259
     */
260
    public static function sizeFormat(
261
        $variable,
262
        $precision = 2
263
    ) {
264
        if (!is_numeric($variable)) {
265
            return $variable;
266
        }
267
268
        $size = (double) $variable;
269
        if ($size > 0) {
270
            $base = log($size) / log(1024);
271
            $suffixes = ['B', 'K', 'M', 'G', 'T'];
272
            $suffix = '';
273
            if (isset($suffixes[floor($base)])) {
274
                $suffix = $suffixes[floor($base)];
275
            }
276
            return round(pow(1024, $base - floor($base)), (int) $precision) . $suffix;
277
        }
278
279
        return $variable;
280
    }
281
}
282