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 Jul 15, 2021
1 parent 6c50214 commit ee45a92
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 101 deletions.
111 changes: 111 additions & 0 deletions src/FiveOperations.php
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;
}
}
63 changes: 0 additions & 63 deletions src/FourOperations.php

This file was deleted.

65 changes: 65 additions & 0 deletions test/FiveOperationsTest.php
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]
];
}
}
38 changes: 0 additions & 38 deletions test/FourOperationsTest.php

This file was deleted.

0 comments on commit ee45a92

Please sign in to comment.