Enum   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A validate() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Constraint;
6
7
class Enum extends Constraint
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $enumValues = [];
13
14
    /**
15
     * @var bool
16
     */
17
    protected $strictType;
18
19
    public function __construct(array $enumValues, string $errorMessage = null, $strictType = false)
20
    {
21
        $this->enumValues = $enumValues;
22
        $this->strictType = $strictType;
23
24
        $this->setErrorMessage(
25
            $errorMessage ?? 'Invalid option for enum. Allowed options are: ' . implode(', ', $this->enumValues)
26
        );
27
    }
28
29
    public function validate($content): bool
30
    {
31
        if (!is_scalar($content)) {
32
            return false;
33
        }
34
35
        return in_array($content, $this->enumValues, $this->strictType);
36
    }
37
}
38