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

StringEnumTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 31
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toSchema() 0 5 1
A sanitizeValue() 0 7 2
A getValidValues() 0 4 1
A validValue() 0 4 2
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