Skip to content

Commit

Permalink
Add remove() and __unset(), update JsonPath (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder authored Dec 20, 2023
1 parent 2c7152b commit 10eccf1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2
version: "2"
plugins:
phpcodesniffer:
enabled: true
Expand All @@ -13,4 +13,5 @@ exclude_patterns:
- "**/*.xml"
- ".circleci/"
- ".gitignore"
- "docs/"
- "docs/"
- "tests/"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"ext-json": "*",
"galbar/jsonpath": "^1.1",
"galbar/jsonpath": "^3.0",
"opis/json-schema": "^1.0.8"
},
"require-dev": {
Expand Down
40 changes: 40 additions & 0 deletions src/RootedJsonData.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,46 @@ public function __isset($path)
return $notSmart->get($path) ? true : false;
}

/**
* Magic __unset method, detects field from path.
*
* @param mixed $path
* Path to unset, including specific field.
*/
public function __unset($path)
{
$exploded = explode(".", $path);
$field = array_pop($exploded);
$imploded = implode(".", $exploded);
$this->remove($imploded, $field);
}

/**
* Wrapper for JsonObject::remove() method, plus validation.
*
* @param mixed $path
* jsonPath.
* @param mixed $field
* Field to remove.
*
* @return \JsonPath\JsonObject
* Modified object (self).
*/
public function remove($path, $field)
{
$validationJsonObject = new JsonObject((string) $this->data);
$validationJsonObject->remove($path, $field);

$result = self::validate($validationJsonObject, $this->schema);
if (!$result->isValid()) {
$keywordArgs = $result->getFirstError()->keywordArgs();
$message = "{$path} expects a {$keywordArgs['expected']}";
throw new ValidationException($message, $result);
}

return $this->data->remove($path, $field);
}

/**
* Get the JSON Schema as a string.
*
Expand Down
54 changes: 54 additions & 0 deletions tests/RootedJsonDatatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,58 @@ public function testAddWithSchema()
$this->expectException(ValidationException::class);
$data->add("$.numbers", ["name" => "three", "value" => 3]);
}

/**
* If a schema is provided, adding elements that match array should work,
* elements that violate schema will fail.
*/
public function testRemove()
{
$json = '{"field1":"foo","field2":"bar"}';
$schema = '
{
"type": "object",
"required":["field1"],
"properties": {
"field1": {
"type":"string"
},
"field2": {
"type":"string"
}
}
}';
$data = new RootedJsonData($json, $schema);
$data->remove("$", "field2");
$this->assertEquals("foo", $data->{"$.field1"});
$this->expectException(ValidationException::class);
$data->remove("$", "field1");
}

/**
* If a schema is provided, adding elements that match array should work,
* elements that violate schema will fail.
*/
public function testUnset()
{
$json = '{"field1":"foo","field2":"bar"}';
$schema = '
{
"type": "object",
"required":["field1"],
"properties": {
"field1": {
"type":"string"
},
"field2": {
"type":"string"
}
}
}';
$data = new RootedJsonData($json, $schema);
unset($data->{"$.field2"});
$this->assertEquals("foo", $data->{"$.field1"});
$this->expectException(ValidationException::class);
unset($data->{"$.field1"});
}
}

0 comments on commit 10eccf1

Please sign in to comment.