forked from laminas/laminas-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter for arithmetic operations laminas#28
Signed-off-by: Flávio Gomes da Silva Lisboa <[email protected]>
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
namespace Laminas\Filter; | ||
|
||
|
||
use Laminas\Filter\AbstractFilter; | ||
use Laminas\Filter\Exception\InvalidArgumentException; | ||
|
||
class FourOperations extends AbstractFilter { | ||
const ADD = 'add'; | ||
const SUB = 'sub'; | ||
const MUL = 'mul'; | ||
const DIV = 'div'; | ||
const MOD = 'mod'; | ||
|
||
/** | ||
* $options expects an array with two keys: | ||
* operation: add, sub, mul or div | ||
* value: value of second operand | ||
* @param array $options | ||
*/ | ||
public function __construct(array $options) | ||
{ | ||
$operations = [self::ADD,self::SUB,self::MUL,self::DIV,self::MOD]; | ||
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'])); | ||
} | ||
if (!isset($options['value'])){ | ||
throw new InvalidArgumentException(sprintf( | ||
'%s expects argument value; received none', | ||
__METHOD__)); | ||
} | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
namespace LaminasTest\Filter; | ||
|
||
use Laminas\Filter\FourOperations; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FourOperationsTest extends TestCase | ||
{ | ||
public function testOperations() | ||
{ | ||
$filter = new FourOperations(['operation'=>'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)); | ||
} | ||
} |