Skip to content

Commit

Permalink
Filter for arithmetic operations laminas#28
Browse files Browse the repository at this point in the history
Signed-off-by: Flávio Gomes da Silva Lisboa <[email protected]>
  • Loading branch information
fgsl committed Dec 2, 2021
1 parent a0f1c53 commit f8a7022
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
36 changes: 22 additions & 14 deletions src/FourOperations.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
<?php
namespace Laminas\Filter;


use Laminas\Filter\AbstractFilter;
use Laminas\Filter\Exception\InvalidArgumentException;

class FourOperations extends AbstractFilter {
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__, (float) $options['operation']));
$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__, (float) $options['operation']));
}
if (!isset($options['value'])){
throw new InvalidArgumentException(sprintf(
'%s expects argument value; received none',
__METHOD__));
if (! isset($options['value'])) {
throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__));
}
$this->options = $options;
}

public function filter($value)
{
$value = (float) $value;
$operand = (float) $this->options['value'];
switch ($this->options['operation']){
switch ($this->options['operation']) {
case self::ADD:
return ($value + $operand);
case self::SUB:
Expand Down
26 changes: 21 additions & 5 deletions test/FourOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,33 @@

class FourOperationsTest extends TestCase
{

public function testOperations(): void
{
$filter = new FourOperations(['operation'=>'add','value'=>4]);
$filter = new FourOperations([
'operation' => 'add',
'value' => 4
]);
$this->assertEquals(9, $filter->filter(5));
$filter = new FourOperations(['operation'=>'sub','value'=>3]);
$filter = new FourOperations([
'operation' => 'sub',
'value' => 3
]);
$this->assertEquals(7, $filter->filter(10));
$filter = new FourOperations(['operation'=>'mul','value'=>5]);
$filter = new FourOperations([
'operation' => 'mul',
'value' => 5
]);
$this->assertEquals(30, $filter->filter(6));
$filter = new FourOperations(['operation'=>'div','value'=>12]);
$filter = new FourOperations([
'operation' => 'div',
'value' => 12
]);
$this->assertEquals(12, $filter->filter(144));
$filter = new FourOperations(['operation'=>'mod','value'=>2]);
$filter = new FourOperations([
'operation' => 'mod',
'value' => 2
]);
$this->assertEquals(1, $filter->filter(3));
}
}

0 comments on commit f8a7022

Please sign in to comment.