Completed
Push — master ( f0b37c...fb09c3 )
by Paul
10s
created

AbstractResource::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LibLynx\Connect\Resource;
4
5
use LibLynx\Connect\Exception\LogicException;
6
7
/**
8
 * Class LibLynxResource provides a base class for all HAL-style resources retrieved from the LibLynx API.
9
 *
10
 * All JSON returned by the LibLynx API is modelled on the Hypertext Application Language
11
 * http://stateless.co/hal_specification.html
12
 *
13
 * @package LibLynx\Connect
14
 *
15
 * @property \stdClass $_links
16
 */
17
abstract class AbstractResource
18
{
19 10
    public function __construct($obj)
20
    {
21 10
        $vars = get_object_vars($obj);
22 10
        foreach ($vars as $name => $value) {
23 10
            $this->$name = $value;
24
        }
25 10
    }
26
27 2
    public function __get($name)
28
    {
29 2
        throw new LogicException("No value called $name");
30
    }
31
32
    /**
33
     * @param $name
34
     * @return mixed
35
     * @throws LogicException if link with name isn't present
36
     */
37 4
    public function getLink($name)
38
    {
39 4
        if (isset($this->_links->$name)) {
40 2
            return $this->_links->$name->href;
41
        }
42 2
        throw new LogicException("resource did not contain a ".$name." link");
43
    }
44
45 2
    public function hasLink($name)
46
    {
47 2
        return isset($this->_links->$name);
48
    }
49
}
50