From f645478305b589156f092274f7046537f72b171c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Gomes=20da=20Silva=20Lisboa?= Date: Mon, 25 Oct 2021 07:27:29 -0300 Subject: [PATCH] Filter for arithmetic operations #28 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Flávio Gomes da Silva Lisboa --- .gitignore | 3 + src/AbstractArithmeticOperation.php | 44 +++++++++++ src/Add.php | 24 ++++++ src/Div.php | 24 ++++++ src/FiveOperations.php | 115 ---------------------------- src/Mod.php | 24 ++++++ src/Mul.php | 24 ++++++ src/Sub.php | 24 ++++++ test/AddTest.php | 33 ++++++++ test/DivTest.php | 33 ++++++++ test/FiveOperationsTest.php | 65 ---------------- test/ModTest.php | 37 +++++++++ test/MulTest.php | 33 ++++++++ test/SubTest.php | 34 ++++++++ 14 files changed, 337 insertions(+), 180 deletions(-) create mode 100644 src/AbstractArithmeticOperation.php create mode 100644 src/Add.php create mode 100644 src/Div.php delete mode 100644 src/FiveOperations.php create mode 100644 src/Mod.php create mode 100644 src/Mul.php create mode 100644 src/Sub.php create mode 100644 test/AddTest.php create mode 100644 test/DivTest.php delete mode 100644 test/FiveOperationsTest.php create mode 100644 test/ModTest.php create mode 100644 test/MulTest.php create mode 100644 test/SubTest.php diff --git a/.gitignore b/.gitignore index b640e945..2fd93bdc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ /laminas-mkdoc-theme/ /phpunit.xml /vendor/ +.buildpath +.project +.settings/ diff --git a/src/AbstractArithmeticOperation.php b/src/AbstractArithmeticOperation.php new file mode 100644 index 00000000..c1541e46 --- /dev/null +++ b/src/AbstractArithmeticOperation.php @@ -0,0 +1,44 @@ +setOperand($args[0]); + } + } else { + $this->setOptions($options); + } + } + + /** + * + * @param integer | float $operand + */ + public function setOperand($operand): AbstractArithmeticOperation + { + $this->options['operand'] = $operand; + return $this; + } +} \ No newline at end of file diff --git a/src/Add.php b/src/Add.php new file mode 100644 index 00000000..8b79ae94 --- /dev/null +++ b/src/Add.php @@ -0,0 +1,24 @@ +options['operand'])) { + throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__)); + } + + $value = (float) $value; + $operand = (float) $this->options['operand']; + return ($value + $operand); + } +} \ No newline at end of file diff --git a/src/Div.php b/src/Div.php new file mode 100644 index 00000000..97b22d09 --- /dev/null +++ b/src/Div.php @@ -0,0 +1,24 @@ +options['operand'])) { + throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__)); + } + + $value = (float) $value; + $operand = (float) $this->options['operand']; + return ($value / $operand); + } +} \ No newline at end of file diff --git a/src/FiveOperations.php b/src/FiveOperations.php deleted file mode 100644 index af470ac0..00000000 --- a/src/FiveOperations.php +++ /dev/null @@ -1,115 +0,0 @@ - null, - 'operand' => null - ]; - - protected array $operations = [ - self::ADD, - self::SUB, - self::MUL, - self::DIV, - self::MOD - ]; - - /** - * $options expects an array with two keys: - * operation: add, sub, mul or div - * operand: value of second operand - * - * @param array $options - */ - public function __construct(array $options = null) - { - if ($options instanceof \Traversable) { - $options = iterator_to_array($options); - } - - if (! is_array($options) || (! isset($options['operation']) && ! isset($options['operand']))) { - $args = func_get_args(); - if (isset($args[0])) { - $this->setOperation($args[0]); - } - if (isset($args[1])) { - $this->setOperand($args[1]); - } - } else { - $this->setOptions($options); - } - } - - /** - * - * @param string $operation - * @throws InvalidArgumentException - */ - public function setOperation(string $operation): FiveOperations - { - if (! in_array($operation, $this->operations)) { - throw new InvalidArgumentException(sprintf('%s expects argument operation string with one of theses values: add, sub, mul, div or mod; received "%s"', __METHOD__, $operation)); - } - - $this->options['operation'] = $operation; - return $this; - } - - /** - * - * @param integer | float $operand - */ - public function setOperand($operand): FiveOperations - { - $this->options['operand'] = $operand; - return $this; - } - - /** - * - * {@inheritdoc} - * @see \Laminas\Filter\FilterInterface::filter() - */ - public function filter($value) - { - if (! isset($this->options['operation']) || (! in_array($this->options['operation'], $this->operations))) { - throw new InvalidArgumentException(sprintf('%s expects argument operation string with one of theses values: add, sub, mul, div or mod; received "%s"', __METHOD__, $this->options['operation'])); - } - - if (! isset($this->options['operand'])) { - throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__)); - } - - $value = (float) $value; - $operand = (float) $this->options['operand']; - switch ($this->options['operation']) { - case self::ADD: - return ($value + $operand); - case self::SUB: - return ($value - $operand); - case self::MUL: - return ($value * $operand); - case self::DIV: - return ($value / $operand); - case self::MOD: - return ($value % $operand); - } - return $value; - } -} \ No newline at end of file diff --git a/src/Mod.php b/src/Mod.php new file mode 100644 index 00000000..11c73a50 --- /dev/null +++ b/src/Mod.php @@ -0,0 +1,24 @@ +options['operand'])) { + throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__)); + } + + $value = (float) $value; + $operand = (float) $this->options['operand']; + return ($value % $operand); + } +} \ No newline at end of file diff --git a/src/Mul.php b/src/Mul.php new file mode 100644 index 00000000..b3411ab0 --- /dev/null +++ b/src/Mul.php @@ -0,0 +1,24 @@ +options['operand'])) { + throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__)); + } + + $value = (float) $value; + $operand = (float) $this->options['operand']; + return ($value * $operand); + } +} \ No newline at end of file diff --git a/src/Sub.php b/src/Sub.php new file mode 100644 index 00000000..11e8d033 --- /dev/null +++ b/src/Sub.php @@ -0,0 +1,24 @@ +options['operand'])) { + throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__)); + } + + $value = (float) $value; + $operand = (float) $this->options['operand']; + return ($value - $operand); + } +} \ No newline at end of file diff --git a/test/AddTest.php b/test/AddTest.php new file mode 100644 index 00000000..2a2ef752 --- /dev/null +++ b/test/AddTest.php @@ -0,0 +1,33 @@ + 4 + ]); + $this->assertEquals(7, $filter->filter(3)); + } + + public function testAddWithOptionGivenBySetter(): void + { + $filter = new Add(); + $filter->setOperand(4); + $this->assertEquals(7, $filter->filter(3)); + } + + public function testInvalidOperand(): void + { + $this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class); + $filter = new Add(); + $this->assertEquals(6, $filter->filter(5)); + } +} \ No newline at end of file diff --git a/test/DivTest.php b/test/DivTest.php new file mode 100644 index 00000000..ce2a0bf0 --- /dev/null +++ b/test/DivTest.php @@ -0,0 +1,33 @@ + 3 + ]); + $this->assertEquals(9, $filter->filter(27)); + } + + public function testOperationsWithOptionsGivenBySetters(): void + { + $filter = new Div(); + $filter->setOperand(3); + $this->assertEquals(9, $filter->filter(27)); + } + + public function testInvalidOperand(): void + { + $this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class); + $filter = new Div(); + $this->assertEquals(5, $filter->filter(5)); + } +} \ No newline at end of file diff --git a/test/FiveOperationsTest.php b/test/FiveOperationsTest.php deleted file mode 100644 index c2f19972..00000000 --- a/test/FiveOperationsTest.php +++ /dev/null @@ -1,65 +0,0 @@ - $operation, - 'operand' => $operand - ]); - $this->assertEquals($expected, $filter->filter($valueToFilter)); - } - - /** - * @dataProvider operationProvider - */ - public function testOperationsWithOptionsGivenBySetters(string $operation, $operand, $valueToFilter, $expected): void - { - $filter = new FiveOperations(); - $filter->setOperation($operation) - ->setOperand($operand); - $this->assertEquals($expected, $filter->filter($valueToFilter)); - } - - public function testInvalidOperation(): void - { - $this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class); - $filter = new FiveOperations([ - 'operation' => 'unknown', - 'operand' => 0 - ]); - $this->assertEquals(10, $filter->filter(5)); - } - - public function testInvalidOperand(): void - { - $this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class); - $filter = new FiveOperations([ - 'operation' => FiveOperations::ADD - ]); - $this->assertEquals(10, $filter->filter(5)); - } - - public function operationProvider(): array - { - // operation, operand, value to filter and expected value - return [ - [FiveOperations::ADD, 4, 5 , 9], - [FiveOperations::SUB, 3, 10, 7], - [FiveOperations::MUL, 5, 6, 30], - [FiveOperations::DIV, 12, 144, 12], - [FiveOperations::MOD, 2, 3, 1] - ]; - } -} \ No newline at end of file diff --git a/test/ModTest.php b/test/ModTest.php new file mode 100644 index 00000000..8d557d18 --- /dev/null +++ b/test/ModTest.php @@ -0,0 +1,37 @@ + 7 + ]); + $this->assertEquals(1, $filter->filter(50)); + } + + public function testModWithOptionGivenBySetter(): void + { + $filter = new Mod(); + $filter->setOperand(7); + $this->assertEquals(1, $filter->filter(50));} + + public function testInvalidOperand(): void + { + $this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class); + $filter = new Mod(); + $this->assertEquals(0, $filter->filter(5)); + } +} \ No newline at end of file diff --git a/test/MulTest.php b/test/MulTest.php new file mode 100644 index 00000000..ce68e46b --- /dev/null +++ b/test/MulTest.php @@ -0,0 +1,33 @@ + 2 + ]); + $this->assertEquals(12, $filter->filter(6)); + } + + public function testOperationWithOptionGivenBySetter(): void + { + $filter = new Mul(); + $filter->setOperand(2); + $this->assertEquals(12, $filter->filter(6)); + } + + public function testInvalidOperand(): void + { + $this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class); + $filter = new Mul(); + $this->assertEquals(5, $filter->filter(5)); + } +} \ No newline at end of file diff --git a/test/SubTest.php b/test/SubTest.php new file mode 100644 index 00000000..7b84cb33 --- /dev/null +++ b/test/SubTest.php @@ -0,0 +1,34 @@ + 2 + ]); + $this->assertEquals(3, $filter->filter(5)); + } + + public function testSubWithOptionGivenBySetter(): void + { + $filter = new Sub(); + $filter->setOperand(2); + $this->assertEquals(3, $filter->filter(5)); + } + + public function testInvalidOperand(): void + { + $this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class); + $filter = new Sub(); + $this->assertEquals(4, $filter->filter(5)); + } +} \ No newline at end of file