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 fd1c56d commit f645478
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 180 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
/laminas-mkdoc-theme/
/phpunit.xml
/vendor/
.buildpath
.project
.settings/
44 changes: 44 additions & 0 deletions src/AbstractArithmeticOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace Laminas\Filter;

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

abstract class AbstractArithmeticOperation extends AbstractFilter
{
protected $options = [
/** expected key "operand" **/
];

/**
* $options expects an array with one key:
* 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['operand']))) {
$args = func_get_args();
if (isset($args[0])) {
$this->setOperand($args[0]);
}
} else {
$this->setOptions($options);
}
}

/**
*
* @param integer | float $operand
*/
public function setOperand($operand): AbstractArithmeticOperation
{
$this->options['operand'] = $operand;
return $this;
}
}
24 changes: 24 additions & 0 deletions src/Add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Laminas\Filter;

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

class Add extends AbstractArithmeticOperation
{
/**
*
* {@inheritdoc}
* @see \Laminas\Filter\FilterInterface::filter()
*/
public function filter($value)
{
if (! isset($this->options['operand'])) {
throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__));
}

$value = (float) $value;
$operand = (float) $this->options['operand'];
return ($value + $operand);
}
}
24 changes: 24 additions & 0 deletions src/Div.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Laminas\Filter;

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

class Div extends AbstractArithmeticOperation
{
/**
*
* {@inheritdoc}
* @see \Laminas\Filter\FilterInterface::filter()
*/
public function filter($value)
{
if (! isset($this->options['operand'])) {
throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__));
}

$value = (float) $value;
$operand = (float) $this->options['operand'];
return ($value / $operand);
}
}
115 changes: 0 additions & 115 deletions src/FiveOperations.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Mod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Laminas\Filter;

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

class Mod extends AbstractArithmeticOperation
{
/**
*
* {@inheritdoc}
* @see \Laminas\Filter\FilterInterface::filter()
*/
public function filter($value)
{
if (! isset($this->options['operand'])) {
throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__));
}

$value = (float) $value;
$operand = (float) $this->options['operand'];
return ($value % $operand);
}
}
24 changes: 24 additions & 0 deletions src/Mul.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Laminas\Filter;

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

class Mul extends AbstractArithmeticOperation
{
/**
*
* {@inheritdoc}
* @see \Laminas\Filter\FilterInterface::filter()
*/
public function filter($value)
{
if (! isset($this->options['operand'])) {
throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__));
}

$value = (float) $value;
$operand = (float) $this->options['operand'];
return ($value * $operand);
}
}
24 changes: 24 additions & 0 deletions src/Sub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Laminas\Filter;

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

class Sub extends AbstractArithmeticOperation
{
/**
*
* {@inheritdoc}
* @see \Laminas\Filter\FilterInterface::filter()
*/
public function filter($value)
{
if (! isset($this->options['operand'])) {
throw new InvalidArgumentException(sprintf('%s expects argument value; received none', __METHOD__));
}

$value = (float) $value;
$operand = (float) $this->options['operand'];
return ($value - $operand);
}
}
33 changes: 33 additions & 0 deletions test/AddTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

namespace LaminasTest\Filter;

use PHPUnit\Framework\TestCase;
use Laminas\Filter\Add;

class AddTest extends TestCase
{

public function testAddWithOptionGivenByConstructor(): void
{
$filter = new Add([
'operand' => 4
]);
$this->assertEquals(7, $filter->filter(3));
}

public function testAddWithOptionGivenBySetter(): void
{
$filter = new Add();
$filter->setOperand(4);
$this->assertEquals(7, $filter->filter(3));
}

public function testInvalidOperand(): void
{
$this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class);
$filter = new Add();
$this->assertEquals(6, $filter->filter(5));
}
}
33 changes: 33 additions & 0 deletions test/DivTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

namespace LaminasTest\Filter;

use PHPUnit\Framework\TestCase;
use Laminas\Filter\Div;

class DivTest extends TestCase
{

public function testDivWithOptionGivenByConstructor(): void
{
$filter = new Div([
'operand' => 3
]);
$this->assertEquals(9, $filter->filter(27));
}

public function testOperationsWithOptionsGivenBySetters(): void
{
$filter = new Div();
$filter->setOperand(3);
$this->assertEquals(9, $filter->filter(27));
}

public function testInvalidOperand(): void
{
$this->expectException(\Laminas\Filter\Exception\InvalidArgumentException::class);
$filter = new Div();
$this->assertEquals(5, $filter->filter(5));
}
}
Loading

0 comments on commit f645478

Please sign in to comment.