1: <?php
2:
3: /*
4: * Copyright (c) 2016 Geraldo B. Landre
5: *
6: * See the file LICENSE for copying permission.
7: */
8:
9: namespace pgn\tags;
10:
11: use utils\Parser;
12:
13: /**
14: * Description of FEN:
15: * This tag uses a string that gives the Forsyth-Edwards Notation for the starting
16: * position used in the game. FEN is described in a later section of this
17: * document. If a SetUp tag appears with a tag value of "1", the FEN tag pair is
18: * also required.
19: *
20: * Data fields (6): Piece placement data, Active color, Castling availability,
21: * En passant target square, Halfmove clock, Fullmove number
22: *
23: * Examples:
24: * rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
25: * rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
26: * rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2
27: * rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2
28: * 4k3/8/8/8/8/8/4P3/4K3 w - - 5 39
29: *
30: * @see pgn_standard.txt
31: * @author Geraldo
32: */
33: class FEN extends Tag {
34:
35: public function getName() {
36: $parsed = Parser::parseClassName(get_class());
37: return $parsed['className'];
38: }
39:
40: /**
41: *
42: * @assert("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") === true
43: * @assert("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1") === true
44: * @assert("rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2") === true
45: * @assert("rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2") === true
46: * @assert("4k3/8/8/8/8/8/4P3/4K3 w - - 5 39") === true
47: * @assert("Prq1823Br b kqKQ 1 1 1") === false
48: * @assert(2) === false
49: * @assert("a") === false
50: * @assert(NULL) === false
51: *
52: * @param mixed $data
53: * @return boolean returns if a data is valid
54: */
55: public function validate($data) {
56: if (!parent::validate($data)) {
57: return false;
58: }
59:
60: return preg_match_all("/^" . self::validPattern() . "$/", $data) === 1;
61: }
62:
63: /**
64: * Get the regex pattern of a valid FEN tag string
65: * @return string regex pattern of a valid SetUp tag string
66: */
67: static public function validPattern() {
68: return "[PNBRQKpnbrqk1-8\/]+\s[wb]\s(-|K?Q?k?q?)\s(-|[abcdefgh][36])\s\d+\s\d+";
69: }
70:
71: }
72: