Skip to content
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

Merged
merged 13 commits into from
Dec 9, 2020
Merged

Conversation

dafeder
Copy link
Member

@dafeder dafeder commented Dec 4, 2020

A strange behavior inherited from the JsonObeject class means that for an existing RootedJsonData object, setting a property by passing an object, in particular a stdClass resulting from json_decode will cause the full object to be stored at that level of the data structure. Validation still works but retrieving the value after setting it will return the same stdObeject, when we would expect any get() to retrieve an array.

By normalizing incoming objects we avoid this inconsistency. See the new testAddJsonData() for examples.

@@ -31,41 +31,40 @@ class RootedJsonData
*/
public function __construct(string $json = "{}", string $schema = "{}")
{
$decoded = json_decode($json);
Copy link
Member Author

@dafeder dafeder Dec 4, 2020

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 ([]).

*
* @return string
*/
public function pretty()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method for returning formatted JSON

@@ -103,7 +112,7 @@ public function get(string $path)
*/
public function __get(string $path)
{
return $this->data->get($path);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset the magic getter

*
* @return bool
*/
public function __isset($path)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent variable names

@dafeder dafeder changed the title Reverted get set Normalize set values when passing objects Dec 4, 2020
@dafeder dafeder marked this pull request as ready for review December 4, 2020 20:20
@fmizzell fmizzell merged commit ea187f9 into master Dec 9, 2020
@fmizzell fmizzell deleted the reverted-get-set branch December 9, 2020 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants