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 <austin@codebeard.com> |
35: | */ |
36: | class Secondstohuman extends Base |
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: | */ |
60: | public function __construct(array $options = []) |
61: | { |
62: | // Check for passed keys |
63: | if (! array_key_exists(self::OPTION_TIMEKEYS, $options)) { |
64: | // Use default |
65: | $options[self::OPTION_TIMEKEYS] = $this->timeKeysDefault; |
66: | } else { |
67: | // Used passed key(s) and make sure it is an array |
68: | $options[self::OPTION_TIMEKEYS] = (!is_array($options[self::OPTION_TIMEKEYS])) ? |
69: | [$options[self::OPTION_TIMEKEYS]] : $options[self::OPTION_TIMEKEYS]; |
70: | } |
71: | |
72: | 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: | */ |
83: | public function apply(array $result, Server $server) |
84: | { |
85: | return Arr::recursively($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: | $iterator->offsetSet( |
99: | // Modify the current key |
100: | sprintf(self::RESULT_KEY, $key), |
101: | // Format the provided time |
102: | sprintf( |
103: | '%02d:%02d:%02d', |
104: | (int)floor($value / 3600), // Hours |
105: | (int)fmod(($value / 60), 60), // Minutes |
106: | (int)fmod($value, 60) // Seconds |
107: | ) |
108: | ); |
109: | } |
110: | }); |
111: | } |
112: | } |
113: |