Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.87% |
26 / 31 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
NotationAnalyzer | |
83.87% |
26 / 31 |
|
66.67% |
4 / 6 |
15.94 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFromRank | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getFromFile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getTargetSquare | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getPieceType | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
getPromotion | |
50.00% |
4 / 8 |
|
0.00% |
0 / 1 |
4.12 |
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 | * @file ChessSquare |
19 | * @ingroup ChessBrowser |
20 | * @author DannyS712 |
21 | */ |
22 | |
23 | namespace MediaWiki\Extension\ChessBrowser; |
24 | |
25 | class NotationAnalyzer { |
26 | |
27 | /** @var string */ |
28 | private $notation; |
29 | |
30 | /** |
31 | * @param string $notation |
32 | */ |
33 | public function __construct( string $notation ) { |
34 | $this->notation = $notation; |
35 | } |
36 | |
37 | /** |
38 | * Get the rank the piece started on |
39 | * |
40 | * @return int|null |
41 | */ |
42 | public function getFromRank() { |
43 | $notation = preg_replace( "/^.+(\d).+\d.*$/s", '$1', $this->notation ); |
44 | if ( strlen( $notation ) > 1 ) { |
45 | return null; |
46 | } |
47 | return ( (int)$notation - 1 ) * 16; |
48 | } |
49 | |
50 | /** |
51 | * Get the file the piece started on |
52 | * |
53 | * @return int|null |
54 | */ |
55 | public function getFromFile() { |
56 | $notation = preg_replace( "/^.*([a-h]).*[a-h].*$/s", '$1', $this->notation ); |
57 | if ( strlen( $notation ) > 1 ) { |
58 | return null; |
59 | } |
60 | return ChessSquare::FILE_TO_NUMBER[$notation]; |
61 | } |
62 | |
63 | /** |
64 | * Get the target square |
65 | * |
66 | * @return int|string |
67 | */ |
68 | public function getTargetSquare() { |
69 | $notation = preg_replace( "/.*([a-h][1-8]).*/s", '$1', $this->notation ); |
70 | try { |
71 | $square = ChessSquare::newFromCoords( $notation ); |
72 | return $square->getNumber(); |
73 | } catch ( ChessBrowserException $e ) { |
74 | return ''; |
75 | } |
76 | } |
77 | |
78 | /** |
79 | * Get the piece type making the move |
80 | * |
81 | * @param string $color |
82 | * @return int |
83 | */ |
84 | public function getPieceType( string $color ): int { |
85 | $notation = $this->notation; |
86 | if ( $notation === 'O-O-O' || $notation === 'O-O' ) { |
87 | $pieceType = 'K'; |
88 | } else { |
89 | $token = $notation[0]; |
90 | $pieceType = preg_match( "/[NRBQK]/", $token ) ? $token : 'P'; |
91 | } |
92 | $pieceType = ( new ChessPiece( $pieceType ) )->getAsHex(); |
93 | if ( $color === 'black' ) { |
94 | $pieceType += 8; |
95 | } |
96 | return $pieceType; |
97 | } |
98 | |
99 | /** |
100 | * Get the promotion |
101 | * |
102 | * If the notation token contains an equal sign then it's a promotion |
103 | * |
104 | * @return string |
105 | */ |
106 | public function getPromotion(): string { |
107 | $notation = $this->notation; |
108 | if ( strpos( $notation, '=' ) !== false ) { |
109 | $piece = preg_replace( "/^.*?=([QRBN]).*$/", '$1', $notation ); |
110 | return strtolower( $piece ); |
111 | } |
112 | |
113 | if ( preg_match( "/[a-h][18][NBRQ]/", $notation ) ) { |
114 | $notation = preg_replace( "/[^a-h18NBRQ]/s", "", $notation ); |
115 | return strtolower( $notation[-1] ); |
116 | } |
117 | return ''; |
118 | } |
119 | |
120 | } |