Completed
Pull Request — master (#2)
by Angel
03:03
created

Curies::getCuriesLinks()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 4
nc 2
nop 0
1
<?php
2
3
namespace roaresearch\yii2\roa\behaviors;
4
5
use yii\{helpers\Url, web\Link};
6
7
/**
8
 * Behavior to autogenerate curies links.
9
 *
10
 * @author Angel (Faryshta) Guevara <[email protected]>
11
 */
12
class Curies extends \yii\base\Behavior
13
{
14
    /**
15
     * @var Link[] The curies links. Autogenerated links will be stored here.
16
     */
17
    public $curiesLinks = [];
18
19
    /**
20
     * @var ?string name of the autogenerated expand curie. Set null if you
21
     * don't want to autogenerate expand curie.
22
     */
23
    public $expandCurieName = 'expand';
24
25
    /**
26
     * @var array the configuration used to generate the expand curie.
27
     */
28
    public $expandCurieLink = [
29
        'title' => 'Embeddable related resources.',
30
    ];
31
32
    /**
33
     * Return all the curies and related links.
34
     *
35
     * @return array the configured and autogenerated curies and related links.
36
     */
37
    public function getCuriesLinks(): array
38
    {
39
        if ($this->expandCurieName) {
40
            $this->curiesLinks['curies'][] = new Link(array_merge(
41
                $this->expandCurieLink,
42
                [
43
                    'name' => $this->expandCurieName,
44
                    'href' => $this->owner->getSelfLink()
45
                        . "?{$this->expandCurieName}={rel}",
46
                ]
47
            ));
48
49
            foreach ($this->owner->extraFields() as $field => $value) {
50
                if (is_int($field)) {
51
                    $field = $value;
52
                }
53
54
                $this->curiesLinks["{$this->expandCurieName}:$field"] = $field;
55
            }
56
        }
57
58
        return $this->curiesLinks;
59
    }
60
}
61