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