Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CargoDisplayFormat | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
allowedParameters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isDeferred | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatArray | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | /** |
3 | * @author Yaron Koren |
4 | * @ingroup Cargo |
5 | */ |
6 | |
7 | class CargoDisplayFormat { |
8 | |
9 | /** @var OutputPage|ParserOutput */ |
10 | protected $mOutput; |
11 | /** @var Parser|null */ |
12 | protected $mParser; |
13 | |
14 | /** |
15 | * @param OutputPage|ParserOutput $output |
16 | * @param Parser|null $parser |
17 | */ |
18 | public function __construct( $output, $parser = null ) { |
19 | $this->mOutput = $output; |
20 | $this->mParser = $parser; |
21 | } |
22 | |
23 | public static function allowedParameters() { |
24 | return []; |
25 | } |
26 | |
27 | public static function isDeferred() { |
28 | return false; |
29 | } |
30 | |
31 | /** |
32 | * Apply a Cargo format to a 2D row-based array of values of any origin. |
33 | * |
34 | * @author Alexander Mashin |
35 | * @param Parser $parser |
36 | * @param array $values A 2D row-based array of values. |
37 | * @param array $mappings A mapping from ED to Cargo variables. |
38 | * @param array $params An array of params for {{#cargo_query:}}. |
39 | * |
40 | * @return array [ string, 'noparse' => bool, 'isHTML' => bool ]. |
41 | */ |
42 | public static function formatArray( Parser $parser, array $values, array $mappings, array $params ): array { |
43 | $format = $params['format'] ?? 'list'; |
44 | $classes = CargoQueryDisplayer::getAllFormatClasses(); |
45 | /** @var CargoDisplayFormat $class */ |
46 | $class = $classes[ $format ] ?? CargoListFormat::class; |
47 | $formatter = new $class( $parser->getOutput(), $parser ); |
48 | |
49 | // This cannot yet be called for "deferred" formats, where the |
50 | // processing is done via JavaScript and not PHP - in theory, |
51 | // it could be done, but it would require being able to pass |
52 | // in a data set to the JavaScript, instead of query |
53 | // information. |
54 | if ( $formatter->isDeferred() ) { |
55 | throw new MWException( "formatArray() cannot be called for the $format format because it is a \"deferred format\"." ); |
56 | } |
57 | |
58 | $query_displayer = new CargoQueryDisplayer(); |
59 | $field_descriptions = []; |
60 | foreach ( $mappings as $local => $external ) { |
61 | $description = new CargoFieldDescription(); |
62 | $description->mType = 'String'; |
63 | $field_descriptions[$local] = $description; |
64 | } |
65 | $query_displayer->mFieldDescriptions = $field_descriptions; |
66 | $query_displayer->mFieldTables = []; |
67 | |
68 | $html = $formatter->display( |
69 | $values, |
70 | $query_displayer->getFormattedQueryResults( $values ), |
71 | $query_displayer->mFieldDescriptions, |
72 | $params |
73 | ); |
74 | $no_html = $params['no html'] ?? false; |
75 | return !$no_html && $format !== 'template' |
76 | ? [ $html, 'noparse' => true, 'isHTML' => true ] |
77 | : [ $html, 'noparse' => false ]; |
78 | } |
79 | } |