Test Setup Failed
Push — main ( e03535...dde247 )
by Pieter
03:39
created

CompositeValueObjectWithExtension::toNative()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 9.9666
1
<?php
2
3
4
namespace Apie\OpenapiSchema\Concerns;
5
6
use Apie\CompositeValueObjects\CompositeValueObjectTrait;
7
use Apie\CompositeValueObjects\Exceptions\FieldMissingException;
8
use Apie\OpenapiSchema\ValueObjects\SpecificationExtension;
9
use Apie\ValueObjects\Exceptions\InvalidValueForValueObjectException;
10
use Apie\ValueObjects\ValueObjectInterface;
11
12
trait CompositeValueObjectWithExtension
13
{
14
    use CompositeValueObjectTrait {
15
        toNative as private internalToArray;
16
        supportsFromNative as private internalSupportsFromNative;
17
        fromNative as private internalFromArray;
18
        with as private internalWith;
19
    }
20
21
    /**
22
     * @var \Apie\OpenapiSchema\ValueObjects\SpecificationExtension|null
23
     */
24
    private $specificationExtension;
25
26
    public static function supportsFromNative($value)
27
    {
28
        if ($value instanceof ValueObjectInterface) {
29
            $value = $value->toNative();
30
        }
31
        if (!is_array($value)) {
32
            return false;
33
        }
34
        $new = [];
35
        foreach ($value as $key => $val) {
36
            if (stripos($key, 'x-') !== 0) {
37
                $new[$key] = $val;
38
            }
39
        }
40
        return static::internalSupportsFromNative($new);
41
    }
42
43
    public function toNative()
44
    {
45
        $result = $this->internalToArray();
46
        if (isset($result['specificationExtension'])) {
47
            $extensions = $result['specificationExtension'];
48
            unset($result['specificationExtension']);
49
            $result = array_merge($result, $extensions);
50
        }
51
        return array_filter(
52
            $result,
53
            function ($value) {
54
                return $value !== null;
55
            }
56
        );
57
    }
58
59
    public static function fromNative($input)
60
    {
61
        if ($input instanceof ValueObjectInterface) {
62
            $input = $input->toNative();
63
        }
64
        if (!is_array($input)) {
65
            throw new InvalidValueForValueObjectException($input, static::class);
66
        }
67
        unset($input['specificationExtension']);
68
        $list = [];
69
        $list['specificationExtension'] = [];
70
        foreach ($input as $key => $value) {
71
            if (stripos($key, 'x-') === 0) {
72
                $list['specificationExtension'][$key] = $value;
73
            } else {
74
                $list[$key] = $value;
75
            }
76
        }
77
        return self::internalFromArray($list);
78
    }
79
80
    public function with(string $fieldName, $value): self
81
    {
82
        if ($fieldName === 'specificationExtension') {
83
            throw new FieldMissingException($fieldName, $this);
84
        }
85
        if (stripos($fieldName, 'x-') === 0) {
86
            $object = clone $this;
87
            if ($object->specificationExtension) {
88
                $object->specificationExtension = $this->specificationExtension->withField($fieldName, $value);
0 ignored issues
show
Bug introduced by
The method withField() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
                /** @scrutinizer ignore-call */ 
89
                $object->specificationExtension = $this->specificationExtension->withField($fieldName, $value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
            } else {
90
                $object->specificationExtension = new SpecificationExtension([$fieldName => $value]);
91
            }
92
            return $object;
93
        }
94
        return $this->internalWith($fieldName, $value);
95
    }
96
}
97