Playlist::__toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace Jalle19\xsphpf;
4
5
/**
6
 * Class Playlist
7
 * @package Jalle19\xsphpf
8
 */
9
class Playlist
10
{
11
12
    const XSPF_XMLNS   = 'http://xspf.org/ns/0/';
13
    const XSPF_VERSION = 1;
14
15
    /**
16
     * @var Track[]
17
     */
18
    private $trackList;
19
20
    /**
21
     * Playlist constructor.
22
     *
23
     * @param array $trackList
24
     */
25
    public function __construct(array $trackList = [])
26
    {
27
        $this->trackList = $trackList;
28
    }
29
30
    /**
31
     * @param Track $track
32
     */
33
    public function addTrack(Track $track)
34
    {
35
        $this->trackList[] = $track;
36
    }
37
38
    /**
39
     * Serializes the playlist into XML
40
     */
41
    public function __toString()
42
    {
43
        $document = new \DOMDocument('1.0', 'UTF-8');
44
45
        // Create and add the playlist element
46
        $playlist = $document->createElement('playlist');
47
        $playlist->setAttribute('version', self::XSPF_VERSION);
48
        $playlist->setAttribute('xmlns', self::XSPF_XMLNS);
49
        $document->appendChild($playlist);
50
51
        // Create and add the trackList element
52
        $trackList = $document->createElement('trackList');
53
        $playlist->appendChild($trackList);
54
55
        // Add track elements
56
        foreach ($this->trackList as $track) {
57
            $trackList->appendChild($track->getDomElement($document));
58
        }
59
60
        // Use pretty printing
61
        $document->formatOutput = true;
62
63
        return $document->saveXML();
64
    }
65
66
}
67