Skip to content

Commit cf5d492

Browse files
authored
Improve no property behavior and schema storage (#6)
1 parent cfd4b28 commit cf5d492

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
vendor/
22
.phpunit*
3-
.vscode/
3+
.vscode/
4+
/.idea/
5+
/composer.lock

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"license": "GPL-3.0-or-later",
66
"require": {
77
"opis/json-schema": "^1.0",
8-
"galbar/jsonpath": "^1.1"
8+
"galbar/jsonpath": "^1.1",
9+
"ext-json": "*"
910
},
1011
"require-dev": {
1112
"phpunit/phpunit": "^9.4"

src/RootedJsonData.php

+39-23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace RootedData;
44

5+
use InvalidArgumentException;
6+
use JsonPath\InvalidJsonException;
57
use Opis\JsonSchema\Schema;
68
use Opis\JsonSchema\Validator;
79
use JsonPath\JsonObject;
@@ -25,16 +27,19 @@ class RootedJsonData
2527
* String of JSON data.
2628
* @param string $schema
2729
* JSON schema document for validation.
30+
* @throws InvalidJsonException
2831
*/
2932
public function __construct(string $json = "{}", string $schema = "{}")
3033
{
3134
$decoded = json_decode($json);
3235

3336
if (!isset($decoded)) {
34-
throw new \InvalidArgumentException("Invalid JSON: " . json_last_error_msg());
37+
throw new InvalidArgumentException("Invalid JSON: " . json_last_error_msg());
3538
}
3639

37-
$this->schema = Schema::fromJsonString($schema);
40+
if (Schema::fromJsonString($schema)) {
41+
$this->schema = $schema;
42+
}
3843

3944
$data = new JsonObject($json, true);
4045
$result = self::validate($data, $this->schema);
@@ -48,19 +53,19 @@ public function __construct(string $json = "{}", string $schema = "{}")
4853
/**
4954
* Validate a JsonObject.
5055
*
51-
* @param JsonPath\JsonObject $data
56+
* @param JsonObject $data
5257
* 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.
5560
*
56-
* @return Opis\JsonSchema\ValidationResult
61+
* @return ValidationResult
5762
* Validation result object, contains error report if invalid.
5863
*/
59-
public static function validate(JsonObject $data, Schema $schema): ValidationResult
64+
public static function validate(JsonObject $data, string $schema): ValidationResult
6065
{
66+
$opiSchema = Schema::fromJsonString($schema);
6167
$validator = new Validator();
62-
$result = $validator->schemaValidation(json_decode("{$data}"), $schema);
63-
return $result;
68+
return $validator->schemaValidation(json_decode("{$data}"), $opiSchema);
6469
}
6570

6671
/**
@@ -80,26 +85,25 @@ public function __toString()
8085
* @return mixed
8186
* Result of JsonPath\JsonObject::__get()
8287
*/
83-
public function get($path)
88+
public function get(string $path)
8489
{
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;
8892
}
89-
return $result;
93+
return $this->data->get($path);
9094
}
9195

9296
/**
93-
* @see JsonPath\JsonObject::__get()
94-
*
9597
* @param string $path
9698
*
9799
* @return mixed
98100
* Result of JsonPath\JsonObject::__get()
101+
* @see \JsonPath\JsonObject::__get()
102+
*
99103
*/
100-
public function __get($path)
104+
public function __get(string $path)
101105
{
102-
return $this->get($path);
106+
return $this->data->get($path);
103107
}
104108

105109
/**
@@ -108,9 +112,10 @@ public function __get($path)
108112
* @param string $path
109113
* @param mixed $value
110114
*
111-
* @return JsonPath\JsonObject
115+
* @return JsonObject
116+
* @throws InvalidJsonException
112117
*/
113-
public function set($path, $value)
118+
public function set(string $path, $value)
114119
{
115120
$validationJsonObject = new JsonObject((string) $this->data);
116121
$validationJsonObject->set($path, $value);
@@ -126,15 +131,26 @@ public function set($path, $value)
126131
}
127132

128133
/**
129-
* @see JsonPath\JsonObject::__set()
134+
* @see \JsonPath\JsonObject::__get()
130135
*
131136
* @param mixed $path
132137
* @param mixed $value
133138
*
134-
* @return JsonPath\JsonObject
139+
* @return JsonObject
135140
*/
136141
public function __set($path, $value)
137142
{
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;
139155
}
140156
}

tests/RootedJsonDatatTest.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\TestCase;
77
use RootedData\RootedJsonData;
88
use Opis\JsonSchema\Exception\InvalidSchemaException;
9+
use Opis\JsonSchema\Schema;
910
use RootedData\Exception\ValidationException;
1011

1112
class RootedJsonDataTest extends TestCase
@@ -39,9 +40,9 @@ public function testBracketSyntax()
3940

4041
public function testAccessToNonExistentProperties()
4142
{
42-
$this->expectExceptionMessage("Property $.city is not set");
4343
$data = new RootedJsonData();
44-
$city = $data->get("$.city");
44+
$this->assertNull($data->get("$.city"));
45+
$this->assertFalse(isset($data->{"$.city"}));
4546
}
4647

4748
public function testJsonFormat()
@@ -110,4 +111,12 @@ public function testJsonPathSetter()
110111
$data->set("$.container.number", 52);
111112
$this->assertEquals(52, $data->get("$.container.number"));
112113
}
114+
115+
public function testSchemaGetter()
116+
{
117+
$json = '{"number":51}';
118+
$schema = '{"type": "object","properties":{"number":{"type":"number"}}}';
119+
$data = new RootedJsonData($json, $schema);
120+
$this->assertEquals($schema, $data->getSchema());
121+
}
113122
}

0 commit comments

Comments
 (0)