InteractsWithIdentifier::setIdentifiers()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2222
cc 6
nc 4
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEpub\Traits;
6
7
use SimpleXMLElement;
8
9
trait InteractsWithIdentifier
10
{
11
    private readonly SimpleXMLElement $opfXml;
12
    private readonly string $dcNamespace;
13
14
    /**
15
     * Gets the authors of the EPUB.
16
     *
17
     * @return array<int, string>
18
     */
19 9
    public function getIdentifiers(): array
20
    {
21 9
        $this->opfXml->registerXPathNamespace('dc', $this->dcNamespace);
22
23 9
        $identifierNodes = $this->opfXml->xpath('//dc:identifier');
24
25 9
        $identifiers = [];
26
27 9
        if ($identifierNodes === false || $identifierNodes === null || $identifierNodes === []) {
28
            return $identifiers;
29
        }
30
31 9
        foreach ($identifierNodes as $identifierNode) {
32 9
            $identifiers[] = (string) $identifierNode;
33
        }
34 9
        return $identifiers;
35
    }
36
37
    /**
38
     * Sets the identifiers of the EPUB.
39
     *
40
     * @param array<int, string> $identifiers
41
     */
42 1
    public function setIdentifiers(array $identifiers): void
43
    {
44 1
        $this->opfXml->registerXPathNamespace('dc', $this->dcNamespace);
45
46 1
        $identifierNodes = $this->opfXml->xpath('//dc:identifier');
47
48 1
        if ($identifierNodes !== false && $identifierNodes !== null && $identifierNodes !== []) {
49 1
            foreach ($identifierNodes as $key => $identifierNode) {
50 1
                unset($identifierNodes[$key][0]);
51
            }
52
        }
53
54 1
        foreach ($identifiers as $identifier) {
55 1
            $this->opfXml->metadata->addChild('identifier', $identifier, $this->dcNamespace);
56
        }
57
    }
58
}
59