Completed
Push — master ( 0348fd...4ceabc )
by Sergey
02:16
created

ArraySerializer::serializeDefinition()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 9.2
cc 3
eloc 13
nc 4
nop 1
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date: 3/14/16
5
 * @time: 12:54 PM
6
 */
7
8
namespace LTDBeget\sphinx\configurator\serializers;
9
10
11
use LTDBeget\sphinx\configurator\Configuration;
12
use LTDBeget\sphinx\configurator\configurationEntities\base\Definition;
13
use LTDBeget\sphinx\configurator\configurationEntities\base\Settings;
14
use LTDBeget\sphinx\configurator\exceptions\LogicException;
15
16
/**
17
 * Class ArraySerializer
18
 * serialize Configuration object to array
19
 * @package LTDBeget\sphinx\configurator\serializers
20
 */
21
class ArraySerializer
22
{
23
    /**
24
     * Make array represent of Configuration object
25
     * @param Configuration $configuration
26
     * @return array
27
     */
28
    public static function serialize(Configuration $configuration) : array
29
    {
30
        $serializer                      = new self();
31
        $serializer->objectConfiguration = $configuration;
32
33
        return $serializer->serializeInternal();
34
    }
35
36
    /**
37
     * @internal
38
     * ArraySerializer constructor.
39
     */
40
    private function __construct() {}
41
42
    /**
43
     * @internal
44
     * @return array
45
     */
46
    private function serializeInternal() : array
47
    {
48
        $this->serializeSource();
49
        $this->serializeIndex();
50
        $this->serializeIndexer();
51
        $this->serializeSearchhd();
52
        $this->serializeCommon();
53
54
        return $this->arrayConfiguration;
55
    }
56
57
    /**
58
     * @internal
59
     * @throws LogicException
60
     */
61
    private function serializeSource()
62
    {
63
        foreach ($this->objectConfiguration->iterateSource() as $source) {
64
            $this->serializeDefinition($source);
65
        }
66
    }
67
68
    /**
69
     * @internal
70
     * @throws LogicException
71
     */
72
    private function serializeIndex()
73
    {
74
        foreach ($this->objectConfiguration->iterateIndex() as $index) {
75
            $this->serializeDefinition($index);
76
        }
77
    }
78
79
    /**
80
     * @internal
81
     */
82
    private function serializeIndexer()
83
    {
84
        if ($this->objectConfiguration->isHasIndexer()) {
85
            $this->serializeSettings($this->objectConfiguration->getIndexer());
86
        }
87
    }
88
89
    /**
90
     * @internal
91
     */
92
    private function serializeSearchhd()
93
    {
94
        if ($this->objectConfiguration->isHasSearchd()) {
95
            $this->serializeSettings($this->objectConfiguration->getSearchd());
96
        }
97
    }
98
99
    /**
100
     * @internal
101
     */
102
    private function serializeCommon()
103
    {
104
        if ($this->objectConfiguration->isHasCommon()) {
105
            $this->serializeSettings($this->objectConfiguration->getCommon());
106
        }
107
    }
108
109
    /**
110
     * @internal
111
     * @param Definition $definition
112
     * @throws LogicException
113
     */
114
    private function serializeDefinition(Definition $definition)
115
    {
116
        $node = [
117
            "type"        => "source",
118
            "name"        => $definition->getName(),
119
            "inheritance" => "",
120
            "options"     => []
121
        ];
122
123
        if ($definition->isHasInheritance()) {
124
            $node["inheritance"] = $definition->getInheritance();
125
        }
126
127
        foreach ($definition->iterateOptions() as $option) {
128
            $node["options"][] = [
129
                "name"  => $option->getName(),
130
                "value" => $option->getValue()
131
            ];
132
        }
133
134
        $this->arrayConfiguration[] = $node;
135
    }
136
137
    /**
138
     * @internal
139
     * @param Settings $settings
140
     */
141
    private function serializeSettings(Settings $settings)
142
    {
143
        $node = [
144
            "type"    => (string) $settings->getType(),
145
            "options" => []
146
        ];
147
148
        foreach ($settings->iterateOptions() as $option) {
149
            $node["options"][] = [
150
                "name"  => $option->getName(),
151
                "value" => $option->getValue()
152
            ];
153
        }
154
155
        $this->arrayConfiguration[] = $node;
156
    }
157
158
    /**
159
     * @var array
160
     */
161
    private $arrayConfiguration = [];
162
163
    /**
164
     * @var Configuration
165
     */
166
    private $objectConfiguration = null;
167
}