Passed
Push — v3 ( dc00fa...37cdd6 )
by
unknown
02:41
created

Secondstohuman::iterate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.9
cc 4
nc 4
nop 1
crap 4
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Filters;
20
21
use GameQ\Helpers\Arr;
22
use GameQ\Server;
23
use RecursiveArrayIterator;
24
25
/**
26
 * Class Secondstohuman
27
 *
28
 * This Filter converts seconds into a human readable time string 'hh:mm:ss'. This is mainly for converting
29
 * a player's connected time into a readable string. Note that most game servers DO NOT return a player's connected
30
 * time. Source (A2S) based games generally do but not always. This class can also be used to convert other time
31
 * responses into readable time
32
 *
33
 * @package GameQ\Filters
34
 * @author  Austin Bischoff <[email protected]>
35
 */
36
class Secondstohuman extends Base
37
{
38
39
    /**
40
     * The options key for setting the data key(s) to look for to convert
41
     */
42
    const OPTION_TIMEKEYS = 'timekeys';
43
44
    /**
45
     * The result key added when applying this filter to a result
46
     */
47
    const RESULT_KEY = 'gq_%s_human';
48
49
    /**
50
     * Holds the default 'time' keys from the response array.  This is key is usually 'time' from A2S responses
51
     *
52
     * @var array
53
     */
54
    protected $timeKeysDefault = ['time'];
55
56
    /**
57
     * Secondstohuman constructor.
58
     *
59
     * @param array $options
60
     */
61 12
    public function __construct(array $options = [])
62
    {
63
        // Check for passed keys
64 12
        if (! array_key_exists(self::OPTION_TIMEKEYS, $options)) {
65
            // Use default
66 6
            $options[self::OPTION_TIMEKEYS] = $this->timeKeysDefault;
67
        } else {
68
            // Used passed key(s) and make sure it is an array
69 6
            $options[self::OPTION_TIMEKEYS] = (!is_array($options[self::OPTION_TIMEKEYS])) ?
70 6
                [$options[self::OPTION_TIMEKEYS]] : $options[self::OPTION_TIMEKEYS];
71
        }
72
73 12
        parent::__construct($options);
74 2
    }
75
76
    /**
77
     * Apply this filter to the result data
78
     *
79
     * @param array  $result
80
     * @param Server $server
81
     *
82
     * @return array
83
     */
84 12
    public function apply(array $result, Server $server)
85
    {
86 10
        return Arr::recursively($result, function ($value, $key, RecursiveArrayIterator $iterator) {
87
            if (
88
                /* Only process whitelisted keys */
89 12
                (in_array($key, $this->options[self::OPTION_TIMEKEYS])) &&
90
                /* Only process numeric values (float, integer, string) */
91 12
                (is_numeric($value))
92
            ) {
93
                /* Ensure the value is float */
94 12
                if (! is_float($value)) {
95
                    $value = floatval($value);
96
                }
97
98
                /* Add a new element to the result */
99 12
                $iterator->offsetSet(
100
                    /* Modify the current key */
101 12
                    sprintf(self::RESULT_KEY, $key),
102
                    /* Format the provided time */
103 12
                    sprintf(
104 12
                        '%02d:%02d:%02d',
105 12
                        (int)floor($value / 3600),    // Hours
106 12
                        (int)fmod(($value / 60), 60), // Minutes
107 12
                        (int)fmod($value, 60)         // Seconds
108 10
                    )
109 10
                );
110
            }
111 12
        });
112
    }
113
}
114