Passed
Push — master ( a18919...9a039e )
by 世昌
01:53
created

ArrayDump::export()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
namespace nebula\arrayobject;
3
4
/**
5
 * 数组导出
6
 */
7
class ArrayDump
8
{
9
    /**
10
     * 将数组美化导出成PHP代码
11
     *
12
     * @param $name 导出的数组名
0 ignored issues
show
Documentation Bug introduced by
The doc comment 导出的数组名 at position 0 could not be parsed: Unknown type name '导出的数组名' at position 0 in 导出的数组名.
Loading history...
13
     * @param $array 导出的数组
14
     * @return string
15
     */
16
    public static function dump(string $name, array $array):string
17
    {
18
        $name = '$'.ltrim($name, '$');
19
        $exstr = '<?php'.PHP_EOL.$name.'=array();'.PHP_EOL;
20
        $exstr .= self::arr2string($name, $array);
21
        $exstr .= 'return '.$name.';';
22
        return $exstr;
23
    }
24
25
    /**
26
     * 将数组导出到PHP文件
27
     *
28
     * @param string $path
29
     * @param string $name
30
     * @param array $array
31
     * @return int
32
     */
33
    public static function export(string $path, string $name, array $array)
34
    {
35
        return \file_put_contents($path, static::dump($name, $array));
36
    }
37
38
    protected static function arr2string($arrname, $array)
39
    {
40
        $exstr = '';
41
        foreach ($array as $key => $value) {
42
            $line = '';
43
            $current=$arrname."['".addslashes($key)."']";
44
            if (is_array($value)) {
45
                $line .= self::parserArraySub($current, $value);
46
            } else {
47
                $line =  $current;
48
                if (is_string($value)) {
49
                    $line .= "='".addslashes($value).'\';'.PHP_EOL;
50
                } elseif (is_bool($value)) {
51
                    $line .= '='.($value ? 'true' : 'false').';'.PHP_EOL;
52
                } elseif (is_null($value)) {
53
                    $line .= '=null;'.PHP_EOL;
54
                } else {
55
                    $line .= '='.$value.';'.PHP_EOL;
56
                }
57
            }
58
            $exstr .= $line;
59
        }
60
        return $exstr;
61
    }
62
63
    protected static function parserArraySub(string $parent, array $array)
64
    {
65
        $line = '';
66
        foreach ($array as $key => $value) {
67
            if (is_array($value)) {
68
                $subpar = $parent."['".$key."']";
69
                $line .= self::parserArraySub($subpar, $value);
70
            } else {
71
                $line .= $parent."['".$key."']";
72
                if (is_string($value)) {
73
                    $line .= "='".addslashes($value).'\';'.PHP_EOL;
74
                } elseif (is_bool($value)) {
75
                    $line .= '='.($value ? 'true' : 'false').';'.PHP_EOL;
76
                } elseif (is_null($value)) {
77
                    $line .= '=null;'.PHP_EOL;
78
                } else {
79
                    $line .= '='.$value.';'.PHP_EOL;
80
                }
81
            }
82
        }
83
        return $line;
84
    }
85
}
86