NumberFilter   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
c 0
b 0
f 0
dl 0
loc 202
rs 9.76
wmc 33

10 Methods

Rating   Name   Duplication   Size   Complexity  
A round() 0 3 1
A div() 0 11 4
A times() 0 7 3
A modulo() 0 7 3
A numberToString() 0 20 3
A plus() 0 7 3
A minus() 0 7 3
A formatMoney() 0 20 5
A sizeFormat() 0 20 5
A format() 0 15 3
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 float|int $variable
63
     * @param float|int $operand
64
     * @return float|int
65
     */
66
    public static function plus(float|int $variable, float|int $operand): float|int
67
    {
68
        if (is_float($operand) || is_float($variable)) {
69
            return (float) $variable + (float) $operand;
70
        }
71
72
        return $variable + $operand;
73
    }
74
75
    /**
76
     * subtraction
77
     * @param float|int $variable
78
     * @param float|int $operand
79
     * @return float|int
80
     */
81
    public static function minus(float|int $variable, float|int $operand): float|int
82
    {
83
        if (is_float($operand) || is_float($variable)) {
84
            return (float) $variable - (float) $operand;
85
        }
86
87
        return $variable - $operand;
88
    }
89
90
    /**
91
     * Times
92
     * @param float|int $variable
93
     * @param float|int $operand
94
     * @return float|int
95
     */
96
    public static function times(float|int $variable, float|int $operand): float|int
97
    {
98
        if (is_float($operand) || is_float($variable)) {
99
            return (float) $variable * (float) $operand;
100
        }
101
102
        return $variable * $operand;
103
    }
104
105
    /**
106
     * Modulo
107
     * @param float|int $variable
108
     * @param float|int $operand
109
     * @return int|float
110
     */
111
    public static function modulo(float|int $variable, float|int $operand): float|int
112
    {
113
        if (is_float($operand) || is_float($variable)) {
114
            return fmod((float) $variable, (float) $operand);
115
        }
116
117
        return fmod($variable, $operand);
118
    }
119
120
    /**
121
     * Division filter
122
     * @param float|int $variable
123
     * @param float|int $operand
124
     * @return int|float
125
     */
126
    public static function div(float|int $variable, float|int $operand): float|int
127
    {
128
        if ($operand == 0) {// don't use ===
129
            return $variable;
130
        }
131
132
        if (is_float($operand) || is_float($variable)) {
133
            return (float) ($variable / $operand);
134
        }
135
136
        return ($variable / $operand);
137
    }
138
139
    /**
140
     * Round the number
141
     * @param float|int $variable
142
     * @param int $number
143
     * @return float
144
     */
145
    public static function round(float|int $variable, int $number = 0): float
146
    {
147
        return round((float) $variable, (int) $number);
148
    }
149
150
    /**
151
     * Number format
152
     * @param float|int|string|null $variable
153
     * @param int|string $decimal
154
     * @param string $decimalPoint
155
     * @param string $separator
156
     * @return string
157
     */
158
    public static function format(
159
        float|int|string|null $variable,
160
        int|string $decimal = 0,
161
        string $decimalPoint = '.',
162
        string $separator = ','
163
    ): string {
164
        if (is_string($variable) && is_numeric($variable) === false) {
165
            return $variable;
166
        }
167
168
        return number_format(
169
            (float) $variable,
170
            (int) $decimal,
171
            $decimalPoint,
172
            $separator
173
        );
174
    }
175
176
    /**
177
     * Number format for money
178
     * @param float|int|string|null $variable
179
     * @param string|int $decimal
180
     * @param string $decimalPoint
181
     * @param string $separator
182
     * @return string
183
     */
184
    public static function formatMoney(
185
        float|int|string|null $variable,
186
        int|string $decimal = 0,
187
        string $decimalPoint = '.',
188
        string $separator = ','
189
    ): string {
190
        if (is_string($variable) && is_numeric($variable) === false) {
191
            return $variable;
192
        }
193
194
        $number = (string) $variable;
195
        if (strpos($number, '.') === false && strpos($number, ',') === false) {
196
            $decimal = 0;
197
        }
198
199
        return number_format(
200
            (float) $variable,
201
            (int) $decimal,
202
            $decimalPoint,
203
            $separator
204
        );
205
    }
206
207
    /**
208
     * Return the given number to string
209
     * @param float|int|string|null $variable
210
     * @return string
211
     */
212
    public static function numberToString(float|int|string|null $variable): string
213
    {
214
        $value = (string) $variable;
215
        if (stripos($value, 'e') !== false) {
216
            // PHP use scientific notation if decimal has 4 zeros
217
            // after dot. so use number format instead of
218
            list($base, $decimal) = explode('E', $value);
219
220
            // Some system use "," instead of "."
221
            if (strpos($value, ',') !== false) {
222
                $arr = explode(',', $base);
223
            } else {
224
                $arr = explode('.', $base);
225
            }
226
            $separator = '%.' . (string)(strlen($arr[1]) + (abs((int)$decimal) - 1)) . 'f';
227
228
            $value = sprintf($separator, $variable);
229
        }
230
231
        return str_replace(',', '.', $value);
232
    }
233
234
    /**
235
     * Units format
236
     * @param float|int|string|null $variable
237
     * @param int|string $precision
238
     * @return float|int|string|null
239
     */
240
    public static function sizeFormat(
241
        float|int|string|null $variable,
242
        int|string $precision = 2
243
    ): float|int|string|null {
244
        if (is_string($variable) && is_numeric($variable) === false) {
245
            return $variable;
246
        }
247
248
        $size = (double) $variable;
249
        if ($size > 0) {
250
            $base = log($size) / log(1024);
251
            $suffixes = ['B', 'K', 'M', 'G', 'T'];
252
            $suffix = '';
253
            if (isset($suffixes[floor($base)])) {
254
                $suffix = $suffixes[floor($base)];
255
            }
256
            return round(pow(1024, $base - floor($base)), (int) $precision) . $suffix;
257
        }
258
259
        return $variable;
260
    }
261
}
262