Skip to content

Instantly share code, notes, and snippets.

@simoebenhida
Last active June 21, 2018 23:57
Show Gist options
  • Save simoebenhida/7de855666d44d2e35cd086c57a3baa02 to your computer and use it in GitHub Desktop.
Save simoebenhida/7de855666d44d2e35cd086c57a3baa02 to your computer and use it in GitHub Desktop.
UnsetterHelperTest.php
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Tests\TestCase;
/*
* Unsets properties on (nested) objects.
*
* @param object $object
* @param string $property
*
* @return object|null
*/
function unsetter(object $object, string $property): ?object
{
$properties = collect(explode('->',$property));
$Object = collect($object);
//get the last item and remove it from the properties array.
$delete = $properties->pop();
//check if the properties array is empty
if($properties->count() === 0)
{
//remove the element from $object array then convert it to object.
$Object = (object) collect($object)->forget($delete)->toArray();
//return null if object is null
if(empty((array) $Object))
{
return null;
}
return $Object;
}
$Object = $Object->map(function($value,$key) use($delete) {
//check if an element of $Object is an object
if(is_object($value))
{
return (object) collect($value)->map(function($item,$key) use($delete) {
if(is_object($item)) {
//get only the elements that don't equal to the deleted element.
return (object) collect($item)->filter(function($item,$key) use($delete) {
return $key !== $delete;
})->toArray();
}
})->toArray();
}else {
return $value;
}
});
//delete an empty object
$Object = $Object->filter(function($item,$key) {
if(is_object($item))
{
//check if object is empty
if(empty((array) $item))
{
return false;
}else {
return collect($item)->filter(function($item,$key) {
if(empty((array) $item))
{
return false;
}else {
return $item;
}
})->isNotEmpty();
}
}
return $item;
});
return (object) $Object->toArray();
}
class UnsetterHelperTest extends TestCase
{
public function testUnsettingRootProperty()
{
$object = (object) [
'one' => 'foo',
'two' => 'bar',
];
$result = (object) [
'one' => 'foo',
];
$this->assertEquals(
unsetter($object, 'two'), $result
);
}
public function testEmptyObjectReturnsNull()
{
$object = (object) [
'foo' => 'bar',
];
$this->assertNull(
unsetter($object, 'foo')
);
}
public function testUnsettingNestedProperty()
{
$object = (object) [
'one' => (object) [
'two' => (object) [
'foobar' => 'bar',
'foobaz' => 'baz',
],
],
];
$result = (object) [
'one' => (object) [
'two' => (object) [
'foobar' => 'bar',
],
],
];
$this->assertEquals(
unsetter($object, 'one->two->foobaz'), $result
);
}
public function testUnsettingNestedPropertyIsRemovedWhenEmpty()
{
$object = (object) [
'foo' => 'bar',
'one' => (object) [
'two' => (object) [
'three' => 'foo',
],
],
];
$result = (object) [
'foo' => 'bar',
];
$this->assertEquals(
unsetter($object, 'one->two->three'), $result
);
}
public function testUnsettingNestedPropertyWithTheSameName()
{
$object = (object) [
'foo' => (object) [
'foo' => (object) [
'foo' => 'bar',
'bar' => 'baz',
],
],
];
$result = (object) [
'foo' => (object) [
'foo' => (object) [
'bar' => 'baz',
],
],
];
$this->assertEquals(
unsetter($object, 'foo->foo->foo'), $result
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment