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
4 changed files
with
176 additions
and
101 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,111 @@ | ||
<?php | ||
namespace Laminas\Filter; | ||
|
||
use Laminas\Filter\AbstractFilter; | ||
use Laminas\Filter\Exception\InvalidArgumentException; | ||
|
||
class FiveOperations extends AbstractFilter | ||
{ | ||
|
||
const ADD = 'add'; | ||
|
||
const SUB = 'sub'; | ||
|
||
const MUL = 'mul'; | ||
|
||
const DIV = 'div'; | ||
|
||
const MOD = 'mod'; | ||
|
||
protected $options = [ | ||
'operation' => null, | ||
'operand' => null | ||
]; | ||
|
||
protected $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($operation) | ||
{ | ||
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; | ||
} | ||
|
||
public function setOperand($operand) | ||
{ | ||
$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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,65 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Filter; | ||
|
||
use Laminas\Filter\FiveOperations; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FiveOperationsTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider operationProvider | ||
*/ | ||
public function testOperationsWithOptionsGivenByConstructor(string $operation, $operand, $valueToFilter, $expected): void | ||
{ | ||
$filter = new FiveOperations([ | ||
'operation' => $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] | ||
]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.