Total Complexity | 38 |
Total Lines | 127 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
10 | class Dumper //extends AnotherClass |
||
11 | { |
||
12 | private const indent = 2; |
||
13 | private const width = 120; |
||
14 | private const options = 00000; |
||
15 | |||
16 | private static $result; |
||
17 | private static $options; |
||
18 | //options |
||
19 | public const EXPAND_SHORT = 00001; |
||
20 | public const SERIALIZE_CUSTOM_OBJECTS = 00010; |
||
21 | |||
22 | public function __construct(int $options = null) |
||
23 | { |
||
24 | if (is_int($options)) self::$options = $options; |
||
1 ignored issue
–
show
|
|||
25 | } |
||
26 | |||
27 | public static function toString($dataType, int $options):string |
||
28 | { |
||
29 | if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
||
1 ignored issue
–
show
|
|||
30 | self::$options = is_int($options) ? $options : self::$options; |
||
31 | self::$result = new DLL; |
||
32 | self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE); |
||
33 | if ($dataType instanceof YamlObject) { |
||
34 | self::dumpYamlObject($dataType); |
||
35 | } elseif (is_array($dataType) && $dataType[0] instanceof YamlObject) { |
||
36 | array_map([self::class, 'dumpYamlObject'], $dataType); |
||
37 | } else { |
||
38 | self::dump($dataType, 0); |
||
39 | } |
||
40 | $out = ''; |
||
41 | foreach (self::$result as $value) { |
||
42 | $out .= $value."\n"; |
||
43 | } |
||
44 | self::$result = null; |
||
45 | return $out; |
||
46 | } |
||
47 | |||
48 | public static function toFile(string $filePath, $dataType, int $options):bool |
||
49 | { |
||
50 | return !is_bool(file_put_contents($filePath, self::toString($dataType, $options))); |
||
51 | } |
||
52 | |||
53 | private static function dump($dataType, int $indent) |
||
54 | { |
||
55 | if (is_scalar($dataType)) { |
||
56 | switch (gettype($dataType)) { |
||
57 | case 'boolean': return $dataType ? 'true' : 'false';break; |
||
1 ignored issue
–
show
|
|||
58 | case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
||
2 ignored issues
–
show
|
|||
59 | case 'double': if (is_nan($dataType)) return '.nan'; |
||
2 ignored issues
–
show
|
|||
60 | default: |
||
1 ignored issue
–
show
|
|||
61 | return $dataType; |
||
62 | } |
||
63 | } elseif (is_object($dataType)) { |
||
64 | self::dumpObject($dataType, $indent); |
||
65 | } elseif (is_array($dataType)) { |
||
66 | self::dumpSequence($dataType, $indent); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | private static function dumpYamlObject(YamlObject $dataType) |
||
74 | // self::insertComments($dataType->getComment()); |
||
75 | //TODO: $references = $dataType->getAllReferences(); |
||
76 | } |
||
77 | |||
78 | private static function add($value, $indent) |
||
83 | } |
||
84 | } |
||
85 | |||
86 | private static function dumpSequence(array $array, int $indent):void |
||
87 | { |
||
88 | $refKeys = range(0, count($array)); |
||
89 | foreach ($array as $key => $item) { |
||
90 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
||
91 | if (is_scalar($item)) { |
||
92 | self::add($lineStart.$item, $indent ); |
||
93 | } else { |
||
94 | self::add($lineStart, $indent ); |
||
95 | self::dump($item, $indent + self::indent); |
||
96 | } |
||
97 | next($refKeys); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | private static function insertComments(array $commentsArray):void |
||
102 | { |
||
103 | foreach ($commentsArray as $lineNb => $comment) { |
||
104 | self::$result->add($lineNb, $comment); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | private function dumpObject(object $object, int $indent) |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
135 | public static function dumpCompact(Compact $object, int $indent) |
||
141 |