Passed
Push — new-api ( 18d26d...074931 )
by Sebastian
04:59
created

Info::factory()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 22
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 28
ccs 23
cts 23
cp 1
crap 8
rs 8.4444
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2017 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Root;
12
13
use SimpleXMLElement;
14
use stdClass;
15
16
class Info
17
{
18
    /**
19
     * @var string
20
     */
21
    private $title;
22
23
    /**
24
     * @var string
25
     */
26
    private $id;
27
28
    /**
29
     * @var array
30
     */
31
    private $authors;
32
33
    /**
34
     * @var array
35
     */
36
    private $links;
37
38 163
    public static function factory(SimpleXMLElement $node): Info
39
    {
40 163
        $authors = [];
41 163
        $links = [];
42 163
        $id = null;
43 163
        $title = null;
44 163
        foreach ($node->children() as $child) {
45 161
            switch ($child->getName()) {
46 161
                case 'author':
47 161
                case 'contributor':
48 41
                    $author = new stdClass();
49
                    /** @var SimpleXMLElement $authorNode */
50 41
                    foreach ($child->children() as $authorNode) {
51 41
                        $author->{$authorNode->getName()} = (string) $authorNode;
52
                    }
53 41
                    $authors[] = $author;
54 41
                    break;
55 161
                case 'link':
56 41
                    $links[] = (string) $child->attributes()['href'];
57 41
                    break;
58 161
                case 'id':
59 159
                    $id = (string) $child;
60 159
                    break;
61 161
                case 'title':
62 161
                    $title = (string) $child;
63
            }
64
        }
65 163
        return new Info($id, $title, $authors, $links);
66
    }
67
68 163
    public function __construct(?string $id, ?string $title, array $authors, array $links)
69
    {
70 163
        $this->id = $id;
71 163
        $this->title = $title;
72 163
        $this->authors = $authors;
73 163
        $this->links = $links;
74 163
    }
75
76
    /**
77
     * @return string
78
     */
79 1
    public function getTitle(): ?string
80
    {
81 1
        return $this->title;
82
    }
83
84
    /**
85
     * @return string
86
     */
87 1
    public function getId(): ?string
88
    {
89 1
        return $this->id;
90
    }
91
92
    /**
93
     * @return array
94
     */
95 1
    public function getAuthors(): array
96
    {
97 1
        return $this->authors;
98
    }
99
100
    /**
101
     * @return array
102
     */
103 1
    public function getLinks(): array
104
    {
105 1
        return $this->links;
106
    }
107
}
108