Pop\Data\Type\Yaml::decode PHP Метод

decode() публичный статический Метод

Decode the data into PHP.
public static decode ( string $data ) : mixed
$data string
Результат mixed
    public static function decode($data)
    {
        $eol = strpos($data, "-\r\n") !== false ? "-\r\n" : "-\n";
        $yaml = substr($data, strpos($data, $eol) + strlen($eol));
        $yamlAry = explode($eol, $yaml);
        $nodes = array();
        $i = 1;
        foreach ($yamlAry as $value) {
            $objs = explode("\n", trim($value));
            $ob = array();
            foreach ($objs as $v) {
                $vAry = explode(':', $v);
                $val = trim($vAry[1]);
                $val = substr($val, 1, -1);
                $ob[trim($vAry[0])] = stripslashes($val);
            }
            $nodes['row_' . $i] = $ob;
            $i++;
        }
        return $nodes;
    }

Usage Example

Пример #1
0
    public function testDecode()
    {
        $yaml = <<<YAML
%YAML 1.1
---
 username: "******"
 password: "******"
 email: "*****@*****.**"
-
 username: "******"
 password: "******"
 email: "*****@*****.**"
YAML;
        $y = Yaml::decode($yaml);
        $this->assertEquals(2, count($y));
        $this->assertEquals('Test1', $y['row_1']['username']);
    }