Pop\Data\Type\Sql::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(";
        $fields = substr($data, strpos($data, '(') + 1);
        $fields = substr($fields, 0, strpos($fields, ')'));
        $search = array(', ', '`', '"', "'", '[', ']');
        $replace = array(',', '', '', "", '', '');
        $fields = str_replace($search, $replace, $fields);
        $fieldsAry = explode(',', $fields);
        $valuesAry = array();
        $values = substr($data, strpos($data, "\n") + 1);
        $insertAry = explode('INSERT', $values);
        foreach ($insertAry as $value) {
            $value = trim($value);
            if (stripos($value, 'INTO') !== false) {
                $value = trim(substr($value, stripos($value, 'VALUES') + 6));
            }
            $valuesAry = array_merge($valuesAry, explode($eol, $value));
        }
        $valAry = array();
        foreach ($valuesAry as $value) {
            if (substr($value, 0, 1) == '(') {
                $value = substr($value, 1);
            }
            if (substr($value, -2) == ');') {
                $value = substr($value, 0, -2);
            }
            $valAry[] = $value;
        }
        $newAry = array();
        $j = 1;
        foreach ($valAry as $val) {
            $ary = array();
            for ($i = 0; $i < count($fieldsAry); $i++) {
                if (substr($val, 0, 1) == "'") {
                    if (strpos($val, ',') !== false) {
                        $v = substr($val, 1, strpos($val, "',"));
                        $l = strlen($v) + 2;
                    } else {
                        $v = $val;
                        $l = strlen($v);
                    }
                } else {
                    if (strpos($val, ',') !== false) {
                        $v = substr($val, 0, strpos($val, ","));
                        $l = strlen($v) + 1;
                    } else {
                        $v = $val;
                        $l = strlen($v);
                    }
                }
                if (substr($v, -1) == "'") {
                    $v = substr($v, 0, -1);
                }
                if (substr($v, 0, 1) == "'") {
                    $v = substr($v, 1);
                }
                $ary[$fieldsAry[$i]] = stripslashes($v);
                $val = substr($val, $l);
                $val = ltrim($val);
            }
            $newAry['row_' . $j] = $ary;
            $j++;
        }
        return $newAry;
    }

Usage Example

Пример #1
0
 public function testDecode()
 {
     $f = new File(__DIR__ . '/../tmp/test.sql');
     $s = Sql::decode($f->read());
     $keys = array('id', 'username', 'password', 'email', 'access');
     $this->assertEquals(9, count($s));
     $this->assertEquals($keys, array_keys($s['row_1']));
 }