Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
GameParser
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 5
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParsedGame
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 parseMoves
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 parseAMove
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
56
 parseVariations
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
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
38namespace MediaWiki\Extension\ChessBrowser\PgnParser;
39
40class GameParser {
41
42    private $game;
43    private $fenParser0x88;
44
45    /**
46     * @param array $game
47     */
48    public function __construct( array $game ) {
49        $this->game = $game;
50    }
51
52    /**
53     * @return array
54     */
55    public function getParsedGame() {
56        $game = $this->game;
57
58        $this->fenParser0x88 = new FenParser0x88( $game[ChessJson::FEN] );
59        $this->parseMoves( $game[ChessJson::MOVE_MOVES] );
60
61        $game[ChessJson::GAME_METADATA][ChessJson::MOVE_PARSED] = 1;
62
63        return $game;
64    }
65
66    /**
67     * Parse each move
68     *
69     * @param array &$moves
70     */
71    private function parseMoves( &$moves ) {
72        foreach ( $moves as &$move ) {
73            $this->parseAMove( $move );
74        }
75    }
76
77    /**
78     * Parse a move
79     *
80     * @param array &$move
81     */
82    private function parseAMove( &$move ) {
83        if (
84            !isset( $move[ChessJson::MOVE_NOTATION] )
85            || (
86                isset( $move[ChessJson::FEN] )
87                && isset( $move[ChessJson::MOVE_FROM] )
88                && isset( $move[ChessJson::MOVE_TO] )
89            )
90            || strlen( $move[ChessJson::MOVE_NOTATION] ) < 2
91        ) {
92            return;
93        }
94
95        if ( isset( $move[ChessJson::MOVE_VARIATIONS] ) ) {
96            $fen = $this->fenParser0x88->getFen();
97            $this->parseVariations( $move[ChessJson::MOVE_VARIATIONS] );
98            $this->fenParser0x88->setFen( $fen );
99        }
100        $move = $this->fenParser0x88->getParsed( $move );
101    }
102
103    /**
104     * Parse variations
105     *
106     * @param array &$variations
107     */
108    private function parseVariations( &$variations ) {
109        foreach ( $variations as &$variation ) {
110            $fen = $this->fenParser0x88->getFen();
111            $this->parseMoves( $variation );
112            $this->fenParser0x88->setFen( $fen );
113        }
114    }
115
116}