StringParser::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php namespace Nord\Lumen\Search;
2
3
use Nord\Lumen\Search\Exceptions\InvalidArgument;
4
5
class StringParser
6
{
7
8
    /**
9
     * @var string
10
     */
11
    private $separator = ';';
12
13
    /**
14
     * @var string
15
     */
16
    private $delimiter = ':';
17
18
19
    /**
20
     * Configuration constructor.
21
     *
22
     * @param array $config
23
     */
24
    public function __construct(array $config = [])
25
    {
26
        $this->configure($config);
27
    }
28
29
30
    /**
31
     * @param $string
32
     *
33
     * @return array
34
     * @throws InvalidArgument
35
     */
36
    public function parse($string)
37
    {
38
        if (!is_string($string)) {
39
            throw new InvalidArgument('Cannot parse non-string values.');
40
        }
41
42
        $array = [];
43
44
        foreach ($this->splitItems($string) as $item) {
45
            $array[] = $this->splitItem($item);
46
        }
47
48
        return $array;
49
    }
50
51
52
    /**
53
     * @param array $config
54
     */
55 View Code Duplication
    protected function configure(array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        if (isset($config['separator'])) {
58
            $this->separator = $config['separator'];
59
        }
60
61
        if (isset($config['delimiter'])) {
62
            $this->delimiter = $config['delimiter'];
63
        }
64
    }
65
66
67
    /**
68
     * @param string $string
69
     *
70
     * @return array
71
     */
72
    protected function splitItems($string)
73
    {
74
        return strpos($string, $this->separator) !== false ? explode($this->separator, $string) : [$string];
75
    }
76
77
78
    /**
79
     * @param string $string
80
     *
81
     * @return array
82
     */
83
    protected function splitItem($string)
84
    {
85
        return explode($this->delimiter, $string);
86
    }
87
}
88