2
2
3
3
namespace RootedData ;
4
4
5
+ use InvalidArgumentException ;
6
+ use JsonPath \InvalidJsonException ;
5
7
use Opis \JsonSchema \Schema ;
6
8
use Opis \JsonSchema \Validator ;
7
9
use JsonPath \JsonObject ;
@@ -25,16 +27,19 @@ class RootedJsonData
25
27
* String of JSON data.
26
28
* @param string $schema
27
29
* JSON schema document for validation.
30
+ * @throws InvalidJsonException
28
31
*/
29
32
public function __construct (string $ json = "{} " , string $ schema = "{} " )
30
33
{
31
34
$ decoded = json_decode ($ json );
32
35
33
36
if (!isset ($ decoded )) {
34
- throw new \ InvalidArgumentException ("Invalid JSON: " . json_last_error_msg ());
37
+ throw new InvalidArgumentException ("Invalid JSON: " . json_last_error_msg ());
35
38
}
36
39
37
- $ this ->schema = Schema::fromJsonString ($ schema );
40
+ if (Schema::fromJsonString ($ schema )) {
41
+ $ this ->schema = $ schema ;
42
+ }
38
43
39
44
$ data = new JsonObject ($ json , true );
40
45
$ result = self ::validate ($ data , $ this ->schema );
@@ -48,19 +53,19 @@ public function __construct(string $json = "{}", string $schema = "{}")
48
53
/**
49
54
* Validate a JsonObject.
50
55
*
51
- * @param JsonPath\ JsonObject $data
56
+ * @param JsonObject $data
52
57
* JsonData object to validate against schema.
53
- * @param Opis\JsonSchema\Schema $schema
54
- * And Opis Json- Schema schema object to validate data against .
58
+ * @param string $schema
59
+ * JSON Schema string .
55
60
*
56
- * @return Opis\JsonSchema\ ValidationResult
61
+ * @return ValidationResult
57
62
* Validation result object, contains error report if invalid.
58
63
*/
59
- public static function validate (JsonObject $ data , Schema $ schema ): ValidationResult
64
+ public static function validate (JsonObject $ data , string $ schema ): ValidationResult
60
65
{
66
+ $ opiSchema = Schema::fromJsonString ($ schema );
61
67
$ validator = new Validator ();
62
- $ result = $ validator ->schemaValidation (json_decode ("{$ data }" ), $ schema );
63
- return $ result ;
68
+ return $ validator ->schemaValidation (json_decode ("{$ data }" ), $ opiSchema );
64
69
}
65
70
66
71
/**
@@ -80,26 +85,25 @@ public function __toString()
80
85
* @return mixed
81
86
* Result of JsonPath\JsonObject::__get()
82
87
*/
83
- public function get ($ path )
88
+ public function get (string $ path )
84
89
{
85
- $ result = $ this ->data ->get ($ path );
86
- if ($ result === false ) {
87
- throw new \Exception ("Property {$ path } is not set " );
90
+ if ($ this ->__isset ($ path ) === false ) {
91
+ return null ;
88
92
}
89
- return $ result ;
93
+ return $ this -> data -> get ( $ path ) ;
90
94
}
91
95
92
96
/**
93
- * @see JsonPath\JsonObject::__get()
94
- *
95
97
* @param string $path
96
98
*
97
99
* @return mixed
98
100
* Result of JsonPath\JsonObject::__get()
101
+ * @see \JsonPath\JsonObject::__get()
102
+ *
99
103
*/
100
- public function __get ($ path )
104
+ public function __get (string $ path )
101
105
{
102
- return $ this ->get ($ path );
106
+ return $ this ->data -> get ($ path );
103
107
}
104
108
105
109
/**
@@ -108,9 +112,10 @@ public function __get($path)
108
112
* @param string $path
109
113
* @param mixed $value
110
114
*
111
- * @return JsonPath\JsonObject
115
+ * @return JsonObject
116
+ * @throws InvalidJsonException
112
117
*/
113
- public function set ($ path , $ value )
118
+ public function set (string $ path , $ value )
114
119
{
115
120
$ validationJsonObject = new JsonObject ((string ) $ this ->data );
116
121
$ validationJsonObject ->set ($ path , $ value );
@@ -126,15 +131,26 @@ public function set($path, $value)
126
131
}
127
132
128
133
/**
129
- * @see JsonPath\JsonObject::__set ()
134
+ * @see \ JsonPath\JsonObject::__get ()
130
135
*
131
136
* @param mixed $path
132
137
* @param mixed $value
133
138
*
134
- * @return JsonPath\ JsonObject
139
+ * @return JsonObject
135
140
*/
136
141
public function __set ($ path , $ value )
137
142
{
138
- return $ this ->set ($ path , $ value );
143
+ return $ this ->data ->set ($ path , $ value );
144
+ }
145
+
146
+ public function __isset ($ name )
147
+ {
148
+ $ notSmart = new JsonObject ("{$ this ->data }" );
149
+ return $ notSmart ->get ($ name ) ? true : false ;
150
+ }
151
+
152
+ public function getSchema ()
153
+ {
154
+ return $ this ->schema ;
139
155
}
140
156
}
0 commit comments