Skip to content

Commit

Permalink
rename Parser Notation
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtree committed Feb 21, 2021
1 parent 1344005 commit 35b224e
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 30 deletions.
11 changes: 11 additions & 0 deletions src/Contract/NotationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Ouxsoft\LuckByDice\Contract;

use Ouxsoft\LuckByDice\Cup;

interface NotationInterface
{
public function encode(Cup $cup) : string;
public function decode(string $expression) : array;
}
10 changes: 0 additions & 10 deletions src/Contract/ParserInterface.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Contract/TurnInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Ouxsoft\LuckByDice\Contract;

use Ouxsoft\LuckByDice\Cup;
use Ouxsoft\LuckByDice\Notation;
use Ouxsoft\LuckByDice\Parser;

interface TurnInterface
{

public function __construct(
Parser $parser,
Notation $notation,
Cup $cup,
string $expression = null
);
Expand Down
10 changes: 5 additions & 5 deletions src/Cup.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

/**
* Cup
* A cup contains Collection of dice
* A cup contains Collections of dice
*
* @package Ouxsoft\LivingMarkup
*/
Expand Down Expand Up @@ -51,22 +51,22 @@ public function current(): int
return $this->container[$this->index];
}

public function next()
public function next() : void
{
$this->index ++;
$this->index++;
}

public function key()
{
return $this->index;
}

public function valid()
public function valid(): bool
{
return isset($this->container[$this->key()]);
}

public function rewind()
public function rewind() : void
{
$this->index = 0;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Dice.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* Class Dice
* A dice has two or more sides and can be roll
* @package Ouxsoft\LuckByDice
*/
class Dice
Expand All @@ -42,4 +43,12 @@ public function roll() : int
{
return mt_rand(1, $this->sides);
}

/**
* @return int
*/
public function getSides() : int
{
return $this->sides;
}
}
6 changes: 3 additions & 3 deletions src/Factory/ConcreteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
namespace Ouxsoft\LuckByDice\Factory;

use Ouxsoft\LuckByDice\Contract\AbstractFactoryInterface;
use Ouxsoft\LuckByDice\Parser;
use Ouxsoft\LuckByDice\Notation;
use Ouxsoft\LuckByDice\Cup;
use Ouxsoft\LuckByDice\Luck;

use Pimple\Container;

class ConcreteFactory implements AbstractFactoryInterface
{
public function makeParser(Container &$container): Parser
public function makeNotation(Container &$container): Notation
{
return new Parser();
return new Notation();
}

public function makeCup(Container &$container): Cup
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function buildContainer(
): Container {
$container = new Container();

$container['parser'] = $abstractFactory->makeParser($container);
$container['notation'] = $abstractFactory->makeNotation($container);
$container['cup'] = $abstractFactory->makeCup($container);
$container['luck'] = $abstractFactory->makeLuck($container);

Expand Down
2 changes: 1 addition & 1 deletion src/Factory/TurnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function getInstance(): Turn
$container = ContainerFactory::buildContainer($abstractFactory);

return new Turn(
$container['parser'],
$container['notation'],
$container['cup']
);
}
Expand Down
15 changes: 12 additions & 3 deletions src/Parser.php → src/Notation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@

namespace Ouxsoft\LuckByDice;

use Ouxsoft\LuckByDice\Contract\ParserInterface;
use Ouxsoft\LuckByDice\Contract\NotationInterface;

class Parser implements ParserInterface
class Notation implements NotationInterface
{
/**
* @param Cup $cup
* @return string
*/
public function encode(Cup $cup) : string
{

}

/**
* @param $expression
* @return array
*/
public function run($expression): array
public function decode(string $expression): array
{
$diceGroups = [];
$diceGroupExpressions = explode(',', strtolower($expression));
Expand Down
12 changes: 6 additions & 6 deletions src/Turn.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ class Turn implements TurnInterface
*/
private $cup;
/**
* @var Parser
* @var Notation
*/
private $parser;
private $notation;
/**
* @var int
*/
private $total;

/**
* Turn constructor.
* @param Parser $parser
* @param Notation $notation
* @param Cup $cup
* @param string|null $expression
* @see Turn::setByString()
*/
public function __construct(
Parser $parser,
Notation $notation,
Cup $cup,
string $expression = null
) {
$this->parser = $parser;
$this->notation = $notation;
$this->cup = $cup;

if ($expression !== null) {
Expand All @@ -59,7 +59,7 @@ public function __construct(
*/
public function set(string $expression) : void
{
$this->cup = $this->parser->run($expression);
$this->cup = $this->notation->decode($expression);
}

/**
Expand Down
Empty file removed tests/src/Unit/App.php
Empty file.
62 changes: 62 additions & 0 deletions tests/src/Unit/DiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the LuckByDice package.
*
* (c) 2017-2021 Ouxsoft <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ouxsoft\LivingMarkup\Tests\Unit;

use Laminas\Http\Exception\OutOfRangeException;
use Ouxsoft\LuckByDice\Dice;
use PHPUnit\Framework\TestCase;
use Ouxsoft\LuckByDice\Factory\TurnFactory;

class DiceTest extends TestCase
{
private $dice;

public function setUp(): void
{
$this->dice = new Dice(6);
}

public function tearDown(): void
{
unset($this->dice);
}


/**
* @covers \Ouxsoft\LuckByDice\Dice::__construct
*/
public function test__construct()
{
$slides = $this->dice->getSides();
$this->assertEquals(6, $slides);

$this->expectException(\OutOfRangeException::class);
$this->dice = new Dice(1);
}

/**
* @covers \Ouxsoft\LuckByDice\Dice::roll
*/
public function testRoll()
{
$outcome = $this->dice->roll();
$this->assertIsInt($outcome);
}

/**
* @covers \Ouxsoft\LuckByDice\Dice::getSides
*/
public function testGetSides()
{
$slides = $this->dice->getSides();
$this->assertEquals(6, $slides);
}
}

0 comments on commit 35b224e

Please sign in to comment.