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

Section::renderOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date  : 3/18/16
5
 * @time  : 5:11 PM
6
 */
7
8
namespace LTDBeget\sphinx\configurator\configurationEntities\base;
9
10
use LTDBeget\sphinx\configurator\Configuration;
11
use LTDBeget\sphinx\configurator\configurationEntities\Option;
12
use LTDBeget\sphinx\enums\base\eOption;
13
use LTDBeget\sphinx\enums\eSection;
14
use LTDBeget\sphinx\informer\Informer;
15
use ReflectionClass;
16
17
/**
18
 * Class Section
19
 *
20
 * @package LTDBeget\sphinx\configurator\configurationEntities\base
21
 * @method Option addOption(eOption $name, string $value)
22
 */
23
abstract class Section
24
{
25
    /**
26
     * Section constructor.
27
     *
28
     * @param Configuration $configuration
29
     */
30 15
    public function __construct(Configuration $configuration)
31
    {
32 15
        $this->configuration = $configuration;
33 15
    }
34
35
    /**
36
     * @return string
37
     */
38 15
    public function className() : string
39
    {
40 15
        return get_called_class();
41
    }
42
43
    /**
44
     * @return eSection
45
     * @throws \InvalidArgumentException
46
     * @throws \LogicException
47
     */
48 15
    final public function getType() : eSection
49
    {
50 15
        if (NULL === $this->type) {
51 15
            $this->initType();
52
        }
53
54 15
        return $this->type;
55
    }
56
57
    /**
58
     * @return Configuration
59
     */
60 14
    final public function getConfiguration() : Configuration
61
    {
62 14
        return $this->configuration;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 3
    public function __toString() : string
69
    {
70
        try {
71 3
            $string = '';
72 3
            $string .= $this->renderDefineBlock() . PHP_EOL;
73 3
            $string .= $this->renderOptions();
74
        } catch (\Exception $e) {
75
            $string = '';
76
        }
77
78 3
        return $string;
79
    }
80
81
    /**
82
     * @return array
83
     * @throws \LogicException
84
     * @throws \InvalidArgumentException
85
     */
86 1
    public function toArray()
87
    {
88
        return [
89 1
            'type'    => (string) $this->getType(),
90 1
            'options' => $this->toArrayOptions()
91
        ];
92
    }
93
94
    /**
95
     * @return Option[]
96
     */
97 4
    final public function iterateOptions()
98
    {
99 4
        foreach ($this->options as $option) {
100 4
            if (is_array($option)) {
101 3
                foreach ($option as $multiOption) {
102
                    /**
103
                     * @var Option $multiOption
104
                     */
105 3
                    if (!$multiOption->isDeleted()) {
106 3
                        yield $multiOption;
107
                    }
108
                }
109
            } else {
110 4
                if (!$option->isDeleted()) {
111 4
                    yield $option;
112
                }
113
            }
114
        }
115 4
    }
116
117
    /**
118
     * mark section as deleted
119
     */
120 2
    public function delete()
121
    {
122 2
        $this->isDeleted = true;
123 2
    }
124
125
    /**
126
     * is option marked as deleted
127
     *
128
     * @return bool
129
     */
130 12
    final public function isDeleted()
131
    {
132 12
        return $this->isDeleted;
133
    }
134
135
    /**
136
     * @param eOption $name
137
     * @param string  $value
138
     *
139
     * @return Option
140
     * @throws \LogicException
141
     * @throws \InvalidArgumentException
142
     * @throws \LTDBeget\sphinx\informer\exceptions\InformerRuntimeException
143
     */
144 9
    final protected function addOptionInternal(eOption $name, string $value) : Option
145
    {
146 9
        $option      = $this->createOption($name, $value);
147 8
        $option_name = (string) $option->getName();
148
149 8
        if ($option->isMultiValue()) {
150 6
            $this->options[$option_name] = $this->options[$option_name] ?? [];
151
            /** @noinspection OffsetOperationsInspection */
152 6
            $this->options[$option_name][] = $option;
153
        } else {
154 7
            $this->options[$option_name] = $option;
155
        }
156
157 8
        return $option;
158
    }
159
160
    /**
161
     * @internal
162
     * @return Informer
163
     */
164 9
    protected function getInformer() : Informer
165
    {
166
        return $this
167 9
            ->getConfiguration()
168 9
            ->getInformer();
169
    }
170
171
    /**
172
     * @return string
173
     * @throws \LogicException
174
     * @throws \InvalidArgumentException
175
     */
176 2
    protected function renderDefineBlock() : string
177
    {
178 2
        return (string) $this->getType();
179
    }
180
181
    /**
182
     * @return string
183
     */
184 3
    protected function renderOptions() : string
185
    {
186 3
        $string = '';
187 3
        $string .= '{' . PHP_EOL;
188 3
        foreach ($this->iterateOptions() as $option) {
189 2
            $string .= "\t{$option}" . PHP_EOL;
190
        }
191 3
        $string .= '}' . PHP_EOL . PHP_EOL;
192
193 3
        return $string;
194
    }
195
196
    /**
197
     * @return array
198
     */
199 1
    protected function toArrayOptions() : array
200
    {
201 1
        $options = [];
202
203 1
        foreach ($this->iterateOptions() as $option) {
204 1
            $options[] = [
205 1
                'name'  => (string) $option->getName(),
206 1
                'value' => $option->getValue()
207
            ];
208
        }
209
210 1
        return $options;
211
    }
212
213
    /**
214
     * @internal
215
     * @return string
216
     */
217 15
    private function shortClassName() : string
218
    {
219 15
        return (new ReflectionClass($this->className()))->getShortName();
220
    }
221
222
    /**
223
     * @internal
224
     *
225
     * @param eOption $name
226
     * @param string  $value
227
     *
228
     * @return Option
229
     * @throws \LogicException
230
     * @throws \InvalidArgumentException
231
     * @throws \LTDBeget\sphinx\informer\exceptions\InformerRuntimeException
232
     */
233 9
    final private function createOption(eOption $name, string $value)
234
    {
235 9
        $informer     = $this->getInformer();
236 9
        $isMultiValue = $informer->getOptionInfo($this->getType(), $name)->isIsMultiValue();
237
238 8
        return new Option($this, $name, $value, $isMultiValue);
239
    }
240
241
    /**
242
     * @internal
243
     * @throws \InvalidArgumentException
244
     * @throws \LogicException
245
     */
246 15
    private function initType()
247
    {
248 15
        $this->type = eSection::get(strtolower($this->shortClassName()));
249 15
    }
250
251
    /**
252
     * @var Configuration
253
     */
254
    private $configuration;
255
256
    /**
257
     * @var eSection
258
     */
259
    private $type;
260
261
    /**
262
     * @var Option[]
263
     */
264
    private $options = [];
265
266
    /**
267
     * @var boolean
268
     */
269
    private $isDeleted = false;
270
}