Learn how to use YAML operation extension in PHP

YAML, to be honest, I don't use it much. When learning the content related to automated testing, I came across the use o...
Convert PHP data to YAML
Convert YAML to PHP array
Callback function processing Label
summary

YAML, to be honest, I don't use it much. When learning the content related to automated testing, I came across the use of this configuration file to configure the continuous integration operation of Travis CI. Of course, it was mainly learning at that time. Although not much contact, but also know that this configuration format has basically become the mainstream. Therefore, I won't explain the specific YAML related content here. Students who don't know much can consult some relevant documents by themselves.

What we are going to learn today is an extension for parsing and converting YAML format in PHP. There is nothing special to explain in the installation process. Just install it like other extensions. However, this extension requires a libyaml devel, which cannot be installed directly through yum or dnf in CentOS. We can find the download address in the link at the end of the article.

Convert PHP data to YAML

For converting PHP data into YAML, it is actually similar to JSON related operations, converting arrays into strings in YAML format.

$addr = array( "given" => "Chris", "family"=> "Dumars", "address"=> array( "lines"=> "458 Walkman Dr. Suite #292", "city"=> "Royal Oak", "state"=> "MI", "postal"=> 48046, ), ); $invoice = array ( "invoice"=> 34843, "date"=> 980208000, "bill-to"=> $addr, "ship-to"=> $addr, "product"=> array( array( "sku"=> "BL394D", "quantity"=> 4, "description"=> "Basketball", "price"=> 450, ), array( "sku"=> "BL4438H", "quantity"=> 1, "description"=> "Super Hoop", "price"=> 2392, ), ), "tax"=> 251.42, "total"=> 4443.52, "comments"=> "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.", ); $yamlString = yaml_emit($invoice); var_dump($yamlString); // string(624) "--- // invoice: 34843 // date: 980208000 // bill-to: // given: Chris // family: Dumars // address: // lines: |- // 458 Walkman Dr. // Suite #292 // city: Royal Oak // state: MI // postal: 48046 // ship-to: // given: Chris // family: Dumars // address: // lines: |- // 458 Walkman Dr. // Suite #292 // city: Royal Oak // state: MI // postal: 48046 // product: // - sku: BL394D // quantity: 4 // description: "\u7BEE\u7403" // price: 450 // - sku: BL4438H // quantity: 1 // description: Super Hoop // price: 2392 // tax: 251.42 // total: 4443.52 // comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338. // ... // "

You can see YAML_ The result of the emit () function transformation is a very standard YAML format. It starts with --, and ends with. However, you will find that there are many tutorials or. yml files in the framework that do not have these symbols. It is learned from the official documents that these symbols are written according to the recommendations, and our extension complies with the recommendations very much, that is, the conversion results are very standard.

In addition, we added Chinese content to the test code. You can see that Chinese is encoded during direct conversion. Just like JSON operation, in the extended function of YAML, we can also specify the encoding format to display Chinese as it is.

var_dump(yaml_emit($invoice, YAML_UTF8_ENCODING)); // string(616) "--- // .................. // description: basketball // .................. // ... // "

Convert YAML to PHP array

Yes, it is also similar to JSON operation, which reverses the string format content in YAML format back to the PHP data content.

var_dump(yaml_parse($yamlString)); // array(8) { // ["invoice"]=> // int(34843) // ["date"]=> // int(980208000) // ["bill-to"]=> // array(3) { // ["given"]=> // string(5) "Chris" // ["family"]=> // string(6) "Dumars" // .................. // ..................

Also very simply a yaml_parse() function. In addition to directly manipulating strings, we can also directly extract the contents of the file for conversion, including yaml above_ The emit () function has a similar function of writing the result directly to a file.

var_dump(yaml_parse_file('styleci.yml')); // array(3) { // ["php"]=> // array(3) { // ["preset"]=> // string(7) "laravel" // ["disabled"]=> // array(1) { // [0]=> // string(10) "unused_use" // } // ["finder"]=> // array(1) { // ["not-name"]=> // array(2) { // [0]=> // string(9) "index.php" // [1]=> // string(10) "server.php" // } // } // } // ["js"]=> // array(1) { // ["finder"]=> // array(1) { // ["not-name"]=> // array(1) { // [0]=> // string(14) "webpack.mix.js" // } // } // } // ["css"]=> // bool(true) // }

The file we tested is the. styleci.yml file that comes with Laravel. We are not required to install this YAML extension in the Laravel framework. It seems that the framework itself has a tool to read and convert this YAML format. Let's talk about this last. And yaml_parse_file() similarly, yaml_emit_file() directly converts PHP data into YAML format and then writes it directly to a file. You can test it yourself.

Callback function processing Label

Whether it's yaml_emit() or yaml_parse() supports a callback parameter operation. Let's look at an example first.

// php: // preset: !laravel laravel // disabled: // .................. // .................. function callback($value){ return str_replace('laravel', 'new version laravel8', $value); } $ndocs = 0; var_dump(yaml_parse_file('styleci.yml', 0, $ndocs, ['!laravel'=>'callback'])); // array(3) { // ["php"]=> // array(3) { // ["preset"]=> // string(20) "new version laravel8" // ["disabled"]=> // array(1) { // ........................ // ........................

What does that mean?! This way of writing laravel can be regarded as a label format in YAML. The function of this callback is to use what callback function to handle when encountering such tags. For example, in our original document! What follows laravel is laravel. In the callback function, we replace the content with new version laravel8, so the final output is that the content of the preset field becomes new version laravel8. Of course, more detailed content and syntax still need us to deeply understand the syntax of YAML format, so I won't say much here. After all, I don't have much contact with it.

summary

There is not much content for this extension, and even if I need to operate the yaml format configuration file in the real business environment, I don't think I will use it. Why? Of course, there are many yaml processing components in Composer for us to use. There is no need to change the PHP environment on the server by expanding compilation and installation. If you use it yourself, you can find many components in packagist.org. If you use Laravel, its bottom layer actually uses yaml processing components in the symfony framework. Directly use composer require symfony/yaml to add this component to your small project. Refer to this document for details:

https://symfony.com/doc/current/components/yaml.html

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/2021/01/source/10. Learn how to use YAML operation extension in PHP. PHP

Reference documents:

https://www.php.net/manual/zh/book.yaml.php

http://www.rpmfind.net/linux/rpm2html/search.php?query=libyaml-devel(x86-64))

http://bd808.com/pecl-file_formats-yaml/

23 November 2021, 07:00 | Views: 4179

Add new comment

For adding a comment, please log in
or create account

0 comments