Paths::getWantedType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Apie\OpenapiSchema\Spec;
4
5
use Apie\CompositeValueObjects\Exceptions\InvalidKeyException;
6
use Apie\CompositeValueObjects\ValueObjectHashmapTrait;
7
use Apie\CompositeValueObjects\ValueObjectListInterface;
8
use Apie\TypeJuggling\AnotherValueObject;
9
use Apie\TypeJuggling\Compound;
10
use Apie\TypeJuggling\MixedTypehint;
11
use Apie\TypeJuggling\TypeUtilInterface;
12
use Apie\ValueObjects\Exceptions\InvalidValueForValueObjectException;
13
14
/**
15
 * @see https://swagger.io/specification/#paths-object
16
 */
17
class Paths implements ValueObjectListInterface
18
{
19
    use ValueObjectHashmapTrait;
20
21
    private function __construct()
22
    {
23
    }
24
25
    protected function sanitizeValue(): void
26
    {
27
        foreach (array_keys($this->list) as $key) {
28
            if (substr($key, 0, 1) === '/') {
29
                return;
30
            }
31
        }
32
        throw new InvalidValueForValueObjectException($this->list, $this);
33
    }
34
35
    protected static function getWantedType(string $fieldName): TypeUtilInterface
36
    {
37
        if (stripos($fieldName, 'x-') === 0) {
38
            return new MixedTypehint($fieldName);
39
        }
40
        if (substr($fieldName, 0, 1) === '/') {
41
            return new Compound(
42
                $fieldName,
43
                new AnotherValueObject($fieldName, Reference::class),
44
                new AnotherValueObject($fieldName, PathItem::class)
45
            );
46
        }
47
        throw new InvalidKeyException($fieldName, new Paths());
48
    }
49
}
50