From 595aeb1972a1c901bbdca97bc4f9791e55ac573a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Gomes=20da=20Silva=20Lisboa?= Date: Mon, 25 Oct 2021 11:48:32 -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 | 115 ------------------------------------ test/FiveOperationsTest.php | 65 -------------------- 2 files changed, 180 deletions(-) delete mode 100644 src/FiveOperations.php delete mode 100644 test/FiveOperationsTest.php 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/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