Skip to content

Commit

Permalink
Added remove method
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegall committed Feb 6, 2023
1 parent 6a5f8bf commit 7e1faf8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ The `set()` method sets a value in the container using dot notation to traverse

The `setAsReference()` method works similarly to the `set()` method, but instead of setting the value directly, it sets a reference to the value. This allows the caller to modify the value directly in the container.

The `remove()` method removes a value from the container using dot notation. The method returns the value that was removed.

The `has()` method checks if a key exists in the container using dot notation to traverse the array. If any intermediate keys in the provided key do not exist, the method returns `false`. If the key exists, the method returns `true`.

The `map()` method applies a callback function to a value within the container and returns the result. If the provided key points to an array, the callback is applied to each item in the array and an array of results is returned. If the `$replace` argument is `true`, the original value in the container is replaced with the result of the callback.
Expand All @@ -48,7 +50,7 @@ The `clear()` removes all items from the container array, except the items speci
use JesseGall\ContainsData\ContainsData;

$data = new class {
use ContainsData;
use ContainsData; // Register the trait
};

// Set a value in the container using dot notation
Expand All @@ -57,6 +59,9 @@ $data->set('foo.bar', 'baz');
// Get a value from the container using dot notation
$value = $data->get('foo.bar'); // "baz"

// Remove a value from the container using dot notation
$data->remove('foo.bar');

// Check if a key exists in the container
if ($data->has('foo.bar')) {
// ...
Expand Down
25 changes: 23 additions & 2 deletions src/ContainsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ public function setAsReference(string $key, mixed &$value): array
return $this->container();
}

/**
* Remove an item using dot notation.
*
* @param string $key
* @return void
*/
public function remove(string $key): void
{
$segments = explode('.', $key);

if (count($segments) > 1) {
try {
$container = &$this->getAsReference(implode('.', array_slice($segments, 0, -1)));
} catch (ReferenceMissingException) {
return;
}
} else {
$container = &$this->container();
}

unset($container[array_pop($segments)]);
}

/**
* Check if an item exists using dot notation.
*
Expand Down Expand Up @@ -253,7 +276,5 @@ public function clear(array $except = []): void
}

$container = $persist->container();

$this->container($container);
}
}
21 changes: 21 additions & 0 deletions tests/ContainsDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,25 @@ public function test_given_except_when_clear_then_reference_still_exists()
$this->assertEquals('new value', $this->subject->get('associative.one'));
}

public function test_when_remove_then_key_does_not_exist()
{
$this->subject->remove('list');

$this->assertFalse($this->subject->has('list'));
}

public function test_when_remove_nested_value_then_key_does_not_exist()
{
$this->subject->remove('one.two.three');

$this->assertFalse($this->subject->has('one.two.three'));
}

public function test_when_remove_but_key_does_not_exist_then_nothing_happens()
{
$this->subject->remove('missing.property');

$this->assertFalse($this->subject->has('missing.property'));
}

}

0 comments on commit 7e1faf8

Please sign in to comment.