From 561671c42c31b06cafa18a1c893a7fb75edd334b Mon Sep 17 00:00:00 2001 From: Dan Feder Date: Tue, 17 Nov 2020 12:13:27 -0500 Subject: [PATCH 1/6] Isset, also getSchema() --- src/RootedJsonData.php | 23 ++++++++++++++++------- tests/RootedJsonDatatTest.php | 13 +++++++++++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/RootedJsonData.php b/src/RootedJsonData.php index 137fdcb..bee2bb8 100644 --- a/src/RootedJsonData.php +++ b/src/RootedJsonData.php @@ -82,11 +82,10 @@ public function __toString() */ public function get($path) { - $result = $this->data->get($path); - if ($result === false) { - throw new \Exception("Property {$path} is not set"); + if ($this->__isset($path) === false) { + return null; } - return $result; + return $this->data->get($path); } /** @@ -99,7 +98,7 @@ public function get($path) */ public function __get($path) { - return $this->get($path); + return $this->data->get($path); } /** @@ -126,7 +125,7 @@ public function set($path, $value) } /** - * @see JsonPath\JsonObject::__set() + * @see JsonPath\JsonObject::__get() * * @param mixed $path * @param mixed $value @@ -135,6 +134,16 @@ public function set($path, $value) */ public function __set($path, $value) { - return $this->set($path, $value); + return $this->data->set($path, $value); + } + + public function __isset($name) + { + $notSmart = new JsonObject("{$this->data}"); + return $notSmart->get($name) ? true : false; + } + + public function getSchema() { + return $this->schema; } } diff --git a/tests/RootedJsonDatatTest.php b/tests/RootedJsonDatatTest.php index a4af9d5..5b1f8f8 100644 --- a/tests/RootedJsonDatatTest.php +++ b/tests/RootedJsonDatatTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use RootedData\RootedJsonData; use Opis\JsonSchema\Exception\InvalidSchemaException; +use Opis\JsonSchema\Schema; use RootedData\Exception\ValidationException; class RootedJsonDataTest extends TestCase @@ -39,9 +40,9 @@ public function testBracketSyntax() public function testAccessToNonExistentProperties() { - $this->expectExceptionMessage("Property $.city is not set"); $data = new RootedJsonData(); - $city = $data->get("$.city"); + $this->assertNull($data->get("$.city")); + $this->assertFalse(isset($data->{"$.city"})); } public function testJsonFormat() @@ -110,4 +111,12 @@ public function testJsonPathSetter() $data->set("$.container.number", 52); $this->assertEquals(52, $data->get("$.container.number")); } + + public function testSchemaGetter() + { + $json = '{"number":51}'; + $schema = '{"type": "object","properties":{"number":{"type":"number"}}}'; + $data = new RootedJsonData($json, $schema); + $this->assertInstanceOf(Schema::class, $data->getSchema()); + } } From 84bb8be2392ef990c30a3a2e49cf3243ffb5dc8a Mon Sep 17 00:00:00 2001 From: Dan Feder Date: Tue, 17 Nov 2020 12:32:57 -0500 Subject: [PATCH 2/6] Change schema storage to string --- src/RootedJsonData.php | 13 ++++++++----- tests/RootedJsonDatatTest.php | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/RootedJsonData.php b/src/RootedJsonData.php index bee2bb8..bd095d9 100644 --- a/src/RootedJsonData.php +++ b/src/RootedJsonData.php @@ -34,7 +34,9 @@ public function __construct(string $json = "{}", string $schema = "{}") throw new \InvalidArgumentException("Invalid JSON: " . json_last_error_msg()); } - $this->schema = Schema::fromJsonString($schema); + if (Schema::fromJsonString($schema)) { + $this->schema = $schema; + } $data = new JsonObject($json, true); $result = self::validate($data, $this->schema); @@ -50,16 +52,17 @@ public function __construct(string $json = "{}", string $schema = "{}") * * @param JsonPath\JsonObject $data * JsonData object to validate against schema. - * @param Opis\JsonSchema\Schema $schema - * And Opis Json-Schema schema object to validate data against. + * @param string $schema + * JSON Schema string. * * @return Opis\JsonSchema\ValidationResult * Validation result object, contains error report if invalid. */ - public static function validate(JsonObject $data, Schema $schema): ValidationResult + public static function validate(JsonObject $data, string $schema): ValidationResult { + $opiSchema = Schema::fromJsonString($schema); $validator = new Validator(); - $result = $validator->schemaValidation(json_decode("{$data}"), $schema); + $result = $validator->schemaValidation(json_decode("{$data}"), $opiSchema); return $result; } diff --git a/tests/RootedJsonDatatTest.php b/tests/RootedJsonDatatTest.php index 5b1f8f8..03b4f52 100644 --- a/tests/RootedJsonDatatTest.php +++ b/tests/RootedJsonDatatTest.php @@ -117,6 +117,6 @@ public function testSchemaGetter() $json = '{"number":51}'; $schema = '{"type": "object","properties":{"number":{"type":"number"}}}'; $data = new RootedJsonData($json, $schema); - $this->assertInstanceOf(Schema::class, $data->getSchema()); + $this->assertEquals($schema, $data->getSchema()); } } From 4aecebbafd2a2f6cb577bf5a984a445e564074b4 Mon Sep 17 00:00:00 2001 From: Dan Feder Date: Thu, 19 Nov 2020 18:07:46 -0500 Subject: [PATCH 3/6] Fix doc issues --- src/RootedJsonData.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/RootedJsonData.php b/src/RootedJsonData.php index bd095d9..ce5d64e 100644 --- a/src/RootedJsonData.php +++ b/src/RootedJsonData.php @@ -128,7 +128,7 @@ public function set($path, $value) } /** - * @see JsonPath\JsonObject::__get() + * @see JsonPath\JsonObject::__set() * * @param mixed $path * @param mixed $value @@ -140,13 +140,27 @@ public function __set($path, $value) return $this->data->set($path, $value); } - public function __isset($name) + /** + * Check if a property or path is set. + * + * @param string $path + * Path to check. + * + * @return bool + * / + public function __isset(string $path) { $notSmart = new JsonObject("{$this->data}"); return $notSmart->get($name) ? true : false; } - public function getSchema() { + /** + * Get the JSON Schema for the JSON. + * + * @return string + * / + public function getSchema() + { return $this->schema; } } From 48b6d3e40e7f2478959de4478177e8f720d28311 Mon Sep 17 00:00:00 2001 From: Dan Feder Date: Thu, 19 Nov 2020 19:06:33 -0500 Subject: [PATCH 4/6] Revert "Fix doc issues" This reverts commit 4aecebba --- src/RootedJsonData.php | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/RootedJsonData.php b/src/RootedJsonData.php index ce5d64e..bd095d9 100644 --- a/src/RootedJsonData.php +++ b/src/RootedJsonData.php @@ -128,7 +128,7 @@ public function set($path, $value) } /** - * @see JsonPath\JsonObject::__set() + * @see JsonPath\JsonObject::__get() * * @param mixed $path * @param mixed $value @@ -140,27 +140,13 @@ public function __set($path, $value) return $this->data->set($path, $value); } - /** - * Check if a property or path is set. - * - * @param string $path - * Path to check. - * - * @return bool - * / - public function __isset(string $path) + public function __isset($name) { $notSmart = new JsonObject("{$this->data}"); return $notSmart->get($name) ? true : false; } - /** - * Get the JSON Schema for the JSON. - * - * @return string - * / - public function getSchema() - { + public function getSchema() { return $this->schema; } } From 51a9fd8dddaf6f19f52aba10d58422558f1aa293 Mon Sep 17 00:00:00 2001 From: Dan Feder Date: Thu, 19 Nov 2020 19:12:27 -0500 Subject: [PATCH 5/6] Fix codeclimate and phpstorm complaints --- .gitignore | 4 +++- composer.json | 3 ++- src/RootedJsonData.php | 29 ++++++++++++++++------------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 2ed0cc3..76cdfb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ vendor/ .phpunit* -.vscode/ \ No newline at end of file +.vscode/ +/.idea/ +/composer.lock diff --git a/composer.json b/composer.json index 465494d..2ccaa86 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,8 @@ "license": "GPL-3.0-or-later", "require": { "opis/json-schema": "^1.0", - "galbar/jsonpath": "^1.1" + "galbar/jsonpath": "^1.1", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "^9.4" diff --git a/src/RootedJsonData.php b/src/RootedJsonData.php index bd095d9..4413cea 100644 --- a/src/RootedJsonData.php +++ b/src/RootedJsonData.php @@ -2,6 +2,8 @@ namespace RootedData; +use InvalidArgumentException; +use JsonPath\InvalidJsonException; use Opis\JsonSchema\Schema; use Opis\JsonSchema\Validator; use JsonPath\JsonObject; @@ -25,13 +27,14 @@ class RootedJsonData * String of JSON data. * @param string $schema * JSON schema document for validation. + * @throws InvalidJsonException */ public function __construct(string $json = "{}", string $schema = "{}") { $decoded = json_decode($json); if (!isset($decoded)) { - throw new \InvalidArgumentException("Invalid JSON: " . json_last_error_msg()); + throw new InvalidArgumentException("Invalid JSON: " . json_last_error_msg()); } if (Schema::fromJsonString($schema)) { @@ -50,20 +53,19 @@ public function __construct(string $json = "{}", string $schema = "{}") /** * Validate a JsonObject. * - * @param JsonPath\JsonObject $data + * @param JsonObject $data * JsonData object to validate against schema. * @param string $schema * JSON Schema string. * - * @return Opis\JsonSchema\ValidationResult + * @return ValidationResult * Validation result object, contains error report if invalid. */ public static function validate(JsonObject $data, string $schema): ValidationResult { $opiSchema = Schema::fromJsonString($schema); $validator = new Validator(); - $result = $validator->schemaValidation(json_decode("{$data}"), $opiSchema); - return $result; + return $validator->schemaValidation(json_decode("{$data}"), $opiSchema); } /** @@ -83,7 +85,7 @@ public function __toString() * @return mixed * Result of JsonPath\JsonObject::__get() */ - public function get($path) + public function get(string $path) { if ($this->__isset($path) === false) { return null; @@ -92,14 +94,14 @@ public function get($path) } /** - * @see JsonPath\JsonObject::__get() - * * @param string $path * * @return mixed * Result of JsonPath\JsonObject::__get() + * @see \JsonPath\JsonObject::__get() + * */ - public function __get($path) + public function __get(string $path) { return $this->data->get($path); } @@ -110,9 +112,10 @@ public function __get($path) * @param string $path * @param mixed $value * - * @return JsonPath\JsonObject + * @return JsonObject + * @throws InvalidJsonException */ - public function set($path, $value) + public function set(string $path, $value) { $validationJsonObject = new JsonObject((string) $this->data); $validationJsonObject->set($path, $value); @@ -128,12 +131,12 @@ public function set($path, $value) } /** - * @see JsonPath\JsonObject::__get() + * @see \JsonPath\JsonObject::__get() * * @param mixed $path * @param mixed $value * - * @return JsonPath\JsonObject + * @return JsonObject */ public function __set($path, $value) { From 91fa54c18748ac44ab83d0afd0995f878cfa2b41 Mon Sep 17 00:00:00 2001 From: Dan Feder Date: Thu, 19 Nov 2020 19:18:23 -0500 Subject: [PATCH 6/6] New line --- src/RootedJsonData.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/RootedJsonData.php b/src/RootedJsonData.php index 4413cea..506e409 100644 --- a/src/RootedJsonData.php +++ b/src/RootedJsonData.php @@ -149,7 +149,8 @@ public function __isset($name) return $notSmart->get($name) ? true : false; } - public function getSchema() { + public function getSchema() + { return $this->schema; } }