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

Implementing unset. #7

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ jobs:
build:
environment:
CC_TEST_REPORTER_ID: 8ec926841c6dfead9c848fd063c569e11b06be11442a8175d588e10607ee2150
XDEBUG_MODE: coverage
docker:
- image: circleci/php:7-cli-node-browsers-legacy
working_directory: ~/repo
Expand Down
14 changes: 11 additions & 3 deletions src/RootedJsonData.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __toString()
*/
public function get(string $path)
{
if ($this->__isset($path) === false) {
if (!isset($this->{$path})) {
return null;
}
return $this->data->get($path);
Expand All @@ -103,7 +103,7 @@ public function get(string $path)
*/
public function __get(string $path)
{
return $this->data->get($path);
return $this->get($path);
}

/**
Expand Down Expand Up @@ -146,9 +146,17 @@ public function __set($path, $value)
public function __isset($name)
Copy link
Member

Choose a reason for hiding this comment

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

Add documentation

Copy link
Member

Choose a reason for hiding this comment

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

Oh my bad, that's from my pull request! I guess I should add that in a separate PR

{
$notSmart = new JsonObject("{$this->data}");
return $notSmart->get($name) ? true : false;
$thing = $notSmart->get($name);
Copy link
Member

Choose a reason for hiding this comment

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

Let's avoid variable names this unnecessarily vague - can we call this $value or $result? Aside from that though, I'm not sure why this is an improvement? When not using smartget, we should always get either an array or false, so I'm not clear on what edge case we're addressing here.

return ($thing === false) ? false : true;
}

public function __unset($name)
Copy link
Member

Choose a reason for hiding this comment

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

Seems like both here and in my __isset() method $path would be a more appropriate name than $name, doesn't it?

Copy link
Member

Choose a reason for hiding this comment

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

Add documentation

{
$field = str_replace('$.', '', $name);
$this->data->remove('$', $field);
Copy link
Member

Choose a reason for hiding this comment

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

Would this work one or more levels down?

}


public function getSchema()
{
return $this->schema;
Expand Down
8 changes: 8 additions & 0 deletions tests/RootedJsonDatatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,12 @@ public function testSchemaGetter()
$data = new RootedJsonData($json, $schema);
$this->assertEquals($schema, $data->getSchema());
}

public function testUnset()
{
$json = '{"number":51}';
$data = new RootedJsonData($json);
unset($data->{'$.number'});
$this->assertNull($data->{'$.number'});
}
}