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

Time   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 43
rs 10
c 1
b 1
f 0
wmc 9
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
D humanToPlain() 0 34 9
1
<?php
2
namespace PHPDaemon\Config\Entry;
3
4
use PHPDaemon\Config\Entry\Generic;
5
6
/**
7
 * Time config entry
8
 *
9
 * @package    Core
10
 * @subpackage Config
11
 *
12
 * @author     Vasily Zorin <[email protected]>
13
 */
14
class Time extends Generic
15
{
16
17
    /**
18
     * Converts human-readable value to plain
19
     * @param $value
20
     * @return int
21
     */
22
    public static function humanToPlain($value)
23
    {
24
        $time = 0;
25
26
        preg_replace_callback('~(\d+(\.\d+)?)\s*([smhd])\s*|(.+)~i', function ($m) use (&$time) {
27
            if (isset($m[4]) && ($m[4] !== '')) {
28
                $time = false;
29
            }
30
31
            if ($time === false) {
32
                return;
33
            }
34
35
            if (!empty($m[2])) {
36
                $n = (float)$m[1];
37
            } else {
38
                $n = (int)$m[1];
39
            }
40
41
            $l = strtolower($m[3]);
42
43
            if ($l === 's') {
44
                $time += $n;
45
            } elseif ($l === 'm') {
46
                $time += $n * 60;
47
            } elseif ($l === 'h') {
48
                $time += $n * 60 * 60;
49
            } elseif ($l === 'd') {
50
                $time += $n * 60 * 60 * 24;
51
            }
52
        }, $value);
53
54
        return $time;
55
    }
56
}
57