Passed
Pull Request — v3 (#729)
by
unknown
38:39 queued 03:37
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\Server;
22
use RecursiveArrayIterator;
23
24
/**
25
 * Class Secondstohuman
26
 *
27
 * This Filter converts seconds into a human readable time string 'hh:mm:ss'. This is mainly for converting
28
 * a player's connected time into a readable string. Note that most game servers DO NOT return a player's connected
29
 * time. Source (A2S) based games generally do but not always. This class can also be used to convert other time
30
 * responses into readable time
31
 *
32
 * @package GameQ\Filters
33
 * @author  Austin Bischoff <[email protected]>
34
 */
35
class Secondstohuman extends Base
36
{
37
38
    /**
39
     * The options key for setting the data key(s) to look for to convert
40
     */
41
    const OPTION_TIMEKEYS = 'timekeys';
42
43
    /**
44
     * The result key added when applying this filter to a result
45
     */
46
    const RESULT_KEY = 'gq_%s_human';
47
48
    /**
49
     * Holds the default 'time' keys from the response array.  This is key is usually 'time' from A2S responses
50
     *
51
     * @var array
52
     */
53
    protected $timeKeysDefault = ['time'];
54
55
    /**
56
     * Secondstohuman constructor.
57
     *
58
     * @param array $options
59 12
     */
60
    public function __construct(array $options = [])
61
    {
62 12
        // Check for passed keys
63
        if (! array_key_exists(self::OPTION_TIMEKEYS, $options)) {
64 6
            // Use default
65
            $options[self::OPTION_TIMEKEYS] = $this->timeKeysDefault;
66
        } else {
67 6
            // Used passed key(s) and make sure it is an array
68 6
            $options[self::OPTION_TIMEKEYS] = (!is_array($options[self::OPTION_TIMEKEYS])) ?
69
                [$options[self::OPTION_TIMEKEYS]] : $options[self::OPTION_TIMEKEYS];
70
        }
71 12
72 2
        parent::__construct($options);
73
    }
74
75
    /**
76
     * Apply this filter to the result data
77
     *
78
     * @param array  $result
79
     * @param Server $server
80
     *
81
     * @return array
82 12
     */
83
    public function apply(array $result, Server $server)
84
    {
85 12
        return static::applyRecursively($result, function ($value, $key, RecursiveArrayIterator $iterator) {
86
            if (
87
                /* Only process whitelisted keys */
88
                (in_array($key, $this->options[self::OPTION_TIMEKEYS])) &&
89
                /* Only process numeric values (float, integer, string) */
90
                (is_numeric($value))
91
            ) {
92
                /* Ensure the value is float */
93
                if (! is_float($value)) {
94
                    $value = floatval($value);
95
                }
96
97
                /* Add a new element to the result */
98 12
                $iterator->offsetSet(
99
                    /* Modify the current key */
100
                    sprintf(self::RESULT_KEY, $key),
101 12
                    /* Format the provided time */
102
                    sprintf(
103 12
                        '%02d:%02d:%02d',
104
                        (int)floor($value / 3600),    // Hours
105 12
                        (int)fmod(($value / 60), 60), // Minutes
106 12
                        (int)fmod($value, 60)         // Seconds
107
                    )
108 12
                );
109
            }
110 12
        });
111 12
    }
112
}
113