Result.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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;
  19. /**
  20. * Provide an interface for easy storage of a parsed server response
  21. *
  22. * @author Aidan Lister <[email protected]>
  23. * @author Tom Buskens <[email protected]>
  24. */
  25. class Result
  26. {
  27. /**
  28. * Formatted server response
  29. *
  30. * @var array
  31. */
  32. protected $result = [];
  33. /**
  34. * Adds variable to results
  35. *
  36. * @param string $name Variable name
  37. * @param string|array $value Variable value
  38. */
  39. public function add($name, $value)
  40. {
  41. $this->result[$name] = $value;
  42. }
  43. /**
  44. * Adds player variable to output
  45. *
  46. * @param string $name Variable name
  47. * @param string $value Variable value
  48. */
  49. public function addPlayer($name, $value)
  50. {
  51. $this->addSub('players', $name, $value);
  52. }
  53. /**
  54. * Adds player variable to output
  55. *
  56. * @param string $name Variable name
  57. * @param string $value Variable value
  58. */
  59. public function addTeam($name, $value)
  60. {
  61. $this->addSub('teams', $name, $value);
  62. }
  63. /**
  64. * Add a variable to a category
  65. *
  66. * @param $sub string The category
  67. * @param $key string The variable name
  68. * @param $value string The variable value
  69. */
  70. public function addSub($sub, $key, $value)
  71. {
  72. // Nothing of this type yet, set an empty array
  73. if (!isset($this->result[$sub]) or !is_array($this->result[$sub])) {
  74. $this->result[$sub] = [];
  75. }
  76. // Find the first entry that doesn't have this variable
  77. $found = false;
  78. $count = count($this->result[$sub]);
  79. for ($i = 0; $i != $count; $i++) {
  80. if (!isset($this->result[$sub][$i][$key])) {
  81. $this->result[$sub][$i][$key] = $value;
  82. $found = true;
  83. break;
  84. }
  85. }
  86. // Not found, create a new entry
  87. if (!$found) {
  88. $this->result[$sub][][$key] = $value;
  89. }
  90. unset($count);
  91. }
  92. /**
  93. * Return all stored results
  94. *
  95. * @return array All results
  96. */
  97. public function fetch()
  98. {
  99. return $this->result;
  100. }
  101. /**
  102. * Return a single variable
  103. *
  104. * @param string $var The variable name
  105. *
  106. * @return mixed The variable value
  107. */
  108. public function get($var)
  109. {
  110. return isset($this->result[$var]) ? $this->result[$var] : null;
  111. }
  112. }