Enum::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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