Completed
Push — develop ( 31036d...99df5f )
by Stuart
01:53
created

ConvertKeyValuePairsToArray::fromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
/**
4
 * Copyright (c) 2016-present Ganbaro Digital Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   ArrayTools/Parsers
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://code.ganbarodigital.com/php-array-tools
42
 */
43
44
namespace GanbaroDigital\ArrayTools\Parsers;
45
46
use GanbaroDigital\ArrayTools\Exceptions\E4xx_UnsupportedType;
47
use GanbaroDigital\Reflection\Maps\MapTypeToMethod;
48
use GanbaroDigital\Reflection\ValueBuilders\SimpleType;
49
use Traversable;
50
51
class ConvertKeyValuePairsToArray
52
{
53
    /**
54
     * convert a line containing key value pairs into an array
55
     *
56
     * @param  mixed $data
57
     *         the data to convert
58
     * @param  string $kvSeparator
59
     *         the separator between keys and values
60
     * @param  string $valueSeparator
61
     *         the separator between values and the next key
62
     * @return mixed
63
     *         will return the same time that $data was
64
     */
65
    public function __invoke($data, $kvSeparator, $valueSeparator)
66
    {
67
        return self::from($data, $kvSeparator, $valueSeparator);
68
    }
69
70
    /**
71
     * convert a line containing key value pairs into an array
72
     *
73
     * @param  mixed $data
74
     *         the data to convert
75
     * @param  string $kvSeparator
76
     *         the separator between keys and values
77
     * @param  string $valueSeparator
78
     *         the separator between values and the next key
79
     * @return mixed
80
     *         will return the same time that $data was
81
     */
82
    public static function from($data, $kvSeparator, $valueSeparator)
83
    {
84
        $method = MapTypeToMethod::using($data, self::$dispatchMap);
85
        return self::$method($data, $kvSeparator, $valueSeparator);
86
    }
87
88
    /**
89
     * convert a line containing key value pairs into an array
90
     *
91
     * @param  Traversable|array $data
92
     *         the data to convert
93
     * @param  string $kvSeparator
94
     *         the separator between keys and values
95
     * @param  string $valueSeparator
96
     *         the separator between values and the next key
97
     * @return mixed
98
     *         will return the same time that $data was
99
     */
100
    private static function fromTraversable($data, $kvSeparator, $valueSeparator)
101
    {
102
        // we're going to convert the contents of $data, item by item
103
        $retval = [];
104
105
        // build our real array
106
        foreach ($data as $key => $item) {
107
            $retval[$key] = self::from($item, $kvSeparator, $valueSeparator);
108
        }
109
110
        // all done :)
111
        return $retval;
112
    }
113
114
    /**
115
     * convert a line containing key value pairs into an array
116
     *
117
     * @param  string $data
118
     *         the data to convert
119
     * @param  string $kvSeparator
120
     *         the separator between keys and values
121
     * @param  string $valueSeparator
122
     *         the separator between values and the next key
123
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
124
     *         will return the same time that $data was
125
     */
126
    private static function fromString($data, $kvSeparator, $valueSeparator)
127
    {
128
        $matches = [];
129
        preg_match_all("|([^$kvSeparator]+)$kvSeparator([^$valueSeparator]+)|", $data, $matches);
130
131
        $retval = array_combine($matches[1], $matches[2]);
132
        return $retval;
133
    }
134
135
    /**
136
     * called when we have a data type that we cannot convert
137
     *
138
     * @param  mixed $data
139
     *         the data to convert
140
     * @return void
141
     * @throws E4xx_UnsupportedType
142
     */
143
    private static function nothingMatchesTheInputType($data)
144
    {
145
        throw new E4xx_UnsupportedType(SimpleType::from($data));
146
    }
147
148
    /**
149
     * lookup map of how to convert which data type
150
     *
151
     * @var array
152
     */
153
    private static $dispatchMap = [
154
        'String' => 'fromString',
155
        'Traversable' => 'fromTraversable',
156
    ];
157
}
158