-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
290 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Roliod\TexasHUPoker\Exceptions; | ||
|
||
use Exception; | ||
|
||
class InvalidFileContent extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Roliod\TexasHUPoker; | ||
|
||
use Roliod\TexasHUPoker\Deck\Rank; | ||
use Roliod\TexasHUPoker\Deck\Suite; | ||
use Roliod\TexasHUPoker\Rules\Concerns\CanGetRanksFromSequence; | ||
use Roliod\TexasHUPoker\Rules\Concerns\CanGetSuitsFromSequence; | ||
|
||
class Validator | ||
{ | ||
use CanGetSuitsFromSequence, CanGetRanksFromSequence; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $deck; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $hasError = false; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $error; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $isValid = true; | ||
|
||
/** | ||
* @param string $deck | ||
*/ | ||
private function __construct(string $deck) | ||
{ | ||
$this->deck = $deck; | ||
|
||
$this->validate(); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
private function validate(): self | ||
{ | ||
$deckToArray = explode(PHP_EOL, $this->deck); | ||
|
||
if ($this->validateHandCount($deckToArray) === false) { | ||
return $this; | ||
} | ||
|
||
if ($this->validateRanks($deckToArray) === false) { | ||
return $this; | ||
} | ||
|
||
if ($this->validateSuits($deckToArray) === false) { | ||
return $this; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasError(): bool | ||
{ | ||
return $this->hasError; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isValid(): bool | ||
{ | ||
return $this->isValid; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function error(): ?string | ||
{ | ||
return $this->error; | ||
} | ||
|
||
/** | ||
* @param string $deck | ||
* | ||
* @return $this | ||
*/ | ||
public static function create(string $deck): self | ||
{ | ||
return new self($deck); | ||
} | ||
|
||
/** | ||
* @param array $deck | ||
* | ||
* @return bool | ||
*/ | ||
private function validateHandCount(array $deck): bool | ||
{ | ||
foreach ($deck as $sequence) { | ||
$hand = explode(' ', $sequence); | ||
|
||
if (count($hand) !== 5) { | ||
$this->error = "Sequence count must be 5 each. Provided: $sequence"; | ||
$this->isValid = false; | ||
$this->hasError = true; | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param array $deck | ||
* | ||
* @return bool | ||
*/ | ||
private function validateRanks(array $deck): bool | ||
{ | ||
foreach ($deck as $hand) { | ||
$ranks = $this->getRanksFromSequence($hand); | ||
|
||
foreach ($ranks as $rank) { | ||
if (!in_array($rank,RANK::LIST)) { | ||
$this->error = "Invalid rank provided: $rank"; | ||
$this->isValid = false; | ||
$this->hasError = true; | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param array $deck | ||
* | ||
* @return bool | ||
*/ | ||
private function validateSuits(array $deck): bool | ||
{ | ||
foreach ($deck as $hand) { | ||
$suits = $this->getSuitsFromSequence($hand); | ||
|
||
foreach ($suits as $suit) { | ||
if (!array_key_exists($suit, Suite::UNICODE_TO_STRING)) { | ||
$this->error = "Invalid suit provided: $suit"; | ||
$this->isValid = false; | ||
$this->hasError = true; | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit; | ||
|
||
use Tests\BaseTestCase; | ||
use Roliod\TexasHUPoker\Validator; | ||
|
||
class ValidatorTest extends BaseTestCase | ||
{ | ||
/** | ||
* @dataProvider provideDeckData | ||
* | ||
* @param string $input | ||
* @param bool $isValid | ||
* @param bool $hasError | ||
* @param string|null $errorMessage | ||
*/ | ||
public function testItCanValidateInput( | ||
string $input, | ||
bool $isValid, | ||
bool $hasError, | ||
?string $errorMessage = null | ||
): void { | ||
$validator = Validator::create($input); | ||
|
||
self::assertSame($isValid, $validator->isValid()); | ||
self::assertSame($hasError, $validator->hasError()); | ||
self::assertSame($errorMessage, $validator->error()); | ||
} | ||
|
||
/** | ||
* @return array[] | ||
*/ | ||
public function provideDeckData(): array | ||
{ | ||
return [ | ||
'invalid_hand_count' => [ | ||
'input' => "3H 3J 3J 4J 5J\n" . "3 3 3 3", | ||
'is_valid' => false, | ||
'has_error' => true, | ||
'expected_message' => 'Sequence count must be 5 each. Provided: 3 3 3 3' | ||
], | ||
'invalid_rank' => [ | ||
'input' => "QH JJ 10J AJ KJ\n" . "QH 18J 10J AJ KJ", | ||
'is_valid' => false, | ||
'has_error' => true, | ||
'expected_message' => 'Invalid rank provided: 18' | ||
], | ||
'invalid_suit' => [ | ||
'input' => "QH JJ 10J AJ KJ\n" . "QH QJ 10J AJ KJ", | ||
'is_valid' => false, | ||
'has_error' => true, | ||
'expected_message' => 'Invalid suit provided: H' | ||
], | ||
'valid' => [ | ||
'input' => "10❤ 10♦ 10♠ 9♣ 9♦\n" . "4♠ J♠ 8♠ 2♠ 9♠", | ||
'is_valid' => true, | ||
'has_error' => false, | ||
'expected_message' => null | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invalid |