SpecificationExtension::withField()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
4
namespace Apie\OpenapiSchema\ValueObjects;
5
6
use Apie\ValueObjects\ValueObjectInterface;
7
8
class SpecificationExtension implements ValueObjectInterface
9
{
10
    private $extraFields;
11
12
    public function __construct(array $extraFields)
13
    {
14
        $res = [];
15
        foreach ($extraFields as $fieldName => $extraField) {
16
            if (stripos($fieldName, 'x-') === 0) {
17
                $res[strtolower($fieldName)] = $extraField;
18
            } else {
19
                $res['x-' . strtolower($fieldName)] = $extraField;
20
            }
21
        }
22
        $this->extraFields = $res;
23
    }
24
25
    public function toNative(): array
26
    {
27
        return $this->extraFields;
28
    }
29
30
    public function withField(string $fieldName, $fieldValue): self
31
    {
32
        $array = $this->extraFields;
33
        if (stripos($fieldName, 'x-') === 0) {
34
            $array[strtolower($fieldName)] = $fieldValue;
35
        } else {
36
            $array['x-' . strtolower($fieldName)] = $fieldValue;
37
        }
38
        return new self($array);
39
    }
40
41
    public static function fromNative($value)
42
    {
43
        return new self($value);
44
    }
45
}
46