Passed
Push — master ( 10e07d...93334d )
by Sergey
03:08
created

ArraySerializer::serializeSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 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
use LTDBeget\sphinx\configurator\Configuration;
11
12
/**
13
 * Class ArraySerializer
14
 * serialize Configuration object to array
15
 *
16
 * @package LTDBeget\sphinx\configurator\serializers
17
 */
18
class ArraySerializer
19
{
20
    /**
21
     * @var array
22
     */
23
    private $arrayConfiguration = [];
24
    /**
25
     * @var Configuration
26
     */
27
    private $objectConfiguration;
28
29
    /**
30
     * @internal
31
     * ArraySerializer constructor.
32
     */
33 1
    private function __construct()
34
    {
35 1
    }
36
37
    /**
38
     * Make array represent of Configuration object
39
     *
40
     * @param Configuration $configuration
41
     *
42
     * @return array
43
     * @throws \LogicException
44
     * @throws \LTDBeget\sphinx\configurator\exceptions\SectionException
45
     * @throws \InvalidArgumentException
46
     * @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException
47
     */
48 1
    public static function serialize(Configuration $configuration) : array
49
    {
50 1
        $serializer                      = new self();
51 1
        $serializer->objectConfiguration = $configuration;
52
53 1
        return $serializer->serializeInternal();
54
    }
55
56
    /**
57
     * @internal
58
     * @return array
59
     * @throws \LTDBeget\sphinx\configurator\exceptions\SectionException
60
     * @throws \LogicException
61
     * @throws \InvalidArgumentException
62
     * @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException
63
     */
64 1
    private function serializeInternal() : array
65
    {
66 1
        $this->serializeSource();
67 1
        $this->serializeIndex();
68 1
        $this->serializeIndexer();
69 1
        $this->serializeSearchhd();
70 1
        $this->serializeCommon();
71
72 1
        return $this->arrayConfiguration;
73
    }
74
75
    /**
76
     * @internal
77
     * @throws \InvalidArgumentException
78
     * @throws \LTDBeget\sphinx\configurator\exceptions\SectionException
79
     * @throws \LogicException
80
     */
81 1
    private function serializeSource()
82
    {
83 1
        foreach ($this->objectConfiguration->iterateSource() as $source) {
84 1
            $this->arrayConfiguration[] = $source->toArray();
85
        }
86 1
    }
87
88
    /**
89
     * @internal
90
     * @throws \InvalidArgumentException
91
     * @throws \LTDBeget\sphinx\configurator\exceptions\SectionException
92
     * @throws \LogicException
93
     */
94 1
    private function serializeIndex()
95
    {
96 1
        foreach ($this->objectConfiguration->iterateIndex() as $index) {
97 1
            $this->arrayConfiguration[] = $index->toArray();
98
        }
99 1
    }
100
101
    /**
102
     * @internal
103
     * @throws \InvalidArgumentException
104
     * @throws \LogicException
105
     */
106 1
    private function serializeIndexer()
107
    {
108 1
        if ($this->objectConfiguration->isHasIndexer()) {
109 1
            $this->arrayConfiguration[] = $this->objectConfiguration->getIndexer()->toArray();
110
        }
111 1
    }
112
113
    /**
114
     * @internal
115
     * @throws \InvalidArgumentException
116
     * @throws \LogicException
117
     */
118 1
    private function serializeSearchhd()
119
    {
120 1
        if ($this->objectConfiguration->isHasSearchd()) {
121 1
            $this->arrayConfiguration[] = $this->objectConfiguration->getSearchd()->toArray();
122
        }
123 1
    }
124
125
    /**
126
     * @internal
127
     * @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException
128
     * @throws \InvalidArgumentException
129
     * @throws \LogicException
130
     */
131 1
    private function serializeCommon()
132
    {
133 1
        if ($this->objectConfiguration->isHasCommon()) {
134 1
            $this->arrayConfiguration[] = $this->objectConfiguration->getCommon()->toArray();
135
        }
136
    }
137
}