Completed
Push — master ( c34cac...3c68a7 )
by Pieter
04:47
created

StringEnumTrait::sanitizeValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
namespace W2w\Lib\Apie\Plugins\ValueObject\ValueObjects;
3
4
use erasys\OpenApi\Spec\v3\Schema;
5
use ReflectionClass;
6
7
trait StringEnumTrait
8
{
9
    use StringTrait { toSchema as private internalToSchema; }
10
11
    final protected function validValue(string $value): bool
12
    {
13
        $values = self::getValidValues();
14
        return isset($values[$value]) || false !== array_search($value, $values, true);
15
    }
16
17
    final protected function sanitizeValue(string $value): string
18
    {
19
        $values = self::getValidValues();
20
        if (isset($values[$value])) {
21
            return $values[$value];
22
        }
23
        return $value;
24
    }
25
26
27
    final public static function getValidValues()
28
    {
29
        $reflectionClass = new ReflectionClass(__CLASS__);
30
        return $reflectionClass->getConstants();
31
    }
32
33
    final static public function toSchema(): Schema
34
    {
35
        $schema = self::internalToSchema();
36
        $schema->enum = array_values(self::getValidValues());
37
        return $schema;
38
    }
39
40
}
41