<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dallgoot</title>
    <description>The latest articles on DEV Community by dallgoot (@dallgoot).</description>
    <link>https://dev.to/dallgoot</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F35476%2F358d2ce2-0a42-4196-832b-d61fcb70157a.png</url>
      <title>DEV Community: dallgoot</title>
      <link>https://dev.to/dallgoot</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dallgoot"/>
    <language>en</language>
    <item>
      <title>a PHP library for YAML</title>
      <dc:creator>dallgoot</dc:creator>
      <pubDate>Thu, 19 Dec 2019 09:03:26 +0000</pubDate>
      <link>https://dev.to/dallgoot/a-php-library-for-yaml-14gf</link>
      <guid>https://dev.to/dallgoot/a-php-library-for-yaml-14gf</guid>
      <description>&lt;h2&gt;
  
  
  What for ?
&lt;/h2&gt;

&lt;p&gt;This is to read YAML content or to generate YAML content corresponding to PHP values.&lt;br&gt;
If you don't know what YAML is I suggest this &lt;a href="https://en.wikipedia.org/wiki/YAML" rel="noopener noreferrer"&gt;Wikipedia Article&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  But there are already multiple libraries for YAML in PHP
&lt;/h2&gt;

&lt;p&gt;Yes but some are not maintained anymore or they lack support of all features of YAML like :&lt;/p&gt;
&lt;h3&gt;
  
  
  multiple documents in a content
&lt;/h3&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="s"&gt;document &lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;

&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="s"&gt;document &lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Dallgoot/YAML will return an array which contains 1 YamlObject for each document.&lt;br&gt;
When only one document is present only a YamlObject is returned for simplicity.&lt;/p&gt;
&lt;h3&gt;
  
  
  complex mappings
&lt;/h3&gt;

&lt;p&gt;Mappings are usually used with simple string values as keys like&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;simple string &lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;some&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;value"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;but YAML also supports complex values for keys like&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;?&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;an&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;array&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;strings&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;still&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;some&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;simple&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;value"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Dallgoot/YAML provides support of complex keys by json-ing them.&lt;br&gt;
These values end as a JSON string which is suitable to be used as properties of YamlObject (or standard PHP object).&lt;br&gt;
They cannot be adressed specifically unless you already know their JSON evaluation but the object can be iterated by a simple &lt;em&gt;foreach&lt;/em&gt; loop with "$key=&amp;gt;$value" parameters and then json_decode-d to use them as complex types.&lt;/p&gt;
&lt;h3&gt;
  
  
  anchors (think as pointers or variables or references) are not really supported or they're duplicated content.
&lt;/h3&gt;

&lt;p&gt;Take a look at this YAML :&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;some key &lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nl"&gt;&amp;amp;my_anchor&lt;/span&gt; &lt;span class="s"&gt;some string value&lt;/span&gt;
&lt;span class="na"&gt;another key &lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;*my_anchor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In Dallgoot/YAML anchors are parsed as variable reference which means that "some key" and "another key" points to the same value "some string value". Changing the value on the YAMLObject changes all values from pointers to it.&lt;/p&gt;
&lt;h3&gt;
  
  
  comments are discarded
&lt;/h3&gt;

&lt;p&gt;Unless i'm mistaken comments in YAML are usually discarded. In Dallgoot/YAML they are preserved in the YamlObject by default and can be accessed by line number or the list (PHP array) can be looped through.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why an article on Dev.to
&lt;/h2&gt;

&lt;p&gt;Developing a library is a bold exercice. It is assuming that it will be of use for many people. In that regard, it should not only do the job that it is supposed to but do it well and concerning parsers do it for &lt;em&gt;every&lt;/em&gt; file.&lt;/p&gt;

&lt;p&gt;At the time of writing this article the library has 374 tests and 838 assertions and has been tested on PHP versions starting from 7.1.10 to 7.4.0 however this is not enough and hence the "Beta" warning still left.&lt;/p&gt;

&lt;p&gt;It needs YOU : that you test your YAML files against it and open a Github issue (or just leave a comment here) if you encounter any problems or malfunctions.&lt;/p&gt;

&lt;p&gt;That's where the help of the Dev.to community will be greatly appreciated ;)&lt;/p&gt;

&lt;p&gt;For installation, concrete use examples, in depth documentation or simply code discovery you'll find what you need in the Github Repo.&lt;br&gt;
Feel free to comment here if you need help or something isn't clear enough.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/dallgoot" rel="noopener noreferrer"&gt;
        dallgoot
      &lt;/a&gt; / &lt;a href="https://github.com/dallgoot/yaml" rel="noopener noreferrer"&gt;
        yaml
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      PHP library to load and parse YAML file to PHP datatypes equivalent
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;Thank you for your attention and comments, critics, suggestions are welcomed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/m23aurqwP" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbmc-cdn.nyc3.digitaloceanspaces.com%2FBMC-button-images%2Fcustom_images%2Forange_img.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>yaml</category>
      <category>parser</category>
      <category>phplibrary</category>
    </item>
  </channel>
</rss>
