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
14 changed files
with
337 additions
and
180 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 |
---|---|---|
|
@@ -7,3 +7,6 @@ | |
/laminas-mkdoc-theme/ | ||
/phpunit.xml | ||
/vendor/ | ||
.buildpath | ||
.project | ||
.settings/ |
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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)); | ||
} | ||
} |
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,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)); | ||
} | ||
} |
Oops, something went wrong.