Rel::extractLinks()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4888
c 0
b 0
f 0
cc 5
nc 4
nop 3
crap 5
1
<?php
2
/*
3
 * This file is part of the Respect\Rest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Respect\Rest\Routines;
10
11
use ArrayObject;
12
use Respect\Rest\Request;
13
14
class Rel extends ArrayObject implements Routinable, ProxyableThrough
15
{
16 4
    public function __construct(array $list)
17
    {
18 4
        $this->setFlags(self::ARRAY_AS_PROPS);
19 4
        $this->exchangeArray($list);
20 4
    }
21
22 4
    public function extractLinks($data, $relSpec, $deep = true)
23
    {
24 4
        if (is_callable($relSpec)) {
25 1
            return call_user_func($relSpec, $data);
26 3
        } elseif ($deep && is_array($relSpec)) {
27 2
            foreach ($relSpec as &$r) {
28 2
                $r = $this->extractLinks($data, $r, false);
29
            }
30
31 2
            return $relSpec;
32
        }
33
34 3
        return $relSpec;
35
    }
36
37 4
    public function through(Request $request, $params)
38
    {
39 4
        $rels = $this;
40
41 4
        return function ($data) use ($rels) {
42 4
            foreach ($rels as &$r) {
43 4
                $r = $rels->extractLinks($data, $r);
44
            }
45
46 4
            if (!isset($data['links'])) {
47 4
                $data['links'] = array();
48
            }
49
50 4
            $data['links'] = array_merge_recursive($data['links'], $rels->getArrayCopy());
51
52 4
            return $data;
53 4
        };
54
    }
55
}
56