HalLinkContainer::get()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
/**
3
 * This file is part of the Hal library
4
 *
5
 * (c) Ben Longden <[email protected]
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @package Nocarrier
11
 */
12
13
namespace Nocarrier;
14
15
/**
16
 * The HalLinkContainer class
17
 *
18
 * @package Nocarrier
19
 * @author Ben Longden <[email protected]>
20
 */
21
class HalLinkContainer extends \ArrayObject
22
{
23
    /**
24
     * Retrieve a link from the container by rel. Also resolve any curie links
25
     * if they are set.
26
     *
27
     * @param string $rel
28
     *   The link relation required.
29
     * @return array|bool
30
     *   Link if found. Otherwise false.
31
     */
32
    public function get($rel)
33
    {
34
        if (array_key_exists($rel, (array) $this)) {
35
            return $this[$rel];
36
        }
37
38
        if (isset($this['curies'])) {
39
            foreach ($this['curies'] as $link) {
40
                $prefix = strstr($link->getUri(), '{rel}', true);
41
                if (strpos($rel, $prefix) === 0) {
42
                    // looks like it is
43
                    $shortrel = substr($rel, strlen($prefix));
44
                    $attrs = $link->getAttributes();
45
                    $curie = "{$attrs['name']}:$shortrel";
46
                    if (isset($this[$curie])) {
47
                        return $this[$curie];
48
                    }
49
                }
50
            }
51
        }
52
53
        return false;
54
    }
55
}
56