From a0f1c535969fc6961950f795070a785299633aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Gomes=20da=20Silva=20Lisboa?= Date: Wed, 14 Jul 2021 07:51:14 -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 | 5 +++-- test/FourOperationsTest.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/FourOperations.php b/src/FourOperations.php index e44a9f83..c63ed325 100644 --- a/src/FourOperations.php +++ b/src/FourOperations.php @@ -24,7 +24,7 @@ public function __construct(array $options) if (!isset($options['operation']) || (!in_array($options['operation'],$operations))){ throw new InvalidArgumentException(sprintf( '%s expects argument operation string with one of theses values: add, sub, mul or div; received "%s"', - __METHOD__, $options['operation'])); + __METHOD__, (float) $options['operation'])); } if (!isset($options['value'])){ throw new InvalidArgumentException(sprintf( @@ -36,7 +36,8 @@ public function __construct(array $options) public function filter($value) { - $operand = $this->options['value']; + $value = (float) $value; + $operand = (float) $this->options['value']; switch ($this->options['operation']){ case self::ADD: return ($value + $operand); diff --git a/test/FourOperationsTest.php b/test/FourOperationsTest.php index 4d631c8f..4032e479 100644 --- a/test/FourOperationsTest.php +++ b/test/FourOperationsTest.php @@ -6,7 +6,7 @@ class FourOperationsTest extends TestCase { - public function testOperations() + public function testOperations(): void { $filter = new FourOperations(['operation'=>'add','value'=>4]); $this->assertEquals(9, $filter->filter(5));