Completed
Push — master ( d34cec...faecd8 )
by Augusto
02:22
created

Rel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 2
            }
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 4
            }
45
46 4
            if (!isset($data['links'])) {
47 4
                $data['links'] = array();
48 4
            }
49
50 4
            $data['links'] = array_merge_recursive($data['links'], $rels->getArrayCopy());
51
52 4
            return $data;
53 4
        };
54
    }
55
}
56