Skip to content

Commit a050d81

Browse files
author
Christian Blank
authored
5 numeric range check (#14)
* Wip: numeric range check * Create numeric range check
1 parent 09775e5 commit a050d81

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

spec/Check/NumericRangeCheckSpec.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace spec\StructureCheck\Check;
4+
5+
use Prophecy\Argument;
6+
use StructureCheck\Check\NumericRangeCheck;
7+
use PhpSpec\ObjectBehavior;
8+
use StructureCheck\ResultInterface;
9+
use StructureCheck\Type\TypeInterface;
10+
11+
class NumericRangeCheckSpec extends ObjectBehavior
12+
{
13+
14+
function it_is_initializable(TypeInterface $child)
15+
{
16+
$this->beConstructedWith($child, 0, 1);
17+
18+
$this->shouldHaveType(NumericRangeCheck::class);
19+
}
20+
21+
function it_implements_type_interface(TypeInterface $child)
22+
{
23+
$this->beConstructedWith($child, 0, 1);
24+
25+
$this->shouldImplement(TypeInterface::class);
26+
}
27+
28+
function it_returns_a_result_on_invalid_child_check(TypeInterface $child, ResultInterface $result)
29+
{
30+
$result->isValid()->willReturn(false);
31+
$child->check(Argument::any())->willReturn($result);
32+
33+
$this->beConstructedWith($child, 0, 1);
34+
35+
$this->check(Argument::any())->shouldHaveType(ResultInterface::class);
36+
}
37+
38+
function it_returns_a_result_on_check(TypeInterface $child, ResultInterface $result)
39+
{
40+
$result->isValid()->willReturn(true);
41+
$child->check(Argument::any())->willReturn($result);
42+
43+
$this->beConstructedWith($child, 0, 1);
44+
45+
$this->check(0)->shouldHaveType(ResultInterface::class);
46+
}
47+
}

src/Check/NumericRangeCheck.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace StructureCheck\Check;
4+
5+
use StructureCheck\Result;
6+
use StructureCheck\Type\TypeInterface;
7+
8+
/**
9+
* Class NumericRangeCheck
10+
* @package StructureCheck\Check
11+
*/
12+
class NumericRangeCheck implements TypeInterface
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private static $lowerBoundErrorMessage = 'The given lower bound %f is greater than the given value %f.';
18+
19+
/**
20+
* @var string
21+
*/
22+
private static $upperBoundErrorMessage = 'The given upper bound %f is smaller than the given value %f.';
23+
24+
/**
25+
* @var TypeInterface
26+
*/
27+
private $child;
28+
29+
/**
30+
* @var int
31+
*/
32+
private $lowerBound;
33+
34+
/**
35+
* @var int
36+
*/
37+
private $upperBound;
38+
39+
/**
40+
* @param TypeInterface $child
41+
* @param int $upperBound
42+
* @param int $lowerBound
43+
*/
44+
public function __construct(TypeInterface $child, $upperBound, $lowerBound)
45+
{
46+
$this->child = $child;
47+
$this->upperBound = $upperBound;
48+
$this->lowerBound = $lowerBound;
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
public function check($value)
55+
{
56+
$result = $this->child->check($value);
57+
58+
if (!$result->isValid()) {
59+
return $result;
60+
}
61+
62+
if ($this->lowerBound > $value) {
63+
return new Result(
64+
false,
65+
[sprintf(self::$lowerBoundErrorMessage, $this->lowerBound, $value)]
66+
);
67+
}
68+
69+
if ($this->upperBound < $value) {
70+
return new Result(
71+
false,
72+
[sprintf(self::$upperBoundErrorMessage, $this->upperBound, $value)]
73+
);
74+
}
75+
76+
return new Result(true);
77+
}
78+
}

0 commit comments

Comments
 (0)