-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Normalize set values when passing objects #9
Changes from all commits
48225c3
ffb8d64
e2fb8a7
ebaabe5
97f9097
2de201a
c219844
bb99e31
a786bda
fed476e
5094c6e
07a1e64
3bfe8d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
vendor/ | ||
.phpunit* | ||
.vscode/ | ||
/.idea/ | ||
/.idea/codeStyles/codeStyleConfig.xml | ||
/composer.lock | ||
/.idea/modules.xml | ||
/.idea/php.xml | ||
/.idea/RootedJsonData.iml | ||
/.idea/vcs.xml | ||
/.idea/workspace.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,41 +31,40 @@ class RootedJsonData | |
*/ | ||
public function __construct(string $json = "{}", string $schema = "{}") | ||
{ | ||
$decoded = json_decode($json); | ||
|
||
if (!isset($decoded)) { | ||
throw new InvalidArgumentException("Invalid JSON: " . json_last_error_msg()); | ||
} | ||
|
||
if (Schema::fromJsonString($schema)) { | ||
$this->schema = $schema; | ||
} | ||
|
||
$data = new JsonObject($json, true); | ||
$result = self::validate($data, $this->schema); | ||
$result = self::validate($json, $this->schema); | ||
if (!$result->isValid()) { | ||
throw new ValidationException("JSON Schema validation failed.", $result); | ||
} | ||
|
||
$this->data = $data; | ||
$this->data = new JsonObject($json, true); | ||
} | ||
|
||
/** | ||
* Validate a JsonObject. | ||
* Validate JSON. | ||
* | ||
* @param JsonObject $data | ||
* JsonData object to validate against schema. | ||
* @param string $json | ||
* JSON string to validate against schema. | ||
* @param string $schema | ||
* JSON Schema string. | ||
* | ||
* @return ValidationResult | ||
* Validation result object, contains error report if invalid. | ||
*/ | ||
public static function validate(JsonObject $data, string $schema): ValidationResult | ||
public static function validate(string $json, string $schema): ValidationResult | ||
{ | ||
$decoded = json_decode($json); | ||
|
||
if (!isset($decoded)) { | ||
throw new InvalidArgumentException("Invalid JSON: " . json_last_error_msg()); | ||
} | ||
|
||
$opiSchema = Schema::fromJsonString($schema); | ||
$validator = new Validator(); | ||
return $validator->schemaValidation(json_decode("{$data}"), $opiSchema); | ||
return $validator->schemaValidation($decoded, $opiSchema); | ||
} | ||
|
||
/** | ||
|
@@ -75,7 +74,17 @@ public static function validate(JsonObject $data, string $schema): ValidationRes | |
*/ | ||
public function __toString() | ||
{ | ||
return (string) $this->data; | ||
return $this->data->getJson(); | ||
} | ||
|
||
/** | ||
* Return pretty-formatted JSON string | ||
* | ||
* @return string | ||
*/ | ||
public function pretty() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method for returning formatted JSON |
||
{ | ||
return $this->data->getJson(JSON_PRETTY_PRINT); | ||
} | ||
|
||
/** | ||
|
@@ -103,7 +112,7 @@ public function get(string $path) | |
*/ | ||
public function __get(string $path) | ||
{ | ||
return $this->data->get($path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reset the magic getter |
||
return $this->get($path); | ||
} | ||
|
||
/** | ||
|
@@ -117,6 +126,7 @@ public function __get(string $path) | |
*/ | ||
public function set(string $path, $value) | ||
{ | ||
$this->normalizeSetValue($value); | ||
$validationJsonObject = new JsonObject((string) $this->data); | ||
$validationJsonObject->set($path, $value); | ||
|
||
|
@@ -130,6 +140,22 @@ public function set(string $path, $value) | |
return $this->data->set($path, $value); | ||
} | ||
|
||
/** | ||
* Ensure consistent data type whether RootedJsonData or stdClass. | ||
* | ||
* @param mixed $value | ||
*/ | ||
private function normalizeSetValue(&$value) | ||
{ | ||
if ($value instanceof RootedJsonData) { | ||
$value = $value->{"$"}; | ||
} | ||
if ($value instanceof \stdClass) { | ||
$value = new RootedJsonData(json_encode($value)); | ||
$this->normalizeSetValue($value); | ||
} | ||
} | ||
|
||
/** | ||
* @see \JsonPath\JsonObject::__get() | ||
* | ||
|
@@ -140,15 +166,29 @@ public function set(string $path, $value) | |
*/ | ||
public function __set($path, $value) | ||
{ | ||
return $this->data->set($path, $value); | ||
return $this->set($path, $value); | ||
} | ||
|
||
public function __isset($name) | ||
/** | ||
* Magic __isset method for a path. | ||
* | ||
* @param mixed $path | ||
* Check if a property at this path is set or not. | ||
* | ||
* @return bool | ||
*/ | ||
public function __isset($path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consistent variable names |
||
{ | ||
$notSmart = new JsonObject("{$this->data}"); | ||
return $notSmart->get($name) ? true : false; | ||
return $notSmart->get($path) ? true : false; | ||
} | ||
|
||
/** | ||
* Get the JSON Schema as a string. | ||
* | ||
* @return string | ||
* The JSON Schema for this object. | ||
*/ | ||
public function getSchema() | ||
{ | ||
return $this->schema; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate before doing anything else. Validate JSON string directly, JsonObject will result in an empty object (
{}
) being transformed into an array ([]
).