From 15298c7cd209cb01400c8d7cc8d004667b2a9217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Gomes=20da=20Silva=20Lisboa?= Date: Tue, 13 Jul 2021 16:56:08 -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/FourOperations.php | 54 +++++++++++++++++++++++++++++++++++++ test/FourOperationsTest.php | 22 +++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/FourOperations.php create mode 100644 test/FourOperationsTest.php diff --git a/src/FourOperations.php b/src/FourOperations.php new file mode 100644 index 00000000..e44a9f83 --- /dev/null +++ b/src/FourOperations.php @@ -0,0 +1,54 @@ +options = $options; + } + + public function filter($value) + { + $operand = $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/FourOperationsTest.php b/test/FourOperationsTest.php new file mode 100644 index 00000000..4d631c8f --- /dev/null +++ b/test/FourOperationsTest.php @@ -0,0 +1,22 @@ +'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