From adc64bb78951b3fe7470171e82bd7adcbb8123e1 Mon Sep 17 00:00:00 2001 From: MilesChou Date: Thu, 6 Dec 2018 12:02:28 +0800 Subject: [PATCH] Add method forget() and only() in Mapping class --- src/Mapping.php | 34 +++++++++++++++++++++++--- tests/Rester/ResterClientBasicTest.php | 26 ++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/Mapping.php b/src/Mapping.php index dc5a778..414fc47 100644 --- a/src/Mapping.php +++ b/src/Mapping.php @@ -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]); + } } /** @@ -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 * @@ -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 diff --git a/tests/Rester/ResterClientBasicTest.php b/tests/Rester/ResterClientBasicTest.php index e002b6e..c53a14d 100644 --- a/tests/Rester/ResterClientBasicTest.php +++ b/tests/Rester/ResterClientBasicTest.php @@ -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 */