Completed
Push — master ( 4ceabc...2a787d )
by Sergey
02:12
created

PlainSerializer::serializeCommon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date: 3/3/16
5
 * @time: 5:13 PM
6
 */
7
8
namespace LTDBeget\sphinx\configurator\serializers;
9
10
11
use LTDBeget\sphinx\configurator\Configuration;
12
use LTDBeget\sphinx\configurator\configurationEntities\base\Section;
13
14
/**
15
 * Class PlainSerializer
16
 * serialize Configuration object to string for file .conf
17
 * @package LTDBeget\sphinx\configurator\serializers
18
 */
19
final class PlainSerializer
20
{
21
    /**
22
     * Make plain content for sphinx configuration file from Configuration object
23
     * @param Configuration $configuration
24
     * @return string
25
     */
26
    public static function serialize(Configuration $configuration) : string
27
    {
28
        $serializer         = new self();
29
        $serializer->object = $configuration;
30
31
        return $serializer->serializeInternal();
32
    }
33
34
    /**
35
     * @internal
36
     * @return string
37
     */
38
    public function serializeInternal() : string
39
    {
40
        $this->serializeSources();
41
        $this->serializeIndexes();
42
        $this->serializeIndexer();
43
        $this->serializeSearchd();
44
        $this->serializeCommon();
45
46
        return $this->string;
47
    }
48
49
    /**
50
     * @internal
51
     */
52
    private function serializeSources()
53
    {
54
        foreach ($this->object->iterateSource() as $source) {
55
            $this->serializeSection($source);
56
        }
57
    }
58
59
    /**
60
     * @internal
61
     */
62
    private function serializeIndexes()
63
    {
64
        foreach ($this->object->iterateIndex() as $index) {
65
            $this->serializeSection($index);
66
        }
67
    }
68
69
    /**
70
     * @internal
71
     */
72
    private function serializeIndexer()
73
    {
74
        if ($this->object->isHasIndexer()) {
75
            $this->serializeSection($this->object->getIndexer());
76
        }
77
    }
78
79
    /**
80
     * @internal
81
     */
82
    private function serializeSearchd()
83
    {
84
        if ($this->object->isHasSearchd()) {
85
            $this->serializeSection($this->object->getSearchd());
86
        }
87
    }
88
89
    /**
90
     * @internal
91
     */
92
    private function serializeCommon()
93
    {
94
        if ($this->object->isHasCommon()) {
95
            $this->serializeSection($this->object->getCommon());
96
        }
97
    }
98
99
    /**
100
     * @internal
101
     * @param Section $section
102
     */
103
    private function serializeSection(Section $section)
104
    {
105
        $this->string .= "{$section}" . PHP_EOL;
106
        $this->string .= "{" . PHP_EOL;
107
        foreach ($section->iterateOptions() as $option) {
108
            $this->string .= "\t{$option}" . PHP_EOL;
109
        }
110
        $this->string .= "}" . PHP_EOL . PHP_EOL;
111
    }
112
113
    /**
114
     * @internal
115
     * ArrayDeserializer constructor.
116
     */
117
    private function __construct()
118
    {
119
    }
120
121
    /**
122
     * @var string
123
     */
124
    private $string = "";
125
126
    /**
127
     * @var Configuration
128
     */
129
    private $object;
130
}