Completed
Push — master ( dc1094...5b5a29 )
by Jean C.
9s
created

Links::parseLinks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace Moip;
4
5
/**
6
 * Class that represents the HATEOAS structure of the API resources.
7
 */
8
class Links
9
{
10
    /**
11
     * @var array[string]Link Map a link name to it's representation.
12
     */
13
    private $links;
14
15
    /**
16
     * Links constructor.
17
     *
18
     * @param \stdClass $links "_link" returned from the API, if there isn't any.
19
     */
20
    public function __construct(\stdClass $links)
21
    {
22
        $this->parseLinks($links);
23
    }
24
25
    /**
26
     * @param \stdClass $links
27
     */
28
    private function parseLinks(\stdClass $links)
29
    {
30
        foreach (get_object_vars($links) as $property => $value) {
31
            if ($property == 'checkout') { // nested links? eg: checkout=>payOnlineBankDebitItau,payCreditCard...
32
                $this->parseLinks($value);
33
            } else {
34
                $this->links[$property] = new Link($property, $value);
35
            }
36
        }
37
    }
38
39
    /**
40
     * Returns all the links from the HATEOAS structure.
41
     *
42
     * @return array[string]Link.
0 ignored issues
show
Documentation introduced by
The doc-type array[string]Link. could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
43
     */
44
    public function getLinks()
45
    {
46
        return $this->links;
47
    }
48
49
    /**
50
     * @param $name string Links name, e.g: "self".
51
     *
52
     * @return null|Link Link object of the corresponding $name or null if the link doesn't exist.
53
     */
54
    public function getLink($name)
55
    {
56
        if (!isset($this->links[$name])) {
57
            return;
58
        }
59
        return $this->links[$name];
60
    }
61
}
62