From 256b4ea739527ef1b00fc6014ebb45f2f8a86cff Mon Sep 17 00:00:00 2001 From: DHTMLGoodies Date: Thu, 7 Feb 2013 17:45:11 +0100 Subject: [PATCH] Code cleanup parser --- PgnGameParser.php | 10 ++++- PgnParser.php | 93 +++++++++++++++++++++++++++++------------------ 2 files changed, 66 insertions(+), 37 deletions(-) diff --git a/PgnGameParser.php b/PgnGameParser.php index f695b8d..d3c7609 100644 --- a/PgnGameParser.php +++ b/PgnGameParser.php @@ -12,8 +12,16 @@ class PgnGameParser{ 'event','site','white','black','result','plycount','eco','fen','timecontrol','round','date','annotator','termination' ); - public function __construct($pgnGame){ + public function __construct($pgnGame = null){ + if(isset($pgnGame)){ + $this->pgnGame = trim($pgnGame); + $this->moveBuilder = new MoveBuilder(); + } + } + + public function setPgn($pgnGame){ $this->pgnGame = trim($pgnGame); + $this->gameData = array(); $this->moveBuilder = new MoveBuilder(); } diff --git a/PgnParser.php b/PgnParser.php index 784ac11..6cc7d6b 100644 --- a/PgnParser.php +++ b/PgnParser.php @@ -1,25 +1,31 @@ pgnFile = $pgnFile; } $this->gameParser = new GameParser(); + $this->pgnGameParser = new PgnGameParser(); } - public function setPgnContent($content){ + public function setPgnContent($content) + { $this->pgnContent = $content; } - private function cleanPgn(){ + private function cleanPgn() + { $c = $this->pgnContent; $c = preg_replace("/\\$[0-9]+/s", "", $c); $c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c); @@ -30,65 +36,80 @@ class PgnParser { $c = preg_replace("/([^\]])(\n+)\[/si", "$1\n\n[", $c); $c = preg_replace("/\n{3,}/s", "\n\n", $c); $c = str_replace("-SB-", "[", $c); - $this->pgnContent = $c; + return $c; } - public static function getArrayOfGames($pgn) { + public static function getArrayOfGames($pgn) + { return self::getPgnGamesAsArray($pgn); } - private function splitPgnIntoGames(){ - $this->pgnGames = $this->getPgnGamesAsArray($this->pgnContent); + + private function splitPgnIntoGames($pgnString) + { + return $this->getPgnGamesAsArray($pgnString); } - private function getPgnGamesAsArray($pgn){ + private function getPgnGamesAsArray($pgn) + { $ret = array(); $content = "\n\n" . $pgn; $games = preg_split("/\n\n\[/s", $content, -1, PREG_SPLIT_DELIM_CAPTURE); - for($i=1, $count = count($games); $i<$count; $i++){ - array_push($ret, trim("[". $games[$i])); + for ($i = 1, $count = count($games); $i < $count; $i++) { + array_push($ret, trim("[" . $games[$i])); } return $ret; } - public function getGamesAsJSON(){ + public function getGamesAsJSON() + { return json_encode($this->getGames()); } - private function isLazy(){ + private function isLazy() + { return false; } - public function getUnparsedGames() { - if(!isset($this->pgnGames)){ - if($this->pgnFile && !isset($this->pgnContent)){ - $this->pgnContent = file_get_contents($this->pgnFile); - } - $this->cleanPgn(); - $this->splitPgnIntoGames(); + public function getUnparsedGames() + { + if (!isset($this->pgnGames)) { + if ($this->pgnFile && !isset($this->pgnContent)) { + $this->pgnContent = file_get_contents($this->pgnFile); + } + $this->pgnGames = $this->splitPgnIntoGames($this->cleanPgn($this->pgnContent)); } return $this->pgnGames; } - public function getFirstGame(){ - $games = $this->getGames(); - if(count($games)){ - return $games[0]; + public function getFirstGame() + { + return $this->getGameByIndex(0); + } + + public function getGameByIndex($index){ + $games = $this->getUnparsedGames(); + if (count($games) && count($games) > $index) { + return $this->getParsedGame($games[$index]); } return null; } - public function getGames(){ - + public function getGames() + { $games = $this->getUnparsedGames(); - $this->games = array(); - for($i=0, $count = count($games);$i<$count; $i++){ - $gameParser = new PgnGameParser($games[$i]); - $this->games[$i] = $gameParser->getParsedData(); - - if(!$this->isLazy()){ - $this->games[$i] = $this->gameParser->getParsedGame($this->games[$i]); - } + $ret = array(); + for ($i = 0, $count = count($games); $i < $count; $i++) { + $ret[] = $this->getParsedGame($games[$i]); } - return $this->games; + return $ret; + } + + private function getParsedGame($unParsedGame){ + $this->pgnGameParser->setPgn($unParsedGame); + $ret = $this->pgnGameParser->getParsedData(); + if (!$this->isLazy()) { + $ret = $this->gameParser->getParsedGame($ret); + } + return $ret; } }