Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions spec/Check/NumericRangeCheckSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace spec\StructureCheck\Check;

use Prophecy\Argument;
use StructureCheck\Check\NumericRangeCheck;
use PhpSpec\ObjectBehavior;
use StructureCheck\ResultInterface;
use StructureCheck\Type\TypeInterface;

class NumericRangeCheckSpec extends ObjectBehavior
{

function it_is_initializable(TypeInterface $child)
{
$this->beConstructedWith($child, 0, 1);

$this->shouldHaveType(NumericRangeCheck::class);
}

function it_implements_type_interface(TypeInterface $child)
{
$this->beConstructedWith($child, 0, 1);

$this->shouldImplement(TypeInterface::class);
}

function it_returns_a_result_on_invalid_child_check(TypeInterface $child, ResultInterface $result)
{
$result->isValid()->willReturn(false);
$child->check(Argument::any())->willReturn($result);

$this->beConstructedWith($child, 0, 1);

$this->check(Argument::any())->shouldHaveType(ResultInterface::class);
}

function it_returns_a_result_on_check(TypeInterface $child, ResultInterface $result)
{
$result->isValid()->willReturn(true);
$child->check(Argument::any())->willReturn($result);

$this->beConstructedWith($child, 0, 1);

$this->check(0)->shouldHaveType(ResultInterface::class);
}
}
78 changes: 78 additions & 0 deletions src/Check/NumericRangeCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace StructureCheck\Check;

use StructureCheck\Result;
use StructureCheck\Type\TypeInterface;

/**
* Class NumericRangeCheck
* @package StructureCheck\Check
*/
class NumericRangeCheck implements TypeInterface
{
/**
* @var string
*/
private static $lowerBoundErrorMessage = 'The given lower bound %f is greater than the given value %f.';

/**
* @var string
*/
private static $upperBoundErrorMessage = 'The given upper bound %f is smaller than the given value %f.';

/**
* @var TypeInterface
*/
private $child;

/**
* @var int
*/
private $lowerBound;

/**
* @var int
*/
private $upperBound;

/**
* @param TypeInterface $child
* @param int $upperBound
* @param int $lowerBound
*/
public function __construct(TypeInterface $child, $upperBound, $lowerBound)
{
$this->child = $child;
$this->upperBound = $upperBound;
$this->lowerBound = $lowerBound;
}

/**
* @inheritdoc
*/
public function check($value)
{
$result = $this->child->check($value);

if (!$result->isValid()) {
return $result;
}

if ($this->lowerBound > $value) {
return new Result(
false,
[sprintf(self::$lowerBoundErrorMessage, $this->lowerBound, $value)]
);
}

if ($this->upperBound < $value) {
return new Result(
false,
[sprintf(self::$upperBoundErrorMessage, $this->upperBound, $value)]
);
}

return new Result(true);
}
}