Secondstohuman.php 4.0 KB

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