Skip to content

Commit f1a4ee6

Browse files
author
Christian Blank
authored
Add exact value type
This solves #6
1 parent 9b2c197 commit f1a4ee6

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

spec/Type/ExactValueTypeSpec.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace spec\StructureCheck\Type;
4+
5+
use StructureCheck\Type\ExactValueType;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
9+
class ExactValueTypeSpec extends ObjectBehavior
10+
{
11+
function it_is_initializable()
12+
{
13+
$this->beConstructedWith(null);
14+
$this->shouldHaveType(ExactValueType::class);
15+
}
16+
17+
function it_is_valid_for_null_if_null_is_the_value()
18+
{
19+
$this->beConstructedWith(null);
20+
$this->check(null)->isValid()->shouldBe(true);
21+
}
22+
}

spec/Type/NumericTypeSpec.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace spec\StructureCheck\Type;
4+
5+
use StructureCheck\Type\NumericType;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
9+
class NumericTypeSpec extends ObjectBehavior
10+
{
11+
function it_is_initializable()
12+
{
13+
$this->shouldHaveType(NumericType::class);
14+
}
15+
16+
function it_should_return_valid_for_integers()
17+
{
18+
$this->check(0)->isValid()->shouldBe(true);
19+
$this->check(1)->isValid()->shouldBe(true);
20+
$this->check(20)->isValid()->shouldBe(true);
21+
$this->check(-144)->isValid()->shouldBe(true);
22+
}
23+
24+
function it_should_return_valid_for_floats()
25+
{
26+
$this->check(0.0)->isValid()->shouldBe(true);
27+
$this->check(1.1235)->isValid()->shouldBe(true);
28+
$this->check(-0.00001)->isValid()->shouldBe(true);
29+
$this->check(-144.12313131313)->isValid()->shouldBe(true);
30+
}
31+
32+
function it_should_return_invalid_for_others()
33+
{
34+
$this->check(null)->isValid()->shouldBe(false);
35+
$this->check("foo")->isValid()->shouldBe(false);
36+
$this->check([])->isValid()->shouldBe(false);
37+
$this->check(true)->isValid()->shouldBe(false);
38+
$this->check(false)->isValid()->shouldBe(false);
39+
}
40+
}

src/Type/ExactValueType.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace StructureCheck\Type;
4+
5+
use StructureCheck\Result;
6+
use StructureCheck\ResultInterface;
7+
8+
class ExactValueType
9+
{
10+
11+
/**
12+
* @var string
13+
*/
14+
private static $errorMessage = 'The value %s is not the same value as %s.';
15+
16+
/**
17+
* ExactValueType constructor.
18+
*
19+
* @param mixed $value
20+
*/
21+
public function __construct($value)
22+
{
23+
$this->value = $value;
24+
}
25+
26+
/**
27+
* @param mixed $value
28+
*
29+
* @return ResultInterface
30+
*/
31+
public function check($value)
32+
{
33+
$checkResult = $this->value === $value;
34+
35+
return new Result(
36+
$checkResult,
37+
!$checkResult ? [sprintf(self::$errorMessage, json_encode($value), $this->value)] : []
38+
);
39+
}
40+
}

src/Type/NumericType.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace StructureCheck\Type;
4+
5+
use StructureCheck\Result;
6+
use StructureCheck\ResultInterface;
7+
8+
class NumericType
9+
{
10+
private static $errorMessage = 'The value %s is not a numeric value.';
11+
12+
/**
13+
* @param mixed $value
14+
*
15+
* @return ResultInterface
16+
*/
17+
public function check($value)
18+
{
19+
$checkResult = is_numeric($value);
20+
21+
return new Result(
22+
$checkResult,
23+
!$checkResult ? [sprintf(self::$errorMessage, json_encode($value))] : []
24+
);
25+
}
26+
}

0 commit comments

Comments
 (0)