Completed
Push — v3 ( 5abad2...d82332 )
by Austin
04:23
created

Secondstohuman::iterate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
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
23
/**
24
 * Class Secondstohuman
25
 *
26
 * This class converts seconds into a human readable time string 'hh:mm:ss'. This is mainly for converting
27
 * a player's connected time into a readable string. Note that most game servers DO NOT return a player's connected
28
 * time. Source (A2S) based games generally do but not always. This class can also be used to convert other time
29
 * responses into readable time
30
 *
31
 * @package GameQ\Filters
32
 * @author  Austin Bischoff <[email protected]>
33
 */
34
class Secondstohuman extends Base
35
{
36
37
    /**
38
     * The options key for setting the data key(s) to look for to convert
39
     */
40
    const OPTION_TIMEKEYS = 'timekeys';
41
42
    /**
43
     * The result key added when applying this filter to a result
44
     */
45
    const RESULT_KEY = 'gq_%s_human';
46
47
    /**
48
     * Holds the default 'time' keys from the response array.  This is key is usually 'time' from A2S responses
49
     *
50
     * @var array
51
     */
52
    protected $timeKeysDefault = ['time'];
53
54
    /**
55
     * Secondstohuman constructor.
56
     *
57
     * @param array $options
58
     */
59 2
    public function __construct(array $options = [])
60
    {
61
        // Check for passed keys
62 2
        if (!array_key_exists(self::OPTION_TIMEKEYS, $options)) {
63
            // Use default
64 1
            $options[self::OPTION_TIMEKEYS] = $this->timeKeysDefault;
65
        } else {
66
            // Used passed key(s) and make sure it is an array
67 1
            $options[self::OPTION_TIMEKEYS] = (!is_array($options[self::OPTION_TIMEKEYS])) ?
68 1
                [$options[self::OPTION_TIMEKEYS]] : $options[self::OPTION_TIMEKEYS];
69
        }
70
71 2
        parent::__construct($options);
72 2
    }
73
74
    /**
75
     * Apply this filter to the result data
76
     *
77
     * @param array  $result
78
     * @param Server $server
79
     *
80
     * @return array
81
     */
82 2
    public function apply(array $result, Server $server)
83
    {
84
        // Send the results off to be iterated and return the updated result
85 2
        return $this->iterate($result);
86
    }
87
88
    /**
89
     * Home grown iterate function.  Would like to replace this with an internal PHP method(s) but could not find a way
90
     * to make the iterate classes add new keys to the response.  They all seemed to be read-only.
91
     *
92
     * @todo: See if there is a more internal way of handling this instead of foreach looping and recursive calling
93
     *
94
     * @param array $result
95
     *
96
     * @return array
97
     */
98 2
    protected function iterate(array &$result)
99
    {
100
        // Iterate over the results
101 2
        foreach ($result as $key => $value) {
102
            // Offload to itself if we have another array
103 2
            if (is_array($value)) {
104
                // Iterate and update the result
105 2
                $result[$key] = $this->iterate($value);
106 2
            } elseif (in_array($key, $this->options[self::OPTION_TIMEKEYS])) {
107
                // We match one of the keys we are wanting to convert so add it and move on
108 2
                $result[sprintf(self::RESULT_KEY, $key)] = sprintf(
109 2
                    "%02d:%02d:%02d",
110 2
                    floor($value / 3600),
111 2
                    ($value / 60) % 60,
112 2
                    $value % 60
113
                );
114
            }
115
        }
116
117 2
        return $result;
118
    }
119
}
120