Passed
Push — v3 ( 962004...bdfc5f )
by Austin
06:52
created

Result::addSub()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 12
c 2
b 1
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 9.2222
cc 6
nc 12
nop 3
crap 6
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
    /**
31
     * Formatted server response
32
     *
33
     * @var        array
34
     */
35
    protected $result = [];
36
37
    /**
38
     * Adds variable to results
39
     *
40
     * @param string $name  Variable name
41
     * @param string|array $value Variable value
42
     */
43 220
    public function add($name, $value)
44
    {
45
46 220
        $this->result[$name] = $value;
47 220
    }
48
49
    /**
50
     * Adds player variable to output
51
     *
52
     * @param string $name  Variable name
53
     * @param string $value Variable value
54
     */
55 138
    public function addPlayer($name, $value)
56
    {
57
58 138
        $this->addSub('players', $name, $value);
59 138
    }
60
61
    /**
62
     * Adds player variable to output
63
     *
64
     * @param string $name  Variable name
65
     * @param string $value Variable value
66
     */
67 20
    public function addTeam($name, $value)
68
    {
69
70 20
        $this->addSub('teams', $name, $value);
71 20
    }
72
73
    /**
74
     * Add a variable to a category
75
     *
76
     * @param  $sub    string  The category
77
     * @param  $key    string  The variable name
78
     * @param  $value  string  The variable value
79
     */
80 164
    public function addSub($sub, $key, $value)
81
    {
82
83
        // Nothing of this type yet, set an empty array
84 164
        if (!isset($this->result[$sub]) or !is_array($this->result[$sub])) {
85 164
            $this->result[$sub] = [];
86
        }
87
88
        // Find the first entry that doesn't have this variable
89 164
        $found = false;
90 164
        $count = count($this->result[$sub]);
91 164
        for ($i = 0; $i != $count; $i++) {
92 162
            if (!isset($this->result[$sub][$i][$key])) {
93 154
                $this->result[$sub][$i][$key] = $value;
94 154
                $found = true;
95 154
                break;
96
            }
97
        }
98
99
        // Not found, create a new entry
100 164
        if (!$found) {
101 164
            $this->result[$sub][][$key] = $value;
102
        }
103
104 164
        unset($count);
105 164
    }
106
107
    /**
108
     * Return all stored results
109
     *
110
     * @return  array  All results
111
     */
112 220
    public function fetch()
113
    {
114
115 220
        return $this->result;
116
    }
117
118
    /**
119
     * Return a single variable
120
     *
121
     * @param string $var The variable name
122
     *
123
     * @return  mixed   The variable value
124
     */
125 123
    public function get($var)
126
    {
127
128 123
        return isset($this->result[$var]) ? $this->result[$var] : null;
129
    }
130
}
131