|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Changelog keeper |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/chkipper |
|
7
|
|
|
* @package chkipper |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hiqdev\chkipper\history; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Markdown history renderer. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class MarkdownRenderer extends AbstractRenderer |
|
20
|
|
|
{ |
|
21
|
|
|
public $indent = ' '; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function render(History $history) |
|
27
|
|
|
{ |
|
28
|
2 |
|
$this->setHistory($history); |
|
29
|
|
|
|
|
30
|
2 |
|
return $this->renderSparse([ |
|
|
|
|
|
|
31
|
2 |
|
$this->renderHeaders(), |
|
32
|
2 |
|
$this->renderTags(), |
|
33
|
2 |
|
$this->renderLinks(), |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
public function renderHeaders() |
|
38
|
|
|
{ |
|
39
|
2 |
|
return $this->renderText($this->getHistory()->getHeaders()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public function renderTags() |
|
43
|
|
|
{ |
|
44
|
2 |
|
return $this->renderObjects('renderTag', $this->getHistory()->getTags(), true); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function renderLinks() |
|
48
|
|
|
{ |
|
49
|
2 |
|
return $this->renderObjects('renderLink', $this->getHistory()->getLinks()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function renderObjects($method, $objects, $sparse = false) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$res = []; |
|
55
|
2 |
|
foreach ($objects as $key => $value) { |
|
56
|
2 |
|
$res[$key] = call_user_func([$this, $method], $value, $key); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
return $this->renderText($res, $sparse); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
public function renderSparse(array $lines) |
|
63
|
|
|
{ |
|
64
|
2 |
|
$res = rtrim(implode("\n", $lines)); |
|
65
|
|
|
|
|
66
|
2 |
|
return $res ? $res . "\n" : ''; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
public function renderText(array $lines, $sparse = false) |
|
70
|
|
|
{ |
|
71
|
2 |
|
if (!$sparse) { |
|
72
|
2 |
|
foreach ($lines as &$line) { |
|
73
|
2 |
|
$line = rtrim($line); |
|
74
|
|
|
} |
|
75
|
2 |
|
$lines = array_filter($lines); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return $this->renderSparse($lines); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public function renderLink($href, $link) |
|
82
|
|
|
{ |
|
83
|
2 |
|
return "[$link]: $href"; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function renderHeader($header) |
|
87
|
|
|
{ |
|
88
|
|
|
return $header; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
public function renderTag(Tag $tag) |
|
92
|
|
|
{ |
|
93
|
2 |
|
return $this->renderSparse([ |
|
94
|
2 |
|
$this->renderTagHead($tag), |
|
95
|
2 |
|
$this->renderObjects('renderNote', $tag->getNotes()), |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
public function renderTagHead(Tag $tag) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$res = '## [' . $tag->getName() . ']'; |
|
102
|
2 |
|
if ($tag->getDate()) { |
|
|
|
|
|
|
103
|
2 |
|
$res .= ' - ' . $tag->getDate(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
return $res . "\n"; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
public function renderNote(Note $note) |
|
110
|
|
|
{ |
|
111
|
2 |
|
return $this->renderText([ |
|
112
|
2 |
|
$this->renderNoteHead($note), |
|
113
|
2 |
|
$this->renderObjects('renderCommit', $note->getCommits()), |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
public function renderNoteHead(Note $note) |
|
118
|
|
|
{ |
|
119
|
2 |
|
return $note->getNote() ? '- ' . $note->getNote() : ''; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
public function renderCommit(Commit $commit) |
|
123
|
|
|
{ |
|
124
|
2 |
|
return $this->renderText([ |
|
125
|
2 |
|
$this->renderCommitHead($commit), |
|
126
|
2 |
|
$this->renderText($commit->getComments()), |
|
127
|
|
|
]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
2 |
|
public function renderCommitHead(Commit $commit) |
|
131
|
|
|
{ |
|
132
|
2 |
|
return $this->indent . '- [' . $commit->getHash() . '] ' . $commit->getLabel(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.