Skip to content

Add EnumType #19 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 10, 2018
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ language: php
matrix:
include:
- php: 5.6
- php: hhvm
- php: 7.0
- php: 7.1
- php: 7.2
- php: nightly
allow_failures:
- php: nightly
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Currently the following types are supported:
* Datetime
* Regex
* Optional
* Enum

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

Expand Down
35 changes: 35 additions & 0 deletions spec/Type/EnumTypeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace spec\StructureCheck\Type;

use StructureCheck\Type\EnumType;
use PhpSpec\ObjectBehavior;

class EnumTypeSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->beConstructedWith(['test', 1, null]);

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

function it_is_valid_for_all_allowed_values()
{
$this->beConstructedWith(['test', 1, null]);

$this->check('test')->isValid()->shouldBe(true);
$this->check(1)->isValid()->shouldBe(true);
$this->check(null)->isValid()->shouldBe(true);
}

function it_is_invalid_for_not_allowed_values()
{
$this->beConstructedWith(['test', 1, null]);

$this->check('array')->isValid()->shouldBe(false);
$this->check(100)->isValid()->shouldBe(false);
$this->check(1.5)->isValid()->shouldBe(false);
$this->check(['test'])->isValid()->shouldBe(false);
}
}
2 changes: 1 addition & 1 deletion src/Type/DatetimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Class DatetimeType
* @package StructureCheck\Type
*/
class DatetimeType
class DatetimeType implements TypeInterface
{
/**
* @var string
Expand Down
44 changes: 44 additions & 0 deletions src/Type/EnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace StructureCheck\Type;

use StructureCheck\Result;
use StructureCheck\ResultInterface;

class EnumType implements TypeInterface
{
/**
* @var array
*/
private $values = [];

/**
* @var string
*/
private static $errorMessage = 'The value %s is not in the allowed values (%s).';

/**
* EnumType constructor.
*
* @param array $values
*/
public function __construct(array $values)
{
$this->values = $values;
}

/**
* @param mixed $value
*
* @return ResultInterface
*/
public function check($value)
{
$checkResult = in_array($value, $this->values, true);

return new Result(
$checkResult,
!$checkResult ? [sprintf(self::$errorMessage, json_encode($value), implode(',', $this->values))] : []
);
}
}
2 changes: 1 addition & 1 deletion src/Type/ExactValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use StructureCheck\Result;
use StructureCheck\ResultInterface;

class ExactValueType
class ExactValueType implements TypeInterface
{

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Type/NumericType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use StructureCheck\Result;
use StructureCheck\ResultInterface;

class NumericType
class NumericType implements TypeInterface
{
private static $errorMessage = 'The value %s is not a numeric value.';

Expand Down
2 changes: 1 addition & 1 deletion src/Type/RegexType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Class RegexType
* @package StructureCheck\Type
*/
class RegexType
class RegexType implements TypeInterface
{
/**
* @var string
Expand Down