From ee45a92e187ae2a3f0e8782ff9ed793e3fef9e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Gomes=20da=20Silva=20Lisboa?= Date: Thu, 15 Jul 2021 09:49:36 -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 --- src/FiveOperations.php | 111 ++++++++++++++++++++++++++++++++++++ src/FourOperations.php | 63 -------------------- test/FiveOperationsTest.php | 65 +++++++++++++++++++++ test/FourOperationsTest.php | 38 ------------ 4 files changed, 176 insertions(+), 101 deletions(-) create mode 100644 src/FiveOperations.php delete mode 100644 src/FourOperations.php create mode 100644 test/FiveOperationsTest.php delete mode 100644 test/FourOperationsTest.php diff --git a/src/FiveOperations.php b/src/FiveOperations.php new file mode 100644 index 00000000..88233595 --- /dev/null +++ b/src/FiveOperations.php @@ -0,0 +1,111 @@ + null, + 'operand' => null + ]; + + protected $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($operation) + { + 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; + } + + public function setOperand($operand) + { + $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/FourOperations.php b/src/FourOperations.php deleted file mode 100644 index d33762b3..00000000 --- a/src/FourOperations.php +++ /dev/null @@ -1,63 +0,0 @@ -options = $options; - } - - public function filter($value) - { - $value = (float) $value; - $operand = (float) $this->options['value']; - 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/test/FiveOperationsTest.php b/test/FiveOperationsTest.php new file mode 100644 index 00000000..c2f19972 --- /dev/null +++ b/test/FiveOperationsTest.php @@ -0,0 +1,65 @@ + $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/FourOperationsTest.php b/test/FourOperationsTest.php deleted file mode 100644 index 7ab95796..00000000 --- a/test/FourOperationsTest.php +++ /dev/null @@ -1,38 +0,0 @@ - 'add', - 'value' => 4 - ]); - $this->assertEquals(9, $filter->filter(5)); - $filter = new FourOperations([ - 'operation' => 'sub', - 'value' => 3 - ]); - $this->assertEquals(7, $filter->filter(10)); - $filter = new FourOperations([ - 'operation' => 'mul', - 'value' => 5 - ]); - $this->assertEquals(30, $filter->filter(6)); - $filter = new FourOperations([ - 'operation' => 'div', - 'value' => 12 - ]); - $this->assertEquals(12, $filter->filter(144)); - $filter = new FourOperations([ - 'operation' => 'mod', - 'value' => 2 - ]); - $this->assertEquals(1, $filter->filter(3)); - } -} \ No newline at end of file