Overview

Namespaces

  • pgn
    • exceptions
    • tags
  • utils

Classes

  • pgn\Game
  • pgn\PGN
  • pgn\tags\Annotator
  • pgn\tags\Black
  • pgn\tags\BlackElo
  • pgn\tags\BlackNA
  • pgn\tags\BlackTitle
  • pgn\tags\BlackType
  • pgn\tags\BlackUSCF
  • pgn\tags\Board
  • pgn\tags\Date
  • pgn\tags\ECO
  • pgn\tags\Event
  • pgn\tags\EventCountry
  • pgn\tags\EventDate
  • pgn\tags\EventRounds
  • pgn\tags\EventSponsor
  • pgn\tags\EventType
  • pgn\tags\FEN
  • pgn\tags\Mode
  • pgn\tags\NIC
  • pgn\tags\Opening
  • pgn\tags\PlyCount
  • pgn\tags\Result
  • pgn\tags\Round
  • pgn\tags\Section
  • pgn\tags\SetUp
  • pgn\tags\Site
  • pgn\tags\Source
  • pgn\tags\SourceDate
  • pgn\tags\Stage
  • pgn\tags\SubVariation
  • pgn\tags\Tag
  • pgn\tags\Termination
  • pgn\tags\Time
  • pgn\tags\TimeControl
  • pgn\tags\UknownTag
  • pgn\tags\UTCDate
  • pgn\tags\UTCTime
  • pgn\tags\Variation
  • pgn\tags\White
  • pgn\tags\WhiteElo
  • pgn\tags\WhiteNA
  • pgn\tags\WhiteTitle
  • pgn\tags\WhiteType
  • pgn\tags\WhiteUSCF
  • utils\Parser
  • utils\String

Exceptions

  • pgn\exceptions\InvalidClassNameException
  • pgn\exceptions\InvalidDataException
  • pgn\exceptions\InvalidGameFormatException
  • pgn\exceptions\InvalidGamePathException
  • pgn\exceptions\PGNException
  • utils\ParserException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /* 
  3:  * Copyright (c) 2016 Geraldo B. Landre
  4:  * 
  5:  * See the file LICENSE for copying permission.
  6:  */
  7: namespace pgn;
  8: 
  9: use pgn\Game;
 10: use pgn\tags\Result;
 11: use pgn\exceptions\InvalidGamePathException;
 12: 
 13: /**
 14:  * Description of PGN
 15:  * @todo Testes a fazer:
 16:  * * Testes Funcionais Básicos + Caminho Básico
 17:  * Game::__toString
 18:  * Game::getTag
 19:  * Game::addTag
 20:  * Game::parse (Exceptions)
 21:  * * Caminho Básico
 22:  * PGN::load
 23:  * PGN::loadFromFile
 24:  * PGN::countGames
 25:  * PGN::__toString
 26:  * 
 27:  * @todo Próximo passo: Implementar Camada de Banco de Dados
 28:  * 
 29:  * @author Geraldo B. Landre <geraldo.landre@gmail.com>
 30:  */
 31: class PGN {
 32:     /**
 33:      *
 34:      * @var Game [] array of Games
 35:      */
 36:     private $games;
 37:     
 38:     /**
 39:      * Creates a PGN object with no games
 40:      */
 41:     public function __construct() {
 42:         $this->games = array ();
 43:     }
 44:     
 45:     /**
 46:      * Returns a string representation of the PGN object
 47:      * @return string a string representation of the PGN object
 48:      */
 49:     public function __toString() {
 50:         return $this->toString();
 51:     }
 52:     
 53:     /**
 54:      * Returns a string representation of the PGN object
 55:      * @todo implement support of a properties file with the property defaultEndLine
 56:      * @param string $endl The end-line character (default: "\n").
 57:      * Example:
 58:      * <pre><code>
 59:      * <?php
 60:      * $object = new PGN;
 61:      * $object->toString("<br />");
 62:      * </code></pre>
 63:      * @return string a string representation of the PGN object
 64:      */
 65:     public function toString($endl = "\n") {
 66:         if ($this->countGames() == 0) {
 67:             return "";
 68:         }
 69:         
 70:         $pgn = "";
 71:         foreach($this->games as $game) {
 72:             $pgn .= $game . $endl;
 73:         }
 74:         return $pgn;
 75:     }
 76:     
 77:     /**
 78:      * Loads games from a PGN string
 79:      * @param string $str PGN string
 80:      * @throws InvalidGameFormatException throws an exception if the parameter
 81:      *         is not a string or if it doesn't have the correct fields, i.e.
 82:      *         the move text and the seven roster tags
 83:      */
 84:     public function load($str) {
 85:         $pattern = "[\s+" . Result::validPattern() . "]";
 86:         $arr = preg_split($pattern, $str);
 87:         
 88:         foreach ($arr as $value) {
 89:             $game = trim ($value);
 90:             if(!empty($game)) {
 91:                 $game = new Game();
 92:                 $game->parse(trim($value));
 93:                 $this->games[] = $game;
 94:             }
 95:         }
 96:     }
 97:     
 98:     /**
 99:      * 
100:      * @param string $path
101:      * @throws InvalidGamePathException throws an exception if the file doesn't
102:      *          exist or if was not possible to read it
103:      * @throws InvalidGameFormatException throws an exception if the parameter
104:      *         is not a string or if it doesn't have the correct fields, i.e.
105:      *         the move text and the seven roster tags
106:      */
107:     public function loadFromFile($path) {
108:         if(!file_exists(realpath($path))) {
109:             throw new InvalidGamePathException('['.__CLASS__.": File [$path] doesn't exist");
110:         }
111:         
112:         $contents = file_get_contents($path);
113:         
114:         if($contents === FALSE) {
115:             throw new InvalidGamePathException('['.__CLASS__.": Couldn't read [$path]");
116:         }
117:         
118:         $this->load($contents);
119:     }
120: 
121:     /**
122:      * 
123:      * @return int the number of games contained in the PGN object
124:      */
125:     public function countGames() {
126:         return count($this->games);
127:     }
128: 
129:     /**
130:      * Seven Tag Roster
131:      * Gets the mandatory tags in a PGN game:
132:      * 
133:      * 1) Event (the name of the tournament or match event)
134:      * 
135:      * 2) Site (the location of the event)
136:      * 
137:      * 3) Date (the starting date of the game)
138:      * 
139:      * 4) Round (the playing round ordinal of the game)
140:      * 
141:      * 5) White (the player of the white pieces)
142:      * 
143:      * 6) Black (the player of the black pieces)
144:      * 
145:      * 7) Result (the result of the game)
146:      */
147:     static public function getSTR() {
148:         return array(
149:             'Event', 'Site', 'Date', 'Round', 'White', 'Black', 'Result'
150:         );
151:     }
152: 
153: }
154: 
API documentation generated by ApiGen