YAML 데이터 직렬화 Parse callbacks

구문 분석 callables은 등록된 YAML 태그가 발견될 때 yaml_parse(), yaml_parse_file() 또는 yaml_parse_url() 함수에 의해 호출됩니다. 콜백에는 태그가 지정된 엔티티의 값, 태그 및 스칼라 엔티티 스타일을 나타내는 플래그가 전달됩니다. 콜백은 YAML 파서가 이 엔터티에 대해 내보내야 하는 데이터를 반환해야 합니다.

예제 #1 Parse callback example

                  
<?php
/**
 * Parsing callback for yaml tag.
 * @param mixed $value Data from yaml file
 * @param string $tag Tag that triggered callback
 * @param int $flags Scalar entity style (see YAML_*_SCALAR_STYLE)
 * @return mixed Value that YAML parser should emit for the given value
 */
function tag_callback ($value, $tag, $flags) {
  var_dump(func_get_args()); // debugging
  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"
}