| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | namespace GameQ\Filters; |
| 20: | |
| 21: | use GameQ\Server; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | class Normalize extends Base |
| 32: | { |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | protected $writeTestData = false; |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | protected $normalize = []; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function apply(array $result, Server $server) |
| 58: | { |
| 59: | |
| 60: | if (! empty($result)) { |
| 61: | |
| 62: | if ($this->writeTestData) { |
| 63: | |
| 64: | $unitTestData = [ ]; |
| 65: | |
| 66: | |
| 67: | $unitTestData['raw'][$server->id()] = $result; |
| 68: | } |
| 69: | |
| 70: | |
| 71: | $this->normalize = $server->protocol()->getNormalize(); |
| 72: | |
| 73: | |
| 74: | $result = array_merge($result, $this->check('general', $result)); |
| 75: | |
| 76: | |
| 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: | |
| 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: | |
| 95: | if ($this->writeTestData) { |
| 96: | |
| 97: | $unitTestData['filtered'][$server->id()] = $result; |
| 98: | |
| 99: | |
| 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: | |
| 108: | return $result; |
| 109: | } |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | protected function check($section, array $data) |
| 120: | { |
| 121: | |
| 122: | $normalized = []; |
| 123: | |
| 124: | |
| 125: | if (isset($this->normalize[$section])) { |
| 126: | |
| 127: | foreach ($this->normalize[$section] as $target => $source) { |
| 128: | |
| 129: | if (! is_array($source)) { |
| 130: | $source = [$source]; |
| 131: | } |
| 132: | |
| 133: | |
| 134: | foreach ($source as $s) { |
| 135: | |
| 136: | if (array_key_exists($s, $data)) { |
| 137: | |
| 138: | $normalized['gq_'.$target] = $data[$s]; |
| 139: | break; |
| 140: | } |
| 141: | } |
| 142: | |
| 143: | |
| 144: | |
| 145: | $normalized['gq_'.$target] = isset($normalized['gq_'.$target]) ? $normalized['gq_'.$target] : null; |
| 146: | } |
| 147: | } |
| 148: | |
| 149: | |
| 150: | return $normalized; |
| 151: | } |
| 152: | } |
| 153: | |