Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
GameParser | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getParsedGame | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
parseMoves | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
parseAMove | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
56 | |||
parseVariations | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * This file is a part of ChessBrowser. |
4 | * |
5 | * ChessBrowser is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU 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 | * This program 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 General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | * |
18 | * This file is a part of PgnParser |
19 | * |
20 | * PgnParser is free software: you can redistribute it and/or modify |
21 | * it under the terms of the GNU Lesser General Public License as published by |
22 | * the Free Software Foundation, either version 3 of the License, or |
23 | * (at your option) any later version. |
24 | * |
25 | * This program is distributed in the hope that it will be useful, |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28 | * GNU Lesser General Public License for more details. |
29 | * |
30 | * You should have received a copy of the GNU Lesser General Public License |
31 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
32 | * |
33 | * @file GameParser |
34 | * @ingroup ChessBrowser |
35 | * @author Alf Magne Kalleland |
36 | */ |
37 | |
38 | namespace MediaWiki\Extension\ChessBrowser\PgnParser; |
39 | |
40 | class GameParser { |
41 | /** @var array */ |
42 | private $game; |
43 | /** @var FenParser0x88|null */ |
44 | private $fenParser0x88; |
45 | |
46 | /** |
47 | * @param array $game |
48 | */ |
49 | public function __construct( array $game ) { |
50 | $this->game = $game; |
51 | } |
52 | |
53 | /** |
54 | * @return array |
55 | */ |
56 | public function getParsedGame() { |
57 | $game = $this->game; |
58 | |
59 | $this->fenParser0x88 = new FenParser0x88( $game[ChessJson::FEN] ); |
60 | $this->parseMoves( $game[ChessJson::MOVE_MOVES] ); |
61 | |
62 | $game[ChessJson::GAME_METADATA][ChessJson::MOVE_PARSED] = 1; |
63 | |
64 | return $game; |
65 | } |
66 | |
67 | /** |
68 | * Parse each move |
69 | * |
70 | * @param array &$moves |
71 | */ |
72 | private function parseMoves( &$moves ) { |
73 | foreach ( $moves as &$move ) { |
74 | $this->parseAMove( $move ); |
75 | } |
76 | } |
77 | |
78 | /** |
79 | * Parse a move |
80 | * |
81 | * @param array &$move |
82 | */ |
83 | private function parseAMove( &$move ) { |
84 | if ( |
85 | !isset( $move[ChessJson::MOVE_NOTATION] ) |
86 | || ( |
87 | isset( $move[ChessJson::FEN] ) |
88 | && isset( $move[ChessJson::MOVE_FROM] ) |
89 | && isset( $move[ChessJson::MOVE_TO] ) |
90 | ) |
91 | || strlen( $move[ChessJson::MOVE_NOTATION] ) < 2 |
92 | ) { |
93 | return; |
94 | } |
95 | |
96 | if ( isset( $move[ChessJson::MOVE_VARIATIONS] ) ) { |
97 | $fen = $this->fenParser0x88->getFen(); |
98 | $this->parseVariations( $move[ChessJson::MOVE_VARIATIONS] ); |
99 | $this->fenParser0x88->setFen( $fen ); |
100 | } |
101 | $move = $this->fenParser0x88->getParsed( $move ); |
102 | } |
103 | |
104 | /** |
105 | * Parse variations |
106 | * |
107 | * @param array &$variations |
108 | */ |
109 | private function parseVariations( &$variations ) { |
110 | foreach ( $variations as &$variation ) { |
111 | $fen = $this->fenParser0x88->getFen(); |
112 | $this->parseMoves( $variation ); |
113 | $this->fenParser0x88->setFen( $fen ); |
114 | } |
115 | } |
116 | |
117 | } |