Playlist   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 23 2
A __construct() 0 3 1
A addTrack() 0 3 1
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