Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
TemplateInfo
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 getDataMw
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\NodeData;
5
6use stdClass;
7
8class TemplateInfo {
9    /**
10     * The target wikitext
11     * @var string|null
12     */
13    public $targetWt;
14
15    /**
16     * The parser function name
17     * @var string|null
18     */
19    public $func;
20
21    /**
22     * The URL of the target
23     * @var string|null
24     */
25    public $href;
26
27    /**
28     * Param infos indexed by key (ParamInfo->k)
29     * @var ParamInfo[]
30     */
31    public $paramInfos;
32
33    /**
34     * Get JSON-serializable data for data-mw.parts.template
35     *
36     * @param int $index The index into data-parsoid.pi
37     * @return stdClass
38     */
39    public function getDataMw( int $index ): stdClass {
40        $target = [ 'wt' => $this->targetWt ];
41        if ( $this->func !== null ) {
42            $target['function'] = $this->func;
43        }
44        if ( $this->href !== null ) {
45            $target['href'] = $this->href;
46        }
47        $params = [];
48        foreach ( $this->paramInfos as $info ) {
49            $param = [
50                'wt' => $info->valueWt,
51            ];
52            if ( $info->html !== null ) {
53                $param['html'] = $info->html;
54            }
55            if ( $info->keyWt !== null ) {
56                $param['key']['wt'] = $info->keyWt;
57            }
58            $params[$info->k] = $param;
59        }
60
61        // Cast everything to object to satisfy pre-serialization consumers of data-mw
62        return (object)[
63            'target' => (object)$target,
64            // params also needs to be cast to object in case all the keys are numeric
65            'params' => (object)$params,
66            'i' => $index
67        ];
68    }
69}