BuildArray   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A from() 0 5 1
A fromTraversable() 0 20 3
A fromEverythingElse() 0 4 1
1
<?php
2
3
/**
4
 * Copyright (c) 2015-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   DataContainers/ValueBuilders
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com
41
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link      http://code.ganbarodigital.com/php-data-containers
43
 */
44
45
namespace GanbaroDigital\DataContainers\ValueBuilders;
46
47
use GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType;
48
use GanbaroDigital\Reflection\Checks\IsTraversable;
49
use GanbaroDigital\Reflection\ValueBuilders\LookupMethodByType;
50
use GanbaroDigital\Reflection\ValueBuilders\SimpleType;
51
use Traversable;
52
53
class BuildArray
54
{
55
    use LookupMethodByType;
56
57
    /**
58
     * convert an existing data structure into an array
59
     *
60
     * @param  mixed $data
61
     *         the data type to convert
62
     * @return array
63
     */
64
    public function __invoke($data)
65
    {
66
        return self::from($data);
67
    }
68
69
    /**
70
     * convert an existing data structure into an array
71
     *
72
     * @param  mixed $data
73
     *         the data type to convert
74
     * @return array
75
     */
76
    public static function from($data)
77
    {
78
        $method = self::lookupMethodFor($data, self::$dispatchTable);
79
        return self::$method($data);
80
    }
81
82
    /**
83
     * convert an array (or traversable object) into an array
84
     *
85
     * this will do a deep conversion
86
     *
87
     * @param  Traversable $data
88
     *         the data to convert
89
     * @return array
90
     */
91
    private static function fromTraversable($data)
92
    {
93
        // our return value
94
        $retval = [];
95
96
        // walk the array
97
        foreach ($data as $key => $value) {
98
            if (IsTraversable::check($value)) {
99
                // array, object - deep copy required
100
                $retval[$key] = self::from($value);
101
            }
102
            else {
103
                // it is either opaque, or a simple type
104
                $retval[$key] = $value;
105
            }
106
        }
107
108
        // all done
109
        return $retval;
110
    }
111
112
    /**
113
     * fallback method - just wrap the data in an array
114
     *
115
     * @param  mixed $data
116
     * @return array
117
     */
118
    private static function fromEverythingElse($data)
119
    {
120
        return [ $data ];
121
    }
122
123
    /**
124
     * our lookup table of which types are handled by which method
125
     * @var array
126
     */
127
    private static $dispatchTable = [
128
        'Traversable' => 'fromTraversable',
129
        'EverythingElse' => 'fromEverythingElse',
130
    ];
131
}