Util   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 37
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A parseRelationshipPaths() 0 18 4
1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
class Util
15
{
16
    /**
17
     * Parse relationship paths.
18
     *
19
     * Given a flat array of relationship paths like:
20
     *
21
     * ['user', 'user.employer', 'user.employer.country', 'comments']
22
     *
23
     * create a nested array of relationship paths one-level deep that can
24
     * be passed on to other serializers:
25
     *
26
     * ['user' => ['employer', 'employer.country'], 'comments' => []]
27
     *
28
     * @param array $paths
29
     *
30
     * @return array
31
     */
32 36
    public static function parseRelationshipPaths(array $paths)
33
    {
34 36
        $tree = [];
35
36 36
        foreach ($paths as $path) {
37 15
            list($primary, $nested) = array_pad(explode('.', $path, 2), 2, null);
38
39 15
            if (! isset($tree[$primary])) {
40 15
                $tree[$primary] = [];
41 15
            }
42
43 15
            if ($nested) {
44 3
                $tree[$primary][] = $nested;
45 3
            }
46 36
        }
47
48 36
        return $tree;
49
    }
50
}
51