|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SRF\Formats\Tree; |
|
4
|
|
|
|
|
5
|
|
|
use Tree\Node\NodeInterface; |
|
6
|
|
|
use Tree\Visitor\Visitor; |
|
7
|
|
|
|
|
8
|
|
|
class TreeNodePrinter implements Visitor { |
|
9
|
|
|
|
|
10
|
|
|
private $depth = 0; |
|
11
|
|
|
private $rowNumber = 0; |
|
12
|
|
|
private $configuration = null; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var TreeResultPrinter |
|
15
|
|
|
*/ |
|
16
|
|
|
private $resultPrinter = null; |
|
17
|
|
|
private $columnLabels = []; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct( TreeResultPrinter $resultPrinter, $configuration ) { |
|
20
|
|
|
$this->configuration = $configuration; |
|
21
|
|
|
$this->resultPrinter = $resultPrinter; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function visit( NodeInterface $node ) { |
|
25
|
|
|
|
|
26
|
|
|
$nodeTexts = [ $this->getTextForNode( $node ) ]; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$this->depth++; |
|
29
|
|
|
$this->rowNumber++; |
|
30
|
|
|
|
|
31
|
|
|
foreach ( $node->getChildren() as $child ) { |
|
32
|
|
|
$nodeTexts = array_merge( |
|
33
|
|
|
$nodeTexts, |
|
34
|
|
|
$child->accept( $this ) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->depth--; |
|
39
|
|
|
|
|
40
|
|
|
return $nodeTexts; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function getTextForNode( TreeNode $node ) { |
|
44
|
|
|
|
|
45
|
|
|
/** @var \SMWResultArray[]|null $row */ |
|
46
|
|
|
$row = $node->getValue(); |
|
47
|
|
|
|
|
48
|
|
|
if ( $row === null ) { |
|
49
|
|
|
return ''; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$textForNode = str_repeat( ( $this->configuration[ 'format' ] === 'oltree' ) ? '#' : '*', $this->depth ); |
|
53
|
|
|
|
|
54
|
|
|
if ( $this->configuration[ 'template' ] === '' ) { |
|
55
|
|
|
// build simple list |
|
56
|
|
|
$textForNode .= $this->getTextForRowNoTemplate( $row ); |
|
57
|
|
|
} else { |
|
58
|
|
|
// build template code |
|
59
|
|
|
$textForNode .= $this->getTextForRowWithTemplate( $row ); |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $textForNode; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \SMWResultArray[] $row |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getTextForRowNoTemplate( $row ) { |
|
71
|
|
|
|
|
72
|
|
|
$cellTexts = []; |
|
73
|
|
|
foreach ( $row as $columnNumber => $cell ) { |
|
74
|
|
|
|
|
75
|
|
|
$valuesText = $this->getValuesTextForCell( $cell, $columnNumber ); |
|
76
|
|
|
|
|
77
|
|
|
if ( $valuesText === '' ) { |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$labelText = $this->getLabelForCell( $cell, $columnNumber ); |
|
82
|
|
|
|
|
83
|
|
|
$cellTexts[] = $labelText . $valuesText; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ( count( $cellTexts ) > 0 ) { |
|
87
|
|
|
$result = array_shift( $cellTexts ); |
|
88
|
|
|
|
|
89
|
|
|
if ( count( $cellTexts ) > 0 ) { |
|
90
|
|
|
$result .= ' (' . join( $this->configuration[ 'sep' ], $cellTexts ) . ')'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} else { |
|
94
|
|
|
$result = ''; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param \SMWResultArray[] $row |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getTextForRowWithTemplate( $row ) { |
|
105
|
|
|
|
|
106
|
|
|
$templateParams = []; |
|
107
|
|
|
foreach ( $row as $columnNumber => $cell ) { |
|
108
|
|
|
|
|
109
|
|
|
$valuesText = $this->getValuesTextForCell( $cell, $columnNumber ); |
|
110
|
|
|
$paramName = $this->getParamNameForCell( $cell, $columnNumber ); |
|
111
|
|
|
|
|
112
|
|
|
$templateParams[] = "$paramName=$valuesText "; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$templateParams[] = "#=$this->rowNumber "; |
|
116
|
|
|
|
|
117
|
|
|
return $this->resultPrinter->getTemplateCall( $this->configuration[ 'template' ], $templateParams ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param \SMWResultArray $cell |
|
122
|
|
|
* @param int $columnNumber |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getValuesTextForCell( \SMWResultArray $cell, $columnNumber ) { |
|
126
|
|
|
|
|
127
|
|
|
$cell->reset(); |
|
128
|
|
|
$linker = $this->resultPrinter->getLinkerForColumn( $columnNumber ); |
|
129
|
|
|
|
|
130
|
|
|
$valueTexts = []; |
|
131
|
|
|
|
|
132
|
|
|
while ( ( $text = $cell->getNextText( SMW_OUTPUT_WIKI, $linker ) ) !== false ) { |
|
133
|
|
|
$valueTexts[] = $text; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$valuesText = join( $this->configuration[ 'sep' ], $valueTexts ); |
|
137
|
|
|
return $valuesText; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param \SMWResultArray $cell |
|
142
|
|
|
* @param int $columnNumber |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function getParamNameForCell( $cell, $columnNumber ) { |
|
146
|
|
|
|
|
147
|
|
|
if ( !array_key_exists( $columnNumber, $this->columnLabels ) ) { |
|
148
|
|
|
|
|
149
|
|
|
$label = $cell->getPrintRequest()->getLabel(); |
|
150
|
|
|
|
|
151
|
|
|
if ( $this->configuration[ 'template arguments' ] === 'numbered' || ( $label === '' ) ) { |
|
152
|
|
|
$paramName = $columnNumber + 1; |
|
153
|
|
|
} elseif ( $this->configuration[ 'template arguments' ] === 'legacy' ) { |
|
154
|
|
|
$paramName = '?' . $label; |
|
155
|
|
|
} else { // $this->configuration[ 'template arguments' ] === 'named' |
|
156
|
|
|
$paramName = $label; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->columnLabels[ $columnNumber ] = $paramName; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $this->columnLabels[ $columnNumber ]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param \SMWResultArray $cell |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function getLabelForCell( $cell, $columnNumber ) { |
|
170
|
|
|
|
|
171
|
|
|
if ( !array_key_exists( $columnNumber, $this->columnLabels ) ) { |
|
172
|
|
|
|
|
173
|
|
|
if ( $this->configuration[ 'headers' ] === 'hide' || $cell->getPrintRequest()->getLabel() === '' ) { |
|
174
|
|
|
$labelText = ''; |
|
175
|
|
|
} elseif ( $this->configuration[ 'headers' ] === 'plain' ) { |
|
176
|
|
|
$labelText = $cell->getPrintRequest()->getText( SMW_OUTPUT_WIKI ) . ': '; |
|
177
|
|
|
} else { // $this->configuration[ 'headers' ] === 'link' |
|
178
|
|
|
$labelText = $cell->getPrintRequest()->getText( SMW_OUTPUT_WIKI, $this->resultPrinter->getLinker() ) . ': '; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$this->columnLabels[ $columnNumber ] = $labelText; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $this->columnLabels[ $columnNumber ]; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.