Skip to content

Commit

Permalink
Add method forget() and only() in Mapping class
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesChou committed Dec 6, 2018
1 parent a5ec929 commit adc64bb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ public function all()
}

/**
* @param string $name
* @return bool
* @param array|string $keys
*/
public function has($name)
public function forget($keys)
{
return array_key_exists($name, $this->list);
if (!is_array($keys)) {
$keys = func_get_args();
}

foreach ($keys as $key) {
unset($this->list[$key]);
}
}

/**
Expand All @@ -63,6 +68,15 @@ public function get($name)
return $this->list[$name];
}

/**
* @param string $name
* @return bool
*/
public function has($name)
{
return array_key_exists($name, $this->list);
}

/**
* Initial mapping object
*
Expand All @@ -75,6 +89,18 @@ public function init(array $mapping)
}
}

/**
* @param array|string $keys
*/
public function only($keys)
{
if (!is_array($keys)) {
$keys = func_get_args();
}

$this->list = array_intersect_key($this->list, array_flip((array)$keys));
}

/**
* @param string $name
* @param ApiInterface|array $api
Expand Down
26 changes: 26 additions & 0 deletions tests/Rester/ResterClientBasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,32 @@ public function shouldBeOkayWhenCallHasApi()
$this->assertTrue($this->target->hasApi('newOne'));
}

/**
* @test
*/
public function shouldReturnFalseWhenCallHasApiAfterRemoveApi()
{
$this->assertTrue($this->target->hasApi('getFoo'));

$this->target->getMapping()->forget('getFoo');

$this->assertFalse($this->target->hasApi('getFoo'));
}

/**
* @test
*/
public function shouldBeOkayWhenCallHasApiAfterCallOnly()
{
$this->assertTrue($this->target->hasApi('getFoo'));
$this->assertTrue($this->target->hasApi('postFoo'));

$this->target->getMapping()->only('getFoo');

$this->assertTrue($this->target->hasApi('getFoo'));
$this->assertFalse($this->target->hasApi('postFoo'));
}

/**
* @test
*/
Expand Down

0 comments on commit adc64bb

Please sign in to comment.