CakeFest 2024: The Official CakePHP Conference

パースコールバック

パースコールバック (callable) は、 yaml_parse()yaml_parse_file() そして yaml_parse_url() 関数が、登録済みの YAML タグに遭遇したときに起動します。 コールバックに渡されるのは、タグ付けされたエンティティの値とタグ、そしてスカラーエンティティの形式を表すフラグです。 このコールバックが返すデータを使って、YAML パーサがこのエンティティを発行することになります。

例1 パースコールバックの例

<?php
/**
* yaml タグのパース用コールバック。
* @param mixed $value yamlファイルからのデータ
* @param string $tag コールバックを起動したタグ
* @param int $flags スカラーエンティティの形式 (YAML_*_SCALAR_STYLE を参照)
* @return mixed YAML パーサが発行するための値
*/
function tag_callback ($value, $tag, $flags) {
var_dump(func_get_args()); // デバッグ用
return "Hello {$value}";
}

$yaml = <<<YAML
greeting: !example/hello World
YAML;

$result = yaml_parse($yaml, 0, $ndocs, array(
'!example/hello' => 'tag_callback',
));

var_dump($result);
?>

上の例の出力は、 たとえば以下のようになります。

array(3) {
  [0]=>
  string(5) "World"
  [1]=>
  string(14) "!example/hello"
  [2]=>
  int(1)
}
array(1) {
  ["greeting"]=>
  string(11) "Hello World"
}
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top