LinkRelation::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 16
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Koriym\AppStateDiagram;
6
7
use Koriym\AppStateDiagram\Exception\InvalidLinkRelationException;
8
use stdClass;
9
10
use function json_encode;
11
use function sprintf;
12
13
final class LinkRelation
14
{
15
    /** @var string */
16
    public $href;
17
18
    /** @var string */
19
    public $rel;
20
21
    /** @var string */
22
    public $title;
23
24
    public function __construct(stdClass $link)
25
    {
26
        if (! isset($link->href)) {
27
            throw new InvalidLinkRelationException((string) json_encode($link));
28
        }
29
30
        if (! isset($link->rel)) {
31
            throw new InvalidLinkRelationException((string) json_encode($link));
32
        }
33
34
        /** @psalm-suppress MixedAssignment */
35
        $this->href = $link->href;
36
        /** @psalm-suppress MixedAssignment */
37
        $this->rel = $link->rel;
38
        /** @psalm-suppress MixedAssignment */
39
        $this->title = $link->title ?? '';
40
    }
41
42
    public function __toString(): string
43
    {
44
        return sprintf('   * %s', $this->toLink());
45
    }
46
47
    private function toLink(): string
48
    {
49
        $str = sprintf('rel: %s <a rel="%s" href="%s">%s</a>', $this->rel, $this->rel, $this->href, $this->href);
50
        if ($this->title !== '') {
51
            $str .= " {$this->title}";
52
        }
53
54
        return $str;
55
    }
56
}
57