Passed
Push — feature/initial-implementation ( 88960a...869acf )
by Fike
02:03
created

Mapping::setParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping;
6
7
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface;
8
use AmaTeam\ElasticSearch\Mapping\Type\RootType;
9
use stdClass;
10
11
class Mapping implements MappingInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $type;
17
    /**
18
     * @var array
19
     */
20
    private $parameters = [];
21
    /**
22
     * @var MappingInterface[]
23
     */
24
    private $properties = [];
25
26
    /**
27
     * @param string $type
28
     */
29
    public function __construct(string $type = null)
30
    {
31
        $this->type = $type;
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function getType(): ?string
38
    {
39
        return $this->type;
40
    }
41
42
    /**
43
     * @param string $type
44
     * @return $this
45
     */
46
    public function setType(string $type)
47
    {
48
        $this->type = $type;
49
        return $this;
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getParameters(): array
56
    {
57
        return $this->parameters;
58
    }
59
60
    /**
61
     * @param array $parameters
62
     * @return $this
63
     */
64
    public function setParameters(array $parameters)
65
    {
66
        $this->parameters = $parameters;
67
        return $this;
68
    }
69
70
    public function setParameter(string $parameter, $value)
71
    {
72
        $this->parameters[$parameter] = $value;
73
        return $this;
74
    }
75
76
    /**
77
     * @return MappingInterface[]
78
     */
79
    public function getProperties(): array
80
    {
81
        return $this->properties;
82
    }
83
84
    public function getProperty(string $name): ?MappingInterface
85
    {
86
        return $this->properties[$name] ?? null;
87
    }
88
89
    /**
90
     * @param MappingInterface[] $properties
91
     * @return $this
92
     */
93
    public function setProperties(array $properties)
94
    {
95
        $this->properties = $properties;
96
        return $this;
97
    }
98
99
    public function setProperty(string $property, MappingInterface $mapping): Mapping
100
    {
101
        $this->properties[$property] = $mapping;
102
        return $this;
103
    }
104
105
    public function removeProperty(string $property): Mapping
106
    {
107
        unset($this->properties[$property]);
108
        return $this;
109
    }
110
111
    public static function asObject(MappingInterface $source)
112
    {
113
        $result = new stdClass();
114
        foreach ($source->getParameters() as $parameter => $value) {
115
            $result->$parameter = $value;
116
        }
117
        if (!empty($source->getProperties())) {
118
            $properties = new stdClass();
119
            foreach ($source->getProperties() as $property => $mapping) {
120
                $properties->$property = static::asObject($mapping);
121
            }
122
            $result->properties = $properties;
123
        }
124
        if ($source->getType() && $source->getType() !== RootType::ID) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $source->getType() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
125
            $result->type = $source->getType();
126
        }
127
        return $result;
128
    }
129
130
    public static function asArray(MappingInterface $source): array
131
    {
132
        $result = [];
133
        foreach ($source->getParameters() as $parameter => $value) {
134
            $result[$parameter] = $value;
135
        }
136
        if (!empty($source->getProperties())) {
137
            $properties = [];
138
            foreach ($source->getProperties() as $property => $mapping) {
139
                $properties[$property] = static::asArray($mapping);
140
            }
141
            $result['properties'] = $properties;
142
        }
143
        if ($source->getType() && $source->getType() !== RootType::ID) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $source->getType() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
144
            $result['type'] = $source->getType();
145
        }
146
        return $result;
147
    }
148
149
    public static function merge(MappingInterface... $sources): Mapping
150
    {
151
        $target = new Mapping();
152
        foreach ($sources as $source) {
153
            if ($source->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $source->getType() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
154
                $target->setType($source->getType());
155
            }
156
            foreach ($source->getParameters() as $parameter => $value) {
157
                $target->setParameter($parameter, $value);
158
            }
159
            foreach ($source->getProperties() as $property => $mapping) {
160
                $candidate = $target->getProperty($property);
161
                $target->setProperty($property, $candidate ? static::merge($candidate, $mapping) : $mapping);
162
            }
163
        }
164
        return $target;
165
    }
166
167
    public static function from(MappingInterface $mapping): Mapping
168
    {
169
        return static::merge($mapping);
170
    }
171
}
172