/** * Parses a string containing a game and fills movetext and tags attributes. * The tag attribute is filled as an associative array (TagName => TagObject) * * @assert(NULL) throws pgn\exceptions\InvalidGameFormatException * @assert(123) throws pgn\exceptions\InvalidGameFormatException * * @param string $unparsedGame string containing one PGN Game * @throws pgn\exceptions\InvalidGameFormatException throws an exception if the parameter * is not a string or if it doesn't have the correct fields, i.e. * the move text and the seven roster tags */ public function parse($unparsedGame) { if (!is_string($unparsedGame)) { throw new InvalidGameFormatException("[" . __CLASS__ . "] invalid game format (it's not a valid string): " . $unparsedGame); } $parsing = explode("]", $unparsedGame); if (count($parsing) < 8) { // TODO: create a checkSTR method throw new InvalidGameFormatException("[" . __CLASS__ . "] invalid game format (it doesn't have correct fields): " . $unparsedGame); } $uncheckedMoveText = array_pop($parsing); foreach ($parsing as $unparsedTag) { $tag = Tag::parse(trim($unparsedTag) . "]", $this->parseErrors); $this->addTag($tag); } if (!$this->checkSetUpAndFEN()) { throw new InvalidGameFormatException("[" . __CLASS__ . "] invalid game format (missing SetUp or FEN): " . $unparsedGame); } if ($this->checkMoveText($uncheckedMoveText)) { $this->moveText = $uncheckedMoveText; } else { $this->parseErrors[] = "[" . __CLASS__ . "] MoveText: " . $uncheckedMoveText; } }