| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\NodeData; |
| 5 |
|
| 6 |
use stdClass; |
| 7 |
use Wikimedia\JsonCodec\Hint; |
| 8 |
use Wikimedia\JsonCodec\JsonCodecable; |
| 9 |
use Wikimedia\JsonCodec\JsonCodecableTrait; |
| 10 |
|
| 11 |
class TemplateInfo implements JsonCodecable { |
| 12 |
use JsonCodecableTrait; |
| 13 |
|
| 14 |
/** |
| 15 |
* The target wikitext |
| 16 |
*/ |
| 17 |
public ?string $targetWt = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* The parser function name |
| 21 |
*/ |
| 22 |
public ?string $func = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* The URL of the target |
| 26 |
*/ |
| 27 |
public ?string $href = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Param infos indexed by key (ParamInfo->k) |
| 31 |
* @var list<ParamInfo> |
| 32 |
*/ |
| 33 |
public array $paramInfos = []; |
| 34 |
|
| 35 |
/** |
| 36 |
* The type of template (template, templatearg, parserfunction). |
| 37 |
* @note For backward-compatibility reasons, this property is |
| 38 |
* not serialized/deserialized. |
| 39 |
* @var 'template'|'templatearg'|'parserfunction'|null |
| 40 |
*/ |
| 41 |
public ?string $type = null; |
| 42 |
|
| 43 |
/** |
| 44 |
* The index into data-parsoid.pi |
| 45 |
*/ |
| 46 |
public ?int $i = null; |
| 47 |
|
| 48 |
/** @inheritDoc */ |
| 49 |
public static function newFromJsonArray( array $json ): TemplateInfo { |
| 50 |
$ti = new TemplateInfo; |
| 51 |
$ti->targetWt = $json['target']['wt'] ?? null; |
| 52 |
$ti->href = $json['target']['href'] ?? null; |
| 53 |
$ti->func = $json['target']['function'] ?? null; |
| 54 |
$ti->paramInfos = []; |
| 55 |
$params = (array)( $json['params'] ?? null ); |
| 56 |
foreach ( $params as $k => $v ) { |
| 57 |
// Converting $params to an array can turn the keys into ints, |
| 58 |
// so we need to explicitly cast them back to string. |
| 59 |
$info = new ParamInfo( (string)$k ); |
| 60 |
$info->valueWt = $v->wt ?? null; |
| 61 |
$info->html = $v->html ?? null; |
| 62 |
$info->keyWt = $v->key->wt ?? null; |
| 63 |
$ti->paramInfos[] = $info; |
| 64 |
} |
| 65 |
$ti->i = $json['i'] ?? null; |
| 66 |
return $ti; |
| 67 |
} |
| 68 |
|
| 69 |
/** @inheritDoc */ |
| 70 |
public static function jsonClassHintFor( string $keyname ) { |
| 71 |
static $hints = null; |
| 72 |
if ( $hints === null ) { |
| 73 |
$hints = [ |
| 74 |
// The most deeply nested stdClass structure is "wt" inside |
| 75 |
// "key" inside a parameter: |
| 76 |
// "params":{"1":{"key":{"wt":"..."}}} |
| 77 |
'params' => Hint::build( |
| 78 |
stdClass::class, Hint::ALLOW_OBJECT, |
| 79 |
Hint::STDCLASS, Hint::ALLOW_OBJECT, |
| 80 |
Hint::STDCLASS, Hint::ALLOW_OBJECT |
| 81 |
), |
| 82 |
]; |
| 83 |
} |
| 84 |
return $hints[$keyname] ?? null; |
| 85 |
} |
| 86 |
|
| 87 |
/** @inheritDoc */ |
| 88 |
public function toJsonArray(): array { |
| 89 |
// This is a complicated serialization, but necessary for |
| 90 |
// backward compatibility with existing data-mw |
| 91 |
$target = [ 'wt' => $this->targetWt ]; |
| 92 |
if ( $this->func !== null ) { |
| 93 |
$target['function'] = $this->func; |
| 94 |
} |
| 95 |
if ( $this->href !== null ) { |
| 96 |
$target['href'] = $this->href; |
| 97 |
} |
| 98 |
$params = []; |
| 99 |
foreach ( $this->paramInfos as $info ) { |
| 100 |
// Non-standard serialization of ParamInfo, alas. |
| 101 |
$param = [ |
| 102 |
'wt' => $info->valueWt, |
| 103 |
]; |
| 104 |
if ( $info->html !== null ) { |
| 105 |
$param['html'] = $info->html; |
| 106 |
} |
| 107 |
if ( $info->keyWt !== null ) { |
| 108 |
$param['key'] = (object)[ |
| 109 |
'wt' => $info->keyWt, |
| 110 |
]; |
| 111 |
} |
| 112 |
$params[$info->k] = (object)$param; |
| 113 |
} |
| 114 |
return [ |
| 115 |
'target' => $target, |
| 116 |
'params' => (object)$params, |
| 117 |
'i' => $this->i, |
| 118 |
]; |
| 119 |
} |
| 120 |
} |