|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHP-JSON-FIXER package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
|
7
|
|
|
* <https://github.com/adhocore> |
|
8
|
|
|
* |
|
9
|
|
|
* Licensed under MIT license. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ahc\Json; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Attempts to fix truncated JSON by padding contextual counterparts at the end. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Jitendra Adhikari <[email protected]> |
|
18
|
|
|
* @license MIT |
|
19
|
|
|
* |
|
20
|
|
|
* @internal |
|
21
|
|
|
* |
|
22
|
|
|
* @link https://github.com/adhocore/php-json-fixer |
|
23
|
|
|
*/ |
|
24
|
|
|
trait PadsJson |
|
25
|
|
|
{ |
|
26
|
|
|
public function pad($tmpJson) |
|
27
|
|
|
{ |
|
28
|
|
|
if (!$this->inStr) { |
|
29
|
|
|
$tmpJson = \rtrim($tmpJson, ','); |
|
30
|
|
|
while ($this->lastToken() === ',') { |
|
|
|
|
|
|
31
|
|
|
$this->popToken(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$tmpJson = $this->padLiteral($tmpJson); |
|
36
|
|
|
$tmpJson = $this->padObject($tmpJson); |
|
37
|
|
|
|
|
38
|
|
|
return $this->padStack($tmpJson); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function padLiteral($tmpJson) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($this->inStr) { |
|
44
|
|
|
return $tmpJson; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$match = \preg_match('/(tr?u?e?|fa?l?s?e?|nu?l?l?)$/', $tmpJson, $matches); |
|
48
|
|
|
|
|
49
|
|
|
if (!$match || null === $literal = $this->maybeLiteral($matches[1])) { |
|
|
|
|
|
|
50
|
|
|
return $tmpJson; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return \substr($tmpJson, 0, 0 - \strlen($matches[1])) . $literal; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function padStack($tmpJson) |
|
57
|
|
|
{ |
|
58
|
|
|
foreach (\array_reverse($this->stack, true) as $index => $token) { |
|
59
|
|
|
if (isset($this->pairs[$token])) { |
|
60
|
|
|
$tmpJson .= $this->pairs[$token]; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $tmpJson; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function padObject($tmpJson) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$this->objectNeedsPadding($tmpJson)) { |
|
70
|
|
|
return $tmpJson; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$part = \substr($tmpJson, $this->objectPos + 1); |
|
74
|
|
|
if (\preg_match('/(\s*\"[^"]+\"\s*:\s*[^,]+,?)+$/', $part, $matches)) { |
|
75
|
|
|
return $tmpJson; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($this->inStr) { |
|
79
|
|
|
$tmpJson .= '"'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$tmpJson = $this->padIf($tmpJson, ':'); |
|
83
|
|
|
$tmpJson = $tmpJson . $this->missingValue; |
|
84
|
|
|
|
|
85
|
|
|
if ($this->lastToken() === '"') { |
|
86
|
|
|
$this->popToken(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $tmpJson; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function objectNeedsPadding($tmpJson) |
|
93
|
|
|
{ |
|
94
|
|
|
$last = \substr($tmpJson, -1); |
|
95
|
|
|
$empty = $last === '{' && !$this->inStr; |
|
96
|
|
|
|
|
97
|
|
|
return !$empty && $this->arrayPos < $this->objectPos; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function padString($string) |
|
101
|
|
|
{ |
|
102
|
|
|
$last = \substr($string, -1); |
|
103
|
|
|
$last2 = \substr($string, -2); |
|
104
|
|
|
|
|
105
|
|
|
if ($last2 === '\"' || $last !== '"') { |
|
106
|
|
|
return $string . '"'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// @codeCoverageIgnoreStart |
|
110
|
|
|
return null; |
|
111
|
|
|
// @codeCoverageIgnoreEnd |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function padIf($string, $substr) |
|
115
|
|
|
{ |
|
116
|
|
|
if (\substr($string, 0 - \strlen($substr)) !== $substr) { |
|
117
|
|
|
return $string . $substr; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $string; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|