AbstractStringParser::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php namespace Nord\Lumen\Elasticsearch\Parsers;
2
3
class AbstractStringParser
4
{
5
    /**
6
     * @var string
7
     */
8
    private $separator = ';';
9
10
    /**
11
     * @var string
12
     */
13
    private $delimiter = ':';
14
15
16
    /**
17
     * Configuration constructor.
18
     *
19
     * @param array $config
20
     */
21
    public function __construct(array $config = [])
22
    {
23
        $this->configure($config);
24
    }
25
26
27
    /**
28
     * @param string $string
29
     *
30
     * @return array
31
     */
32
    public function parse(string $string)
33
    {
34
        $array = [];
35
36
        foreach ($this->splitItems($string) as $item) {
37
            $array[] = $this->splitItem($item);
38
        }
39
40
        return $array;
41
    }
42
43
44
    /**
45
     * @param array $config
46
     */
47
    protected function configure(array $config)
48
    {
49
        if (isset($config['separator'])) {
50
            $this->separator = $config['separator'];
51
        }
52
53
        if (isset($config['delimiter'])) {
54
            $this->delimiter = $config['delimiter'];
55
        }
56
    }
57
58
59
    /**
60
     * @param string $string
61
     *
62
     * @return array
63
     */
64
    protected function splitItems($string)
65
    {
66
        return strpos($string, $this->separator) !== false ? explode($this->separator, $string) : [$string];
67
    }
68
69
70
    /**
71
     * @param string $string
72
     *
73
     * @return array
74
     */
75
    protected function splitItem($string)
76
    {
77
        return explode($this->delimiter, $string);
78
    }
79
}
80