1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @category Library
|
4
|
|
|
* @license MIT http://opensource.org/licenses/MIT
|
5
|
|
|
* @link https://github.com/emlynwest/changelog
|
6
|
|
|
*/
|
7
|
|
|
|
8
|
|
|
namespace ChangeLog\Renderer;
|
9
|
|
|
|
10
|
|
|
use ChangeLog\Log;
|
11
|
|
|
use ChangeLog\Release;
|
12
|
|
|
use ChangeLog\RenderInterface;
|
13
|
|
|
use SimpleXMLElement;
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
* Allows change logs to be rendered to XML.
|
17
|
|
|
*/
|
18
|
|
|
class Xml implements RenderInterface
|
19
|
|
|
{
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* {@inheritdoc}
|
23
|
|
|
*/
|
24
|
1 |
|
public function render(Log $log)
|
25
|
|
|
{
|
26
|
1 |
|
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><log />');
|
27
|
|
|
|
28
|
1 |
|
$xml->addChild('title', $log->getTitle());
|
29
|
1 |
|
$xml->addChild('description', $log->getDescription());
|
30
|
|
|
|
31
|
1 |
|
$releases = $xml->addChild('releases');
|
32
|
|
|
|
33
|
|
|
/** @var Release $release */
|
34
|
1 |
|
foreach ($log as $release)
|
35
|
|
|
{
|
36
|
1 |
|
$this->renderRelease($releases, $release);
|
37
|
|
|
}
|
38
|
|
|
|
39
|
1 |
|
$xml = $xml->asXML();
|
40
|
1 |
|
return ($xml !== false) ? $xml : '' ;
|
|
|
|
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* Adds the release's changes to the given xml release node.
|
45
|
|
|
*
|
46
|
|
|
* @param SimpleXMLElement $releaseNode
|
47
|
|
|
* @param Release $release
|
48
|
|
|
*/
|
49
|
1 |
|
protected function addChanges(SimpleXMLElement $releaseNode, Release $release)
|
50
|
|
|
{
|
51
|
1 |
|
$changesNode = $releaseNode->addChild('changes');
|
52
|
|
|
|
53
|
1 |
|
foreach ($release->getAllChanges() as $type => $changes)
|
54
|
|
|
{
|
55
|
1 |
|
$typeNode = $changesNode->addChild('type');
|
56
|
1 |
|
$typeNode->addAttribute('name', $type);
|
57
|
|
|
|
58
|
1 |
|
foreach ($changes as $change)
|
59
|
|
|
{
|
60
|
1 |
|
$typeNode->addChild('change', $change);
|
61
|
|
|
}
|
62
|
|
|
}
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
/**
|
66
|
|
|
* @param SimpleXMLElement $releases
|
67
|
|
|
* @param Release $release
|
68
|
|
|
*/
|
69
|
1 |
|
protected function renderRelease($releases, $release)
|
70
|
|
|
{
|
71
|
1 |
|
$releaseNode = $releases->addChild('release');
|
72
|
1 |
|
$releaseNode->addChild('name', $release->getName());
|
73
|
1 |
|
$releaseNode->addChild('link', $release->getLink());
|
74
|
|
|
|
75
|
1 |
|
if ($release->getLinkName() !== null)
|
|
|
|
|
76
|
|
|
{
|
77
|
1 |
|
$releaseNode->addChild('linkName', $release->getLinkName());
|
78
|
|
|
}
|
79
|
|
|
|
80
|
1 |
|
if ($release->getDate() !== null)
|
81
|
|
|
{
|
82
|
1 |
|
$releaseNode->addChild('date',
|
83
|
1 |
|
$release->getDate()
|
84
|
1 |
|
->format('Y-m-d')
|
85
|
1 |
|
);
|
86
|
|
|
}
|
87
|
|
|
|
88
|
1 |
|
$this->addChanges($releaseNode, $release);
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
}
|
92
|
|
|
|