Completed
Pull Request — master (#243)
by Дмитрий
05:05
created

Number::humanToPlain()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 8
eloc 11
c 1
b 1
f 0
nc 5
nop 1
dl 0
loc 20
rs 7.7777
1
<?php
2
namespace PHPDaemon\Config\Entry;
3
4
use PHPDaemon\Config\Entry\Generic;
5
6
/**
7
 * Number config entry
8
 *
9
 * @package    Core
10
 * @subpackage Config
11
 *
12
 * @author     Vasily Zorin <[email protected]>
13
 */
14
class Number extends Generic
15
{
16
17
    /**
18
     * Converts human-readable value to plain
19
     * @param $value
20
     * @return int|null
21
     */
22
    public static function humanToPlain($value)
23
    {
24
        if ($value === null) {
25
            return null;
26
        }
27
        $l = substr($value, -1);
28
29
        if (($l === 'k') || ($l === 'K')) {
30
            return ((int)substr($value, 0, -1) * 1000);
31
        }
32
33
        if (($l === 'm') || ($l === 'M')) {
34
            return ((int)substr($value, 0, -1) * 1000 * 1000);
35
        }
36
37
        if (($l === 'g') || ($l === 'G')) {
38
            return ((int)substr($value, 0, -1) * 1000 * 1000 * 1000);
39
        }
40
        return (int)$value;
41
    }
42
}
43