Skip to content

Commit 55740b3

Browse files
TuningYourCodeChristian Blank
authored and
Christian Blank
committedAug 10, 2018
Add EnumType #19 (#20)
* Add EnumType #19 * Remove unsupported hhvm; Add php7.2; Update Readme.md
1 parent 992a0bc commit 55740b3

File tree

8 files changed

+85
-5
lines changed

8 files changed

+85
-5
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ language: php
44
matrix:
55
include:
66
- php: 5.6
7-
- php: hhvm
87
- php: 7.0
98
- php: 7.1
9+
- php: 7.2
1010
- php: nightly
1111
allow_failures:
1212
- php: nightly

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Currently the following types are supported:
8484
* Datetime
8585
* Regex
8686
* Optional
87+
* Enum
8788

8889
There are some open issues with ideas for more types. Feel free to send pull requests.
8990

‎spec/Type/EnumTypeSpec.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace spec\StructureCheck\Type;
4+
5+
use StructureCheck\Type\EnumType;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class EnumTypeSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->beConstructedWith(['test', 1, null]);
13+
14+
$this->shouldHaveType(EnumType::class);
15+
}
16+
17+
function it_is_valid_for_all_allowed_values()
18+
{
19+
$this->beConstructedWith(['test', 1, null]);
20+
21+
$this->check('test')->isValid()->shouldBe(true);
22+
$this->check(1)->isValid()->shouldBe(true);
23+
$this->check(null)->isValid()->shouldBe(true);
24+
}
25+
26+
function it_is_invalid_for_not_allowed_values()
27+
{
28+
$this->beConstructedWith(['test', 1, null]);
29+
30+
$this->check('array')->isValid()->shouldBe(false);
31+
$this->check(100)->isValid()->shouldBe(false);
32+
$this->check(1.5)->isValid()->shouldBe(false);
33+
$this->check(['test'])->isValid()->shouldBe(false);
34+
}
35+
}

‎src/Type/DatetimeType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Class DatetimeType
1212
* @package StructureCheck\Type
1313
*/
14-
class DatetimeType
14+
class DatetimeType implements TypeInterface
1515
{
1616
/**
1717
* @var string

‎src/Type/EnumType.php

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

‎src/Type/ExactValueType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use StructureCheck\Result;
66
use StructureCheck\ResultInterface;
77

8-
class ExactValueType
8+
class ExactValueType implements TypeInterface
99
{
1010

1111
/**

‎src/Type/NumericType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use StructureCheck\Result;
66
use StructureCheck\ResultInterface;
77

8-
class NumericType
8+
class NumericType implements TypeInterface
99
{
1010
private static $errorMessage = 'The value %s is not a numeric value.';
1111

‎src/Type/RegexType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Class RegexType
1010
* @package StructureCheck\Type
1111
*/
12-
class RegexType
12+
class RegexType implements TypeInterface
1313
{
1414
/**
1515
* @var string

0 commit comments

Comments
 (0)
Please sign in to comment.