• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / category / 13097770468

02 Feb 2025 10:01AM UTC coverage: 39.815%. Remained the same
13097770468

push

github

twinh
refactor(internal): 生成元数据到模型类中,删除 Trait

2 of 2 branches covered (100.0%)

43 of 108 relevant lines covered (39.81%)

6.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.29
/src/Service/CategoryModel.php
1
<?php
2

3
namespace Miaoxing\Category\Service;
4

5
use Miaoxing\Plugin\BaseModel;
6
use Miaoxing\Plugin\Model\HasAppIdTrait;
7
use Miaoxing\Plugin\Model\ModelTrait;
8
use Miaoxing\Plugin\Model\ReqQueryTrait;
9
use Miaoxing\Plugin\Model\SnowflakeTrait;
10
use Wei\Model\SoftDeleteTrait;
11

12
/**
13
 * @property CategoryModel $parent
14
 * @property CategoryModel[]|CategoryModel $children
15
 * @property string|null $id
16
 * @property string $appId
17
 * @property string $parentId
18
 * @property int $level 分类的层级
19
 * @property string $name
20
 * @property string $image
21
 * @property string $description
22
 * @property int $sort
23
 * @property string $listTpl
24
 * @property string $linkTo 链接到的配置数组
25
 * @property bool $isEnabled 是否启用
26
 * @property int $pv
27
 * @property int $uv
28
 * @property string|null $createdAt
29
 * @property string|null $updatedAt
30
 * @property string $createdBy
31
 * @property string $updatedBy
32
 * @property string|null $deletedAt
33
 * @property string $deletedBy
34
 */
35
class CategoryModel extends BaseModel
36
{
37
    use HasAppIdTrait;
38
    use ModelTrait;
39
    use ReqQueryTrait;
40
    use SnowflakeTrait;
41
    use SoftDeleteTrait;
42

43
    protected $attributes = [
44
        'parentId' => 0,
45
        'level' => 1,
46
        'sort' => 50,
47
        'linkTo' => [],
48
    ];
49

50
    protected $columns = [
51
        'linkTo' => [
52
            'cast' => 'json',
53
            'default' => [],
54
        ],
55
    ];
56

57
    public function getGuarded(): array
58
    {
59
        return array_merge($this->guarded, [
60✔
60
            'level',
60✔
61
        ]);
60✔
62
    }
63

64
    public function afterDestroy()
65
    {
66
        $this->children->destroy();
18✔
67
    }
68

69
    /**
70
     * 如果设置了URL地址,说明是外链,否则是内部栏目
71
     *
72
     * @return string
73
     */
74
    public function getUrl()
75
    {
76
        if (!$this['linkTo'] || !isset($this['linkTo']['type'])) {
×
77
            $this['linkTo'] = $this->createLinkTo();
×
78
        }
79

80
        return $this->linkTo->getUrl($this['linkTo']);
×
81
    }
82

83
    /**
84
     * Record: 根据当前栏目,创建linkTo数组
85
     *
86
     * @return array
87
     * @todo 是否应该由mallCategory,photoCategory等去生成URL?
88
     */
89
    public function createLinkTo()
90
    {
91
        switch (true) {
92
            case 'mall' == $this['type']:
×
93
                return ['type' => 'mall', 'mall' => 'products?categoryId=' . $this['id']];
×
94

95
            case 'photo' == $this['type']:
×
96
                return ['type' => 'photo', 'photo' => 'album?categoryId=' . $this['id']];
×
97

98
            case 'video' == $this['type']:
×
99
                return ['type' => 'video', 'video' => 'video?categoryId=' . $this['id']];
×
100

101
            case 'article' == $this['type']:
×
102
            default:
103
                return ['type' => 'site', 'site' => 'article?categoryId=' . $this['id']];
×
104
        }
105
    }
106

107
    /**
108
     * 获取当前栏目的父栏目
109
     *
110
     * @return $this
111
     */
112
    public function parent(): self
113
    {
114
        return $this->belongsTo(static::class, 'id', 'parent_id');
6✔
115
    }
116

117
    public function children(): self
118
    {
119
        return $this->hasMany(static::class, 'parent_id')->desc('sort');
36✔
120
    }
121

122
    /**
123
     * Record: 获取当前栏目的所有父栏目
124
     */
125
    public function getParents()
126
    {
127
        $parents = $this()->beColl();
×
128

129
        $category = $this;
×
130
        while ($parent = $category->getParent()->find()) {
×
131
            $parents[] = $category = $parent;
×
132
        }
133

134
        return $parents;
×
135
    }
136

137
    /**
138
     * Record: 获取某级父栏目
139
     * @param mixed $level
140
     */
141
    public function getParentByLevel($level)
142
    {
143
        $parent = $this;
×
144
        while ($parent->find()) {
×
145
            if ($parent['level'] == $level) {
×
146
                break;
×
147
            }
148
            $parent = $parent->getParent();
×
149
        }
150

151
        return $parent;
×
152
    }
153

154
    /**
155
     * Collection
156
     *
157
     * @param array $categories
158
     * @return array
159
     */
160
    public function getTree($categories = [])
161
    {
162
        foreach ($this->attributes as $category) {
×
163
            $categories[] = $category;
×
164
            $categories = $category->children->getTree($categories);
×
165
        }
166

167
        return $categories;
×
168
    }
169

170
    /**
171
     * 获取当前分类下的所有子分类
172
     *
173
     * @param Category $categories
174
     * @return Category|Category[]
175
     */
176
    public function getAllChildren(?Category $categories = null)
177
    {
178
        // 第一次,初始化一个Collection对象
179
        if (!$categories) {
×
180
            $categories = $this()->beColl();
×
181
        }
182

183
        // 递归获取所有子分类
184
        $children = $this->getChildren()->findAll();
×
185
        foreach ($children as $category) {
×
186
            $categories[] = $category;
×
187
            $category->getAllChildren($categories);
×
188
        }
189

190
        // 返回所有分类
191
        return $categories;
×
192
    }
193

194
    /**
195
     * Record: 获取当前分类和子分类
196
     */
197
    public function getChildrenIds()
198
    {
199
        $categories = $this->getAllChildren();
×
200
        $categories[] = $this;
×
201

202
        return $categories->getAll('id');
×
203
    }
204
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc