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\Server;
22:
23: /**
24: * Class Normalize
25: *
26: * This Filter is responsible for normalizing the provided result's
27: * property names to the GameQ standard.
28: *
29: * @package GameQ\Filters
30: */
31: class Normalize extends Base
32: {
33: /**
34: * Determines if data should be persisted for unit testing.
35: *
36: * @var bool
37: */
38: protected $writeTestData = false;
39:
40: /**
41: * Holds the protocol specific normalize information
42: *
43: * @var array
44: */
45: protected $normalize = [];
46:
47: /**
48: * Apply this filter
49: *
50: * @SuppressWarnings(PHPMD.CyclomaticComplexity)
51: *
52: * @param array $result
53: * @param \GameQ\Server $server
54: *
55: * @return array
56: */
57: public function apply(array $result, Server $server)
58: {
59: // Determine if there is data to be processed
60: if (! empty($result)) {
61: // Handle unit test data generation
62: if ($this->writeTestData) {
63: // Initialize potential data for unit testing
64: $unitTestData = [ ];
65:
66: // Add the initial result to the unit test data
67: $unitTestData['raw'][$server->id()] = $result;
68: }
69:
70: /* Grab the normalize definition from the server's protocol *///
71: $this->normalize = $server->protocol()->getNormalize();
72:
73: // Normalize general information
74: $result = array_merge($result, $this->check('general', $result));
75:
76: // Normalize player information
77: if (isset($result['players']) && count($result['players']) > 0) {
78: foreach ($result['players'] as $key => $player) {
79: $result['players'][$key] = array_merge($player, $this->check('player', $player));
80: }
81: } else {
82: $result['players'] = [];
83: }
84:
85: // Normalize team information
86: if (isset($result['teams']) && count($result['teams']) > 0) {
87: foreach ($result['teams'] as $key => $team) {
88: $result['teams'][$key] = array_merge($team, $this->check('team', $team));
89: }
90: } else {
91: $result['teams'] = [];
92: }
93:
94: // Handle unit test data generation
95: if ($this->writeTestData) {
96: // Add the filtered result to the unit test data
97: $unitTestData['filtered'][$server->id()] = $result;
98:
99: // Persist the collected data to the tests directory
100: file_put_contents(
101: sprintf('%s/../../../tests/Filters/Providers/Normalize/%s_1.json', __DIR__, $server->protocol()->getProtocol()),
102: json_encode($unitTestData, JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR)
103: );
104: }
105: }
106:
107: // Return the filtered result
108: return $result;
109: }
110:
111: /**
112: * Check a section for normalization
113: *
114: * @param string $section
115: * @param array $data
116: *
117: * @return array
118: */
119: protected function check($section, array $data)
120: {
121: // Initialize the normalized output
122: $normalized = [];
123:
124: // Ensure the provided section is defined
125: if (isset($this->normalize[$section])) {
126: // Process each mapping individually
127: foreach ($this->normalize[$section] as $target => $source) {
128: // Treat explicit source like implicit sources
129: if (! is_array($source)) {
130: $source = [$source];
131: }
132:
133: // Find the first possible source
134: foreach ($source as $s) {
135: // Determine if the current source does exist
136: if (array_key_exists($s, $data)) {
137: // Add the normalized mapping
138: $normalized['gq_'.$target] = $data[$s];
139: break;
140: }
141: }
142:
143: // Write null in case no source was found
144: // TODO: Remove this in the next major version.
145: $normalized['gq_'.$target] = isset($normalized['gq_'.$target]) ? $normalized['gq_'.$target] : null;
146: }
147: }
148:
149: // Return the normalized data
150: return $normalized;
151: }
152: }
153: