Pop\Data\Type\Xml::encode PHP Метод

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

Encode the data into its native format.
public static encode ( mixed $data, string $table = null, boolean $pma = false ) : string
$data mixed
$table string
$pma boolean
Результат string
    public static function encode($data, $table = null, $pma = false)
    {
        $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?" . ">\n<data>\n";
        if ($pma) {
            foreach ($data as $key => $ary) {
                $table = null === $table ? substr($key, 0, strrpos($key, '_')) : $table;
                $xml .= "    <table name=\"" . $table . "\">\n";
                foreach ($ary as $k => $v) {
                    $xml .= "        <column name=\"" . $k . "\">" . $v . "</column>\n";
                }
                $xml .= "    </table>\n";
            }
        } else {
            foreach ($data as $key => $ary) {
                $table = null === $table ? substr($key, 0, strrpos($key, '_')) : $table;
                if (empty($table)) {
                    $table = 'row';
                }
                $xml .= "    <" . $table . ">\n";
                foreach ($ary as $k => $v) {
                    $xml .= "        <" . $k . ">" . $v . "</" . $k . ">\n";
                }
                $xml .= "    </" . $table . ">\n";
            }
        }
        $xml .= "</data>\n";
        return $xml;
    }

Usage Example

Пример #1
0
 public function testEncode()
 {
     $ary = array(array('Name' => 'Test1', 'Num' => 1), array('Name' => 'Test2', 'Num' => 2));
     $x = Xml::encode($ary);
     $this->assertContains('<Name>Test1</Name>', $x);
     $x = Xml::encode($ary, 'users', true);
     $this->assertContains('<table name="users">', $x);
 }