1 | <?php |
||
18 | class MarkdownRenderer extends AbstractRenderer |
||
19 | { |
||
20 | public $indent = ' '; |
||
21 | |||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | public function render(History $history) |
||
26 | { |
||
27 | $this->setHistory($history); |
||
28 | |||
29 | return $this->renderSparse([ |
||
30 | $this->renderHeaders(), |
||
31 | $this->renderTags(), |
||
32 | $this->renderLinks(), |
||
33 | ]); |
||
34 | } |
||
35 | |||
36 | public function renderHeaders() |
||
37 | { |
||
38 | return $this->renderText($this->getHistory()->getHeaders()); |
||
39 | } |
||
40 | |||
41 | public function renderTags() |
||
42 | { |
||
43 | return $this->renderObjects('renderTag', $this->getHistory()->getTags(), true); |
||
44 | } |
||
45 | |||
46 | public function renderLinks($links = null) |
||
47 | { |
||
48 | if ($links === null) { |
||
49 | $links = $this->getHistory()->getLinks(); |
||
50 | } |
||
51 | |||
52 | return $this->renderObjects('renderLink', $links); |
||
53 | } |
||
54 | |||
55 | public function renderObjects($method, $objects, $sparse = false) |
||
56 | { |
||
57 | $res = []; |
||
58 | foreach ($objects as $key => $value) { |
||
59 | $res[$key] = call_user_func([$this, $method], $value, $key); |
||
60 | } |
||
61 | |||
62 | return $this->renderText($res, $sparse); |
||
63 | } |
||
64 | |||
65 | public function renderSparse(array $lines) |
||
66 | { |
||
67 | $res = rtrim(implode("\n", $lines)); |
||
68 | |||
69 | return $res ? $res . "\n" : ''; |
||
70 | } |
||
71 | |||
72 | public function renderText(array $lines, $sparse = false) |
||
73 | { |
||
74 | if (!$sparse) { |
||
75 | foreach ($lines as &$line) { |
||
76 | $line = rtrim($line); |
||
77 | } |
||
78 | $lines = array_filter($lines); |
||
79 | } |
||
80 | |||
81 | return $this->renderSparse($lines); |
||
82 | } |
||
83 | |||
84 | public function renderLink($href, $link) |
||
85 | { |
||
86 | return "[$link]: $href"; |
||
87 | } |
||
88 | |||
89 | public function renderHeader($header) |
||
93 | |||
94 | public function renderTag(Tag $tag) |
||
95 | { |
||
96 | return $this->renderSparse([ |
||
97 | $this->renderTagHead($tag), |
||
98 | $this->renderObjects('renderNote', $tag->getNotes()), |
||
99 | ]); |
||
100 | } |
||
101 | |||
102 | public function renderTagHead(Tag $tag) |
||
111 | |||
112 | public function renderNote(Note $note) |
||
119 | |||
120 | public function renderNoteHead(Note $note) |
||
124 | |||
125 | public function renderCommit(Commit $commit) |
||
132 | |||
133 | public function renderCommitHead(Commit $commit) |
||
137 | } |
||
138 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: