Skip to content

Commit 6b3fdb7

Browse files
author
FreedomKnight
committed
修改成 categorize
1 parent f267b6c commit 6b3fdb7

File tree

3 files changed

+95
-55
lines changed

3 files changed

+95
-55
lines changed

src/Categorizable.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,34 @@
33

44
use Cviebrock\EloquentTaggable\Taggable;
55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
67

78
trait Categorizable
89
{
910
use Taggable;
10-
11-
public function tagId($id)
11+
12+
public function categories(): MorphToMany
1213
{
13-
$tags = (array)$id;
14-
$this->tag($tags);
14+
return $this->tags();
1515
}
1616

17-
public function retagId($id)
17+
public function categorize($categories)
1818
{
19-
$tags = (array)$id;
20-
$this->retag($tags);
19+
return $this->tag($categories);
2120
}
2221

23-
public function untagId($id)
22+
public function decategorize($categories)
2423
{
25-
$tags = (array)$id;
26-
$this->untag($tags);
24+
return $this->untag($categories);
2725
}
2826

29-
public function detagId($id)
27+
public function recategorize($categories)
3028
{
31-
$tags = (array)$id;
32-
$this->detag($tags);
29+
return $this->retag($categories);
3330
}
3431

35-
public function scopeWithAllTagsId(Builder $query, $id): Builder
32+
public function scopeWithAllCategories(Builder $query, $categories): Builder
3633
{
37-
$tags = (array)$id;
3834
return $this->scopeWithAllTags($query, $tags);
3935
}
4036
}

src/Services/TagService.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace UniSharp\Category\Services;
3+
4+
use Cviebrock\EloquentTaggable\Models\Tag;
5+
use Cviebrock\EloquentTaggable\Services\TagService as OriginalTagService;
6+
7+
class TagService extends OriginalTagService
8+
{
9+
public function find(string $tagName)
10+
{
11+
if (is_numeric($tagName) && $tag = Tag::find($tagName)) {
12+
return $tag;
13+
}
14+
return Tag::byName($tagName)->first();
15+
}
16+
17+
public function buildTagArray($tags): array
18+
{
19+
if (is_int($tags)) {
20+
$tags = (string) $tags;
21+
}
22+
23+
if (is_iterable($tags)) {
24+
$tags = collect($tags)->map(function ($tag) {
25+
return (string) $tag;
26+
})->toArray();
27+
}
28+
29+
return parent::buildTagArray($tags);
30+
}
31+
}

tests/CategoryTest.php

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,75 @@
22
namespace UniSharp\Category\Test;
33

44
use UniSharp\Category\Test\TestCase;
5-
use UniSharp\Category\Models\Category;
65
use UniSharp\Category\Test\TestModel;
6+
use UniSharp\Category\Models\Category;
7+
use UniSharp\Category\Services\TagService as UnisharpTagService;
8+
use Cviebrock\EloquentTaggable\Services\TagService;
79

810
class CategoriesTest extends TestCase
911
{
1012
public function setUp()
1113
{
1214
parent::setUp();
1315
$this->testModel = $this->newModel();
16+
$this->app->singleton(TagService::class, UnisharpTagService::class);
1417
}
1518

1619
// tag所需用id(Integer屬性)
1720
public function testCategoryAddIntegerId()
1821
{
19-
$this->testModel->tagId(1);
22+
$this->testModel->categorize(1);
2023
$this->assertCount(1, $this->testModel->tags);
2124
$arr = [2,3,4,1];
22-
$this->testModel->tagId($arr);
25+
$this->testModel->categorize($arr);
2326
$this->assertCount(4, $this->testModel->tags);
2427
}
28+
29+
public function testCategorize()
30+
{
31+
$model = TestModel::create(['title' => 'test']);
32+
$model->categorize("News");
33+
$this->assertEquals("News", $model->categories->pluck('name')->first());
34+
}
35+
36+
public function testCategorizeDelimeter()
37+
{
38+
$model = TestModel::create(['title' => 'test']);
39+
$model->categorize("News,Products");
40+
$this->assertArraySubset(["News", "Products"], $model->categories->pluck('name')->toArray());
41+
}
42+
43+
public function testCategorizeArray()
44+
{
45+
$model = TestModel::create(['title' => 'test']);
46+
$model->categorize(["News", "Products"]);
47+
$this->assertArraySubset(["News", "Products"], $model->categories->pluck('name')->toArray());
48+
}
49+
50+
public function testCategorizeById()
51+
{
52+
$model = TestModel::create(['title' => 'test']);
53+
$foo = Category::create(["name" => "foo"]);
54+
$bar = Category::create(["name" => "bar"]);
55+
$model->categorize("{$bar->tag_id}");
56+
$this->assertCount(1, $model->categories);
57+
$this->assertEquals($bar->id, $model->categories->pluck('id')->first());
58+
}
59+
60+
public function testCategorizeByManyId()
61+
{
62+
$model = TestModel::create(['title' => 'test']);
63+
$foo = Category::create(["name" => "foo"]);
64+
$bar = Category::create(["name" => "bar"]);
65+
$model->categorize(["{$foo->tag_id}", "{$bar->tag_id}"]);
66+
$this->assertCount(2, $model->categories);
67+
$this->assertEquals([$foo->tag_id, $bar->tag_id], $model->categories->pluck('tag_id')->toArray());
68+
}
2569

26-
//
2770
public function testCategoryUnTagId()
2871
{
29-
$this->testModel->tagId([1,2,3]);
30-
$this->testModel->untagId(1);
72+
$this->testModel->categorize([1,2,3]);
73+
$this->testModel->decategorize(1);
3174
$this->assertEquals(
3275
"2,3",
3376
$this->testModel->tagList
@@ -36,8 +79,8 @@ public function testCategoryUnTagId()
3679

3780
public function testCategoryReTagId()
3881
{
39-
$this->testModel->tagId(1);
40-
$this->testModel->retagId(2);
82+
$this->testModel->categorize(1);
83+
$this->testModel->recategorize(2);
4184
$this->assertEquals(
4285
2,
4386
$this->testModel->tagList
@@ -46,41 +89,11 @@ public function testCategoryReTagId()
4689

4790
public function testCategoryDeTagId()
4891
{
49-
$this->testModel->tagId(1);
50-
$this->testModel->detagId(1);
92+
$this->testModel->categorize(1);
93+
$this->testModel->decategorize(1);
5194
$this->assertEquals(
5295
"",
5396
$this->testModel->tagList
5497
);
5598
}
56-
57-
public function testIntegerOneIdGetModel()
58-
{
59-
$arr = [2,3,4,1];
60-
$this->testModel->tagId($arr);
61-
$model = $this->newModel(["title" => "apple"]);
62-
$model->tagId(2);
63-
$test = $this->newModel(["title" => "banana"]);
64-
$this->assertCount(2, TestModel::withAllTagsId(2)->get());
65-
$title = TestModel::withAllTagsId([2])->get();
66-
$this->assertEquals(
67-
"apple",
68-
$title[1]->title
69-
);
70-
}
71-
72-
public function testIntegerManyIdGetModel()
73-
{
74-
$arr = [2,3,4,1];
75-
$this->testModel->tagId($arr);
76-
$model = $this->newModel(["title" => "apple"]);
77-
$model->tagId(2);
78-
$test = $this->newModel(["title" => "banana"]);
79-
$this->assertCount(2, TestModel::withAllTagsId(2)->get());
80-
$title = TestModel::withAllTagsId([2,4])->get();
81-
$this->assertEquals(
82-
"test",
83-
$title[0]->title
84-
);
85-
}
8699
}

0 commit comments

Comments
 (0)