@@ -31,41 +31,40 @@ class RootedJsonData
31
31
*/
32
32
public function __construct (string $ json = "{} " , string $ schema = "{} " )
33
33
{
34
- $ decoded = json_decode ($ json );
35
-
36
- if (!isset ($ decoded )) {
37
- throw new InvalidArgumentException ("Invalid JSON: " . json_last_error_msg ());
38
- }
39
-
40
34
if (Schema::fromJsonString ($ schema )) {
41
35
$ this ->schema = $ schema ;
42
36
}
43
37
44
- $ data = new JsonObject ($ json , true );
45
- $ result = self ::validate ($ data , $ this ->schema );
38
+ $ result = self ::validate ($ json , $ this ->schema );
46
39
if (!$ result ->isValid ()) {
47
40
throw new ValidationException ("JSON Schema validation failed. " , $ result );
48
41
}
49
42
50
- $ this ->data = $ data ;
43
+ $ this ->data = new JsonObject ( $ json , true ) ;
51
44
}
52
45
53
46
/**
54
- * Validate a JsonObject .
47
+ * Validate JSON .
55
48
*
56
- * @param JsonObject $data
57
- * JsonData object to validate against schema.
49
+ * @param string $json
50
+ * JSON string to validate against schema.
58
51
* @param string $schema
59
52
* JSON Schema string.
60
53
*
61
54
* @return ValidationResult
62
55
* Validation result object, contains error report if invalid.
63
56
*/
64
- public static function validate (JsonObject $ data , string $ schema ): ValidationResult
57
+ public static function validate (string $ json , string $ schema ): ValidationResult
65
58
{
59
+ $ decoded = json_decode ($ json );
60
+
61
+ if (!isset ($ decoded )) {
62
+ throw new InvalidArgumentException ("Invalid JSON: " . json_last_error_msg ());
63
+ }
64
+
66
65
$ opiSchema = Schema::fromJsonString ($ schema );
67
66
$ validator = new Validator ();
68
- return $ validator ->schemaValidation (json_decode ( "{ $ data }" ) , $ opiSchema );
67
+ return $ validator ->schemaValidation ($ decoded , $ opiSchema );
69
68
}
70
69
71
70
/**
@@ -75,7 +74,17 @@ public static function validate(JsonObject $data, string $schema): ValidationRes
75
74
*/
76
75
public function __toString ()
77
76
{
78
- return (string ) $ this ->data ;
77
+ return $ this ->data ->getJson ();
78
+ }
79
+
80
+ /**
81
+ * Return pretty-formatted JSON string
82
+ *
83
+ * @return string
84
+ */
85
+ public function pretty ()
86
+ {
87
+ return $ this ->data ->getJson (JSON_PRETTY_PRINT );
79
88
}
80
89
81
90
/**
@@ -103,7 +112,7 @@ public function get(string $path)
103
112
*/
104
113
public function __get (string $ path )
105
114
{
106
- return $ this ->data -> get ($ path );
115
+ return $ this ->get ($ path );
107
116
}
108
117
109
118
/**
@@ -117,6 +126,7 @@ public function __get(string $path)
117
126
*/
118
127
public function set (string $ path , $ value )
119
128
{
129
+ $ this ->normalizeSetValue ($ value );
120
130
$ validationJsonObject = new JsonObject ((string ) $ this ->data );
121
131
$ validationJsonObject ->set ($ path , $ value );
122
132
@@ -130,6 +140,22 @@ public function set(string $path, $value)
130
140
return $ this ->data ->set ($ path , $ value );
131
141
}
132
142
143
+ /**
144
+ * Ensure consistent data type whether RootedJsonData or stdClass.
145
+ *
146
+ * @param mixed $value
147
+ */
148
+ private function normalizeSetValue (&$ value )
149
+ {
150
+ if ($ value instanceof RootedJsonData) {
151
+ $ value = $ value ->{"$ " };
152
+ }
153
+ if ($ value instanceof \stdClass) {
154
+ $ value = new RootedJsonData (json_encode ($ value ));
155
+ $ this ->normalizeSetValue ($ value );
156
+ }
157
+ }
158
+
133
159
/**
134
160
* @see \JsonPath\JsonObject::__get()
135
161
*
@@ -140,15 +166,29 @@ public function set(string $path, $value)
140
166
*/
141
167
public function __set ($ path , $ value )
142
168
{
143
- return $ this ->data -> set ($ path , $ value );
169
+ return $ this ->set ($ path , $ value );
144
170
}
145
171
146
- public function __isset ($ name )
172
+ /**
173
+ * Magic __isset method for a path.
174
+ *
175
+ * @param mixed $path
176
+ * Check if a property at this path is set or not.
177
+ *
178
+ * @return bool
179
+ */
180
+ public function __isset ($ path )
147
181
{
148
182
$ notSmart = new JsonObject ("{$ this ->data }" );
149
- return $ notSmart ->get ($ name ) ? true : false ;
183
+ return $ notSmart ->get ($ path ) ? true : false ;
150
184
}
151
185
186
+ /**
187
+ * Get the JSON Schema as a string.
188
+ *
189
+ * @return string
190
+ * The JSON Schema for this object.
191
+ */
152
192
public function getSchema ()
153
193
{
154
194
return $ this ->schema ;
0 commit comments