Passed
Push — feature/initial-implementation ( 79751f...3b7c72 )
by Fike
01:53
created

Mapping::asObject()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 8
nop 1
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
     * @return string
28
     */
29
    public function getType(): string
30
    {
31
        return $this->type;
32
    }
33
34
    /**
35
     * @param string $type
36
     * @return $this
37
     */
38
    public function setType(string $type)
39
    {
40
        $this->type = $type;
41
        return $this;
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function getParameters(): array
48
    {
49
        return $this->parameters;
50
    }
51
52
    /**
53
     * @param array $parameters
54
     * @return $this
55
     */
56
    public function setParameters(array $parameters)
57
    {
58
        $this->parameters = $parameters;
59
        return $this;
60
    }
61
62
    public function setParameter(string $parameter, $value)
63
    {
64
        $this->parameters[$parameter] = $value;
65
        return $this;
66
    }
67
68
    /**
69
     * @return MappingInterface[]
70
     */
71
    public function getProperties(): array
72
    {
73
        return $this->properties;
74
    }
75
76
    /**
77
     * @param MappingInterface[] $properties
78
     * @return $this
79
     */
80
    public function setProperties(array $properties)
81
    {
82
        $this->properties = $properties;
83
        return $this;
84
    }
85
86
    public static function asObject(MappingInterface $source)
87
    {
88
        $result = new stdClass();
89
        foreach ($source->getParameters() as $parameter => $value) {
90
            $result->$parameter = $value;
91
        }
92
        if (!empty($source->getProperties())) {
93
            $properties = new stdClass();
94
            foreach ($source->getProperties() as $property => $mapping) {
95
                $properties->$property = static::asObject($mapping);
96
            }
97
            $result->properties = $properties;
98
        }
99
        if ($source->getType() && $source->getType() !== RootType::ID) {
100
            $result->type = $source->getType();
101
        }
102
        return $result;
103
    }
104
}
105