diff --git a/README.md b/README.md index b1ea893..4408e09 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,15 @@ $newsItem = NewsItem::create([ //attaching tags $newsItem->attachTag('third tag'); +$newsItem->attachTag('third tag','some_type'); $newsItem->attachTags(['fourth tag', 'fifth tag']); +$newsItem->attachTags(['fourth_tag','fifth_tag'],'some_type'); //detaching tags $newsItem->detachTags('third tag'); +$newsItem->detachTags('third tag','some_type'); $newsItem->detachTags(['fourth tag', 'fifth tag']); +$newsItem->detachTags(['fourth tag', 'fifth tag'],'some_type'); //syncing tags $newsItem->syncTags(['first tag', 'second tag']); // all other tags on this model will be detached diff --git a/src/HasTags.php b/src/HasTags.php index bdd0d5d..2c168d7 100644 --- a/src/HasTags.php +++ b/src/HasTags.php @@ -139,14 +139,14 @@ public function tagsWithType(string $type = null): Collection /** * @param array|\ArrayAccess|\Spatie\Tags\Tag $tags - * + * @param string|null $type * @return $this */ - public function attachTags($tags) + public function attachTags($tags, string $type = null) { $className = static::getTagClassName(); - $tags = collect($className::findOrCreate($tags)); + $tags = collect($className::findOrCreate($tags, $type)); $this->tags()->syncWithoutDetaching($tags->pluck('id')->toArray()); @@ -156,21 +156,23 @@ public function attachTags($tags) /** * @param string|\Spatie\Tags\Tag $tag * + * @param string|null $type * @return $this */ - public function attachTag($tag) + public function attachTag($tag, string $type = null) { - return $this->attachTags([$tag]); + return $this->attachTags([$tag], $type); } /** * @param array|\ArrayAccess $tags * + * @param string|null $type * @return $this */ - public function detachTags($tags) + public function detachTags($tags, string $type = null) { - $tags = static::convertToTags($tags); + $tags = static::convertToTags($tags, $type); collect($tags) ->filter() @@ -184,11 +186,12 @@ public function detachTags($tags) /** * @param string|\Spatie\Tags\Tag $tag * + * @param string|null $type * @return $this */ - public function detachTag($tag) + public function detachTag($tag, string $type = null) { - return $this->detachTags([$tag]); + return $this->detachTags([$tag], $type); } /** diff --git a/tests/HasTagsTest.php b/tests/HasTagsTest.php index a4eae3f..f8ded97 100644 --- a/tests/HasTagsTest.php +++ b/tests/HasTagsTest.php @@ -36,6 +36,17 @@ public function it_can_attach_a_tag() $this->assertEquals(['tagName'], $this->testModel->tags->pluck('name')->toArray()); } + public function it_can_attach_a_tag_with_a_type() + { + $this->testModel->attachTag('tagName', 'testType'); + + $this->assertCount(1, $this->testModel->tags); + + $this->assertEquals(['tagName'], $this->testModel->tags->pluck('name')->toArray()); + + $this->assertEquals(['testType'], $this->testModel->tags->pluck('type')->toArray()); + } + /** @test */ public function it_can_attach_a_tag_multiple_times_without_creating_duplicate_entries() { @@ -99,6 +110,14 @@ public function it_can_attach_multiple_tags() $this->assertCount(2, $this->testModel->tags); } + /** @test */ + public function it_can_attach_multiple_tags_with_a_type() + { + $this->testModel->attachTags(['test1', 'test2'], 'testType'); + + $this->assertCount(2, $this->testModel->tags->where('type', '=', 'testType')->toArray()); + } + /** @test */ public function it_can_attach_a_existing_tag() { @@ -117,6 +136,44 @@ public function it_can_detach_a_tag() $this->assertEquals(['test1', 'test3'], $this->testModel->tags->pluck('name')->toArray()); } + /** @test */ + public function it_can_detach_a_tag_with_a_type() + { + $this->testModel->attachTags(['test1', 'test2'], 'testType'); + + $this->testModel->detachTag('test2', 'testType'); + + $this->assertEquals(['test1'], $this->testModel->tags->pluck('name')->toArray()); + } + + /** @test */ + public function it_can_detach_a_tag_with_a_type_and_not_affect_a_tag_without_a_type() + { + $this->testModel->attachTag('test1', 'testType'); + + $this->testModel->attachTag('test1'); + + $this->testModel->detachTag('test1', 'testType'); + + $this->assertEquals(['test1'], $this->testModel->tags->pluck('name')->toArray()); + + $this->assertNull($this->testModel->tags->where('name', '=', 'test1')->first()->type); + } + + /** @test */ + public function it_can_detach_a_tag_with_a_type_while_leaving_another_of_a_different_type() + { + $this->testModel->attachTag('test1', 'testType'); + + $this->testModel->attachTag('test1', 'otherType'); + + $this->testModel->detachTag('test1', 'testType'); + + $this->assertEquals(['test1'], $this->testModel->tags->pluck('name')->sort()->toArray()); + + $this->assertEquals('otherType', $this->testModel->tags->where('name', '=', 'test1')->first()->type); + } + /** @test */ public function it_can_detach_multiple_tags() {