diff --git a/README.md b/README.md index 8c7beb7..d569c6a 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,17 @@ $result = BaseConvert::convert('FF', 16, '0123456789ABCDEF')->to(10, 'ZYXWVUTSRQ ```php use Graphita\Mathematics\Factorial; -// Factrorial 3 : 3x2x1 -$result = (new Factorial(3))->calculate(); // 6 +// Factorial 3 : 3x2x1 +$result = Factorial::instance(3)->calculate()->getResult(); // 6 -// Factrorial 4 : 4x3x2x1 -$result = (new Factorial(4))->calculate(); // 24 +// Factorial 4 : 4x3x2x1 +$result = Factorial::instance(4)->calculate()->getResult(); // 24 + +// Factorial 5 : 5x4x3x2x1 +$factorial = new Factorial; +$factorial->setNumber(5); +$factorial->calculate(); +$result = $factorial->getResult(); ``` ### Permutation diff --git a/src/Factorial.php b/src/Factorial.php index d41b3f3..bf968cb 100644 --- a/src/Factorial.php +++ b/src/Factorial.php @@ -3,6 +3,8 @@ namespace Graphita\Mathematics; use InvalidArgumentException; +use function PHPUnit\Framework\isEmpty; +use function PHPUnit\Framework\isNull; class Factorial { @@ -12,29 +14,57 @@ class Factorial private int $number; /** - * @param $number + * @var ?int */ - public function __construct($number) + private ?int $result = null; + + /** + * @param int|null $number + * @return Factorial + */ + public static function instance(int $number = null): Factorial { - if (!is_integer($number)) { - throw new InvalidArgumentException('Number must be a Positive Integer to Calculate Factorial !'); + $instance = new static(); + if (is_integer($number)) { + $instance->setNumber($number); } + return $instance; + } + + /** + * @param int $number + * @return Factorial + */ + public function setNumber(int $number): static + { if ($number < 0) { throw new InvalidArgumentException('Number must be a Positive Integer to Calculate Factorial !'); } $this->number = $number; + + return $this; } /** - * @return int + * @return $this */ - public function calculate(): int + public function calculate(): static { - $result = 1; - while ($this->number > 1) { - $result *= $this->number; - $this->number--; + $number = $this->number; + $this->result = 1; + while ($number > 1) { + $this->result *= $number; + $number--; } - return $result; + + return $this; + } + + /** + * @return int|null + */ + public function getResult(): ?int + { + return $this->result; } } \ No newline at end of file diff --git a/tests/FactorialTest.php b/tests/FactorialTest.php index dc0750b..976f3b4 100644 --- a/tests/FactorialTest.php +++ b/tests/FactorialTest.php @@ -7,54 +7,63 @@ class FactorialTest extends TestCase { - public function testWithNonIntegerNumber() - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Number must be a Positive Integer to Calculate Factorial !'); - - $result = (new Factorial(2.5))->calculate(); - } - public function testWithNegativeNumber() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Number must be a Positive Integer to Calculate Factorial !'); - $result = (new Factorial(-2))->calculate(); + $result = Factorial::instance(-2)->calculate()->getResult(); } public function testFactorialZero() { - $result = (new Factorial(0))->calculate(); + $result = Factorial::instance(0)->calculate()->getResult(); $this->assertEquals(1, $result); } public function testFactorialOne() { - $result = (new Factorial(1))->calculate(); + $result = Factorial::instance(1)->calculate()->getResult(); $this->assertEquals(1, $result); } public function testFactorialTwo() { - $result = (new Factorial(2))->calculate(); + $result = Factorial::instance(2)->calculate()->getResult(); $this->assertEquals(2, $result); } public function testFactorialThree() { - $result = (new Factorial(3))->calculate(); + $result = Factorial::instance(3)->calculate()->getResult(); $this->assertEquals(2 * 3, $result); } public function testFactorialTen() { - $result = (new Factorial(10))->calculate(); + $result = Factorial::instance(10)->calculate()->getResult(); $this->assertEquals(2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10, $result); } + + public function testSetNumber() + { + $factorial = new Factorial(); + $factorial->setNumber(4); + $factorial->calculate(); + $result = $factorial->getResult(); + + $this->assertEquals(2 * 3 * 4, $result); + } + + public function testGetResultBeforeCalculate() + { + $result = Factorial::instance(3)->getResult(); + + $this->assertNull($result); + } }