如何使用PHP-YAML的自定义标签回调?

发布于 2024-11-17 08:19:57 字数 1422 浏览 2 评论 0原文

根据官方文档,有一种方法可以提供回调对于自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-11-24 08:19:57

经过大量研究和测试,我找到了一些答案。

如扩展程序的单元测试,每个回调采用三个参数:

常量回调应该返回一个混合值供解析器发出。

After much research and testing, I have found some answers.

As found in the extension's unit tests, each callback takes three arguments:

  • mixed $data - The tagged data, already parsed
  • string $tag - The tag name, expanded according to the offical YAML tag specs:
    • !custom expands to !custom if no tag prefix is defined
    • !custom expands to prefixcustom, where prefix 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 the YAML_*_TAG constants
    • !<verbatim-tag> expands toverbatim-tag`. Note there is not a leading exclamation mark.
  • integer $style - The scalar style used. See the YAML_*_SCALAR_STYLE constants

The callback should return a mixed value for the parser to emit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文