如何使用PHP-YAML的自定义标签回调?
根据官方文档,有一种方法可以提供回调对于自定义 YAML 标签:
mixed yaml_parse ( string $input [, int $pos = 0 [, int &$ndocs [, array $callbacks ]]] )
回调
YAML 节点的内容处理程序。 YAML 标签的关联数组 =>回调映射。
然而,似乎没有关于这个主题的其他文档,即使在扩展源中也是如此!
我创建了这个脚本作为测试:
<?php
$yaml =<<<YAML
---
prop: !custom val
YAML;
print_r(yaml_parse($yaml,0,$n,array(
YAML_STR_TAG => function () {
echo "YAML_STR_TAG\n";
$args = func_get_args();
print_r($args);
return 'x';
},
'!custom' => function () {
echo "!custom\n";
$args = func_get_args();
print_r($args);
return 'y';
}
)));
我得到了这个输出:
$ php yaml.php
YAML_STR_TAG
Array
(
[0] => prop
[1] => tag:yaml.org,2002:str
[2] => 1
)
!custom
Array
(
[0] => val
[1] => !custom
[2] => 1
)
Array
(
[x] => y
)
从那里我可以看出几件事:
- 回调查找中使用的键是 PHP-YAML 的 预定义常量 或 YAML 源中使用的自定义标记,包括感叹号
- 中的每个键和地图被“标记”并且传递给匹配的回调,可能是因为根据 YAML 规范,密钥也可以是任何有效类型。
- 三个参数传递给回调:标签的“主题”、标签本身和一些数字,可能对应于
YAML_*_SCALAR_STYLE
常量。 - 回调的返回值替换了标记的数据结构
任何人都可以确认此函数的预期行为吗?
According to the official documentation, there is a way to provide callbacks for custom YAML tags:
mixed yaml_parse ( string $input [, int $pos = 0 [, int &$ndocs [, array $callbacks ]]] )
callbacks
Content handlers for YAML nodes. Associative array of YAML tag => callback mappings.
However, there seems to be no other documentation on the subject, even in the extension source!
I created this script as a test:
<?php
$yaml =<<<YAML
---
prop: !custom val
YAML;
print_r(yaml_parse($yaml,0,$n,array(
YAML_STR_TAG => function () {
echo "YAML_STR_TAG\n";
$args = func_get_args();
print_r($args);
return 'x';
},
'!custom' => function () {
echo "!custom\n";
$args = func_get_args();
print_r($args);
return 'y';
}
)));
And I got this output:
$ php yaml.php
YAML_STR_TAG
Array
(
[0] => prop
[1] => tag:yaml.org,2002:str
[2] => 1
)
!custom
Array
(
[0] => val
[1] => !custom
[2] => 1
)
Array
(
[x] => y
)
From that I can tell several things:
- The key used in the callback lookup is either one of PHP-YAML's predefined constants or the custom tag used in the YAML source, including the exclamation point
- Each key and value in a map gets "tagged" and passed to the matching callback, probably because according to the YAML spec the key can be any valid type as well.
- Three arguments are passed to the callback: the "subject" of the tag, the tag itself, and some number, probably corresponding to a
YAML_*_SCALAR_STYLE
constant. - The return value of callbacks replaces the tagged data structure
Can anyone confirm the expected behavior of this function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量研究和测试,我找到了一些答案。
如扩展程序的单元测试,每个回调采用三个参数:
$data
- 标记的数据,已解析$tag
- 标签名称,根据官方 YAML 标签规范< /a>:如果没有定义标签前缀,
!custom
将扩展为!custom
!custom
扩展为prefixcustom
,其中prefix
由文档元数据%TAG 定义!前缀
。请注意,没有前导感叹号!!preset
扩展为解析器定义的内部类型。请参阅YAML_*_TAG
常量< /里>!<逐字标记>扩展为
verbatim-tag`。请注意,没有前导感叹号。$style
- 使用的标量样式。请参阅YAML_*_SCALAR_STYLE
常量回调应该返回一个混合值供解析器发出。
After much research and testing, I have found some answers.
As found in the extension's unit tests, each callback takes three arguments:
$data
- The tagged data, already parsed$tag
- The tag name, expanded according to the offical YAML tag specs:!custom
expands to!custom
if no tag prefix is defined!custom
expands toprefixcustom
, whereprefix
is defined by the document meta-data%TAG ! prefix
. Note there is not a leading exclamation mark!!preset
expands to the parser-defined internal type. See theYAML_*_TAG
constants!<verbatim-tag> expands to
verbatim-tag`. Note there is not a leading exclamation mark.$style
- The scalar style used. See theYAML_*_SCALAR_STYLE
constantsThe callback should return a mixed value for the parser to emit.