|
| 1 | +# Chess API Client |
| 2 | + |
| 3 | +A PHP client for [chess-api.com](https://chess-api.com/) using PSR-18 HTTP Client. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- PHP 8.2+ |
| 8 | +- PSR-18 HTTP Client implementation (e.g. Symfony HttpClient, Guzzle) |
| 9 | +- PSR-17 HTTP Factory implementation |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +composer require p-chess/api |
| 15 | +``` |
| 16 | + |
| 17 | +If you want to use Symfony HttpClient: |
| 18 | + |
| 19 | +```bash |
| 20 | +composer require nyholm/psr7 symfony/http-client |
| 21 | +``` |
| 22 | + |
| 23 | +If you want to use Guzzle: |
| 24 | + |
| 25 | +```bash |
| 26 | +composer require guzzlehttp/guzzle |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +```php |
| 32 | +use PChess\Api\ChessApiClient; |
| 33 | + |
| 34 | +// Create the client with your PSR-18/PSR-17 implementations |
| 35 | +$client = new ChessApiClient( |
| 36 | + $httpClient, // PSR-18 ClientInterface |
| 37 | + $requestFactory, // PSR-17 RequestFactoryInterface |
| 38 | + $streamFactory, // PSR-17 StreamFactoryInterface |
| 39 | +); |
| 40 | + |
| 41 | +// Get the best move for a position |
| 42 | +$fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'; |
| 43 | +$move = $client->getBestMove($fen); |
| 44 | +// Returns: 'e4' (or similar) |
| 45 | + |
| 46 | +// Restrict the engine to specific moves |
| 47 | +$move = $client->getBestMove($fen, ['e2e4', 'd2d4', 'g1f3']); |
| 48 | +// Returns one of the allowed moves |
| 49 | +``` |
| 50 | + |
| 51 | +### Parameters |
| 52 | + |
| 53 | +| Parameter | Type | Description | |
| 54 | +|-----------------|----------|--------------------------------------------------------------------------| |
| 55 | +| `$fen` | `string` | The chess position in FEN notation (includes side to move) | |
| 56 | +| `$allowedMoves` | `array` | Optional array of allowed moves in UCI format (e.g., `['e2e4', 'd2d4']`) | |
| 57 | + |
| 58 | +### Return Value |
| 59 | + |
| 60 | +The method returns a `string` containing the best move in UCI format (e.g., `'e2e4'`). |
| 61 | + |
| 62 | +### Exceptions |
| 63 | + |
| 64 | +The client throws `ChessApiException` in the following cases: |
| 65 | + |
| 66 | +- HTTP error (non-2xx status code) |
| 67 | +- Invalid API response (missing `move` field) |
| 68 | +- API error (e.g., invalid FEN) |
| 69 | + |
| 70 | +```php |
| 71 | +use PChess\Api\ChessApiException; |
| 72 | + |
| 73 | +try { |
| 74 | + $move = $client->getBestMove($fen); |
| 75 | +} catch (ChessApiException $e) { |
| 76 | + // Handle the error |
| 77 | + echo $e->getMessage(); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Example with Symfony HttpClient |
| 82 | + |
| 83 | +```php |
| 84 | +use Nyholm\Psr7\Factory\Psr17Factory; |
| 85 | +use PChess\Api\ChessApiClient; |
| 86 | +use Symfony\Component\HttpClient\Psr18Client; |
| 87 | + |
| 88 | +$factory = new Psr17Factory(); |
| 89 | +$httpClient = new Psr18Client(); |
| 90 | + |
| 91 | +$chessClient = new ChessApiClient($httpClient, $factory, $factory); |
| 92 | + |
| 93 | +$fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'; |
| 94 | +$move = $chessClient->getBestMove($fen); |
| 95 | + |
| 96 | +echo "Best move: $move\n"; |
| 97 | +``` |
| 98 | + |
| 99 | +## Example with Guzzle |
| 100 | + |
| 101 | +```php |
| 102 | +use GuzzleHttp\Client; |
| 103 | +use GuzzleHttp\Psr7\HttpFactory; |
| 104 | +use PChess\Api\ChessApiClient; |
| 105 | + |
| 106 | +$httpClient = new Client(); |
| 107 | +$factory = new HttpFactory(); |
| 108 | + |
| 109 | +$chessClient = new ChessApiClient($httpClient, $factory, $factory); |
| 110 | + |
| 111 | +$fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'; |
| 112 | +$move = $chessClient->getBestMove($fen); |
| 113 | + |
| 114 | +echo "Best move: $move\n"; |
| 115 | +``` |
| 116 | + |
| 117 | +## Development |
| 118 | + |
| 119 | +Before exdecuting one of the following, you must execute at least once |
| 120 | + |
| 121 | +```bash |
| 122 | +docker build -t chess-api . |
| 123 | +``` |
| 124 | + |
| 125 | +### Running Tests |
| 126 | + |
| 127 | +```bash |
| 128 | +docker run --rm -v "$(pwd)":/srv/chess-api chess-api vendor/bin/phpunit --color=always |
| 129 | +``` |
| 130 | + |
| 131 | +### Coding Standard |
| 132 | + |
| 133 | +```bash |
| 134 | +docker run --rm -v "$(pwd)":/srv/chess-api chess-api vendor/bin/php-cs-fixer --ansi |
| 135 | +``` |
| 136 | + |
| 137 | +### Static Analysis |
| 138 | + |
| 139 | +```bash |
| 140 | +docker run --rm -v "$(pwd)":/srv/chess-api chess-api vendor/bin/phpstan --ansi |
| 141 | +``` |
0 commit comments