Code cleanup parser

This commit is contained in:
DHTMLGoodies
2013-02-07 17:45:11 +01:00
parent 929c109bb9
commit 256b4ea739
2 changed files with 66 additions and 37 deletions

View File

@@ -12,10 +12,18 @@ class PgnGameParser{
'event','site','white','black','result','plycount','eco','fen','timecontrol','round','date','annotator','termination' '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->pgnGame = trim($pgnGame);
$this->moveBuilder = new MoveBuilder(); $this->moveBuilder = new MoveBuilder();
} }
}
public function setPgn($pgnGame){
$this->pgnGame = trim($pgnGame);
$this->gameData = array();
$this->moveBuilder = new MoveBuilder();
}
public function getParsedData(){ public function getParsedData(){
$this->gameData = $this->getMetadata(); $this->gameData = $this->getMetadata();

View File

@@ -1,25 +1,31 @@
<?php <?php
class PgnParser { class PgnParser
{
private $pgnFile; private $pgnFile;
private $pgnContent; private $pgnContent;
private $pgnGames; private $pgnGames;
private $games; private $games;
private $gameParser; private $gameParser;
private $pgnGameParser;
public function __construct($pgnFile = ""){ public function __construct($pgnFile = "")
{
if ($pgnFile) { if ($pgnFile) {
$this->pgnFile = $pgnFile; $this->pgnFile = $pgnFile;
} }
$this->gameParser = new GameParser(); $this->gameParser = new GameParser();
$this->pgnGameParser = new PgnGameParser();
} }
public function setPgnContent($content){ public function setPgnContent($content)
{
$this->pgnContent = $content; $this->pgnContent = $content;
} }
private function cleanPgn(){ private function cleanPgn()
{
$c = $this->pgnContent; $c = $this->pgnContent;
$c = preg_replace("/\\$[0-9]+/s", "", $c); $c = preg_replace("/\\$[0-9]+/s", "", $c);
$c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c); $c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c);
@@ -30,17 +36,21 @@ class PgnParser {
$c = preg_replace("/([^\]])(\n+)\[/si", "$1\n\n[", $c); $c = preg_replace("/([^\]])(\n+)\[/si", "$1\n\n[", $c);
$c = preg_replace("/\n{3,}/s", "\n\n", $c); $c = preg_replace("/\n{3,}/s", "\n\n", $c);
$c = str_replace("-SB-", "[", $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); 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(); $ret = array();
$content = "\n\n" . $pgn; $content = "\n\n" . $pgn;
$games = preg_split("/\n\n\[/s", $content, -1, PREG_SPLIT_DELIM_CAPTURE); $games = preg_split("/\n\n\[/s", $content, -1, PREG_SPLIT_DELIM_CAPTURE);
@@ -50,45 +60,56 @@ class PgnParser {
return $ret; return $ret;
} }
public function getGamesAsJSON(){ public function getGamesAsJSON()
{
return json_encode($this->getGames()); return json_encode($this->getGames());
} }
private function isLazy(){ private function isLazy()
{
return false; return false;
} }
public function getUnparsedGames() { public function getUnparsedGames()
{
if (!isset($this->pgnGames)) { if (!isset($this->pgnGames)) {
if ($this->pgnFile && !isset($this->pgnContent)) { if ($this->pgnFile && !isset($this->pgnContent)) {
$this->pgnContent = file_get_contents($this->pgnFile); $this->pgnContent = file_get_contents($this->pgnFile);
} }
$this->cleanPgn(); $this->pgnGames = $this->splitPgnIntoGames($this->cleanPgn($this->pgnContent));
$this->splitPgnIntoGames();
} }
return $this->pgnGames; return $this->pgnGames;
} }
public function getFirstGame(){ public function getFirstGame()
$games = $this->getGames(); {
if(count($games)){ return $this->getGameByIndex(0);
return $games[0]; }
public function getGameByIndex($index){
$games = $this->getUnparsedGames();
if (count($games) && count($games) > $index) {
return $this->getParsedGame($games[$index]);
} }
return null; return null;
} }
public function getGames(){ public function getGames()
{
$games = $this->getUnparsedGames(); $games = $this->getUnparsedGames();
$this->games = array(); $ret = array();
for ($i = 0, $count = count($games); $i < $count; $i++) { for ($i = 0, $count = count($games); $i < $count; $i++) {
$gameParser = new PgnGameParser($games[$i]); $ret[] = $this->getParsedGame($games[$i]);
$this->games[$i] = $gameParser->getParsedData(); }
return $ret;
}
private function getParsedGame($unParsedGame){
$this->pgnGameParser->setPgn($unParsedGame);
$ret = $this->pgnGameParser->getParsedData();
if (!$this->isLazy()) { if (!$this->isLazy()) {
$this->games[$i] = $this->gameParser->getParsedGame($this->games[$i]); $ret = $this->gameParser->getParsedGame($ret);
} }
} return $ret;
return $this->games;
} }
} }