forked from CubiclDev/php-structure-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumType.php
44 lines (37 loc) · 871 Bytes
/
EnumType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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))] : []
);
}
}