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

miaoxing / admin / 13066008623

30 Jan 2025 03:53PM UTC coverage: 20.332% (+0.03%) from 20.306%
13066008623

push

github

twinh
refactor: Table 中的切换功能改为 Switch 组件

19 of 83 branches covered (22.89%)

147 of 723 relevant lines covered (20.33%)

2.46 hits per line

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

32.26
/src/Service/AdminMenuModel.php
1
<?php
2

3
namespace Miaoxing\Admin\Service;
4

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

12
/**
13
 * @property string|null $id 编号
14
 * @property string $parentId 上级菜单
15
 * @property int $level 层级
16
 * @property string $path 路径
17
 * @property string $code 标识
18
 * @property string $label 名称
19
 * @property string $url 链接
20
 * @property string $icon 图标
21
 * @property int $sort 顺序
22
 * @property bool $isShow 是否显示
23
 * @property bool $isEnabled 是否启用
24
 * @property array $metadata 元数据
25
 * @property string|null $createdAt
26
 * @property string|null $updatedAt
27
 * @property string $createdBy
28
 * @property string $updatedBy
29
 * @property string|null $deletedAt
30
 * @property string $deletedBy
31
 */
32
class AdminMenuModel extends BaseModel
33
{
34
    use ModelTrait;
35
    use SnowflakeTrait;
36
    use ReqQueryTrait;
37
    use SnowflakeTrait;
38
    use SoftDeleteTrait;
39
    use TreeTrait;
40

41
    protected $columns = [
42
        'metadata' => [
43
            'default' => [],
44
        ],
45
    ];
46

47
    protected $parentIdColumn = 'parentId';
48

49
    /**
50
     * @var array
51
     * @experimental
52
     */
53
    protected $privates = [];
54

55
    /**
56
     * @return string|null
57
     */
58
    public function getCode(): ?string
59
    {
60
        return $this->code;
18✔
61
    }
62

63
    /**
64
     * @param string|null $code
65
     * @return $this
66
     */
67
    public function setCode(?string $code): self
68
    {
69
        $this->code = $code;
30✔
70
        return $this;
30✔
71
    }
72

73
    /**
74
     * @return string|null
75
     */
76
    public function getLabel(): ?string
77
    {
78
        return $this->label;
×
79
    }
80

81
    /**
82
     * @param string|null $label
83
     * @return $this
84
     */
85
    public function setLabel(?string $label): self
86
    {
87
        $this->label = $label;
×
88
        return $this;
×
89
    }
90

91
    /**
92
     * @return string|null
93
     */
94
    public function getUrl(): ?string
95
    {
96
        return $this->url;
6✔
97
    }
98

99
    /**
100
     * @param string|null $url
101
     * @return $this
102
     */
103
    public function setUrl(?string $url): self
104
    {
105
        $this->url = $url;
6✔
106
        return $this;
6✔
107
    }
108

109
    /**
110
     * @param int $sort
111
     * @return $this
112
     */
113
    public function setSort(int $sort): self
114
    {
115
        $this->sort = $sort;
×
116
        return $this;
×
117
    }
118

119
    /**
120
     * @return int
121
     */
122
    public function getSort(): int
123
    {
124
        return $this->sort;
×
125
    }
126

127
    /**
128
     * @return string|null
129
     */
130
    public function getIcon(): ?string
131
    {
132
        return $this->icon;
×
133
    }
134

135
    /**
136
     * @param string|null $icon
137
     * @return $this
138
     */
139
    public function setIcon(?string $icon): self
140
    {
141
        $this->icon = $icon;
×
142
        return $this;
×
143
    }
144

145
    /**
146
     * @param bool $isShow
147
     * @return $this
148
     */
149
    public function setIsShow(bool $isShow): self
150
    {
151
        $this->isShow = $isShow;
×
152
        return $this;
×
153
    }
154

155
    /**
156
     * @return bool
157
     */
158
    public function isShow(): bool
159
    {
160
        return $this->isShow;
×
161
    }
162

163
    /**
164
     * @param string $name
165
     * @param mixed $value
166
     * @return $this
167
     */
168
    public function setMetadata(string $name, $value): self
169
    {
170
        $this->metadata[$name] = $value;
×
171
        return $this;
×
172
    }
173

174
    /**
175
     * @param string $name
176
     * @return mixed
177
     */
178
    public function getMetadata(string $name)
179
    {
180
        return $this->metadata[$name] ?? null;
×
181
    }
182

183
    /**
184
     * @param array<string> $apis
185
     * @return $this
186
     */
187
    public function setApis(array $apis): self
188
    {
189
        $this->privates['apis'] = $apis;
×
190
        return $this;
×
191
    }
192

193
    /**
194
     * @return array<string>
195
     */
196
    public function getApis(): array
197
    {
198
        return $this->privates['apis'] ?? [];
×
199
    }
200

201
    /**
202
     * @param string|self $code
203
     * @return $this
204
     */
205
    public function addChild($code = null): self
206
    {
207
        $item = $code instanceof self ? $code : self::new()->setCode($code);
30✔
208

209
        if (null === $code) {
30✔
210
            $this->children[] = $item;
24✔
211
            $item->setCode(array_key_last($this->children->attributes));
24✔
212
        } else {
213
            $this->children[$item->getCode()] = $item;
12✔
214
        }
215

216
        return $item;
30✔
217
    }
218

219
    /**
220
     * @param string $code
221
     * @return $this|null
222
     */
223
    public function getChild(string $code): ?self
224
    {
225
        return $this->children->hasColl($code) ? $this->children[$code] : null;
6✔
226
    }
227

228
    /**
229
     * Get or add child by the name
230
     *
231
     * @param string $code
232
     * @return $this
233
     */
234
    public function child(string $code): self
235
    {
236
        return $this->getChild($code) ?? $this->addChild($code);
×
237
    }
238

239
    /**
240
     * Remove child by the name
241
     *
242
     * @param string $code
243
     * @return $this
244
     */
245
    public function removeChild(string $code): self
246
    {
247
        unset($this->children[$code]);
24✔
248
        return $this;
24✔
249
    }
250

251
    /**
252
     * Remove child by the URL
253
     *
254
     * @param string|array $url
255
     * @return $this
256
     */
257
    public function removeChildByUrl($url): self
258
    {
259
        $url = (array) $url;
6✔
260
        foreach ($this->children as $code => $child) {
6✔
261
            if (in_array($child->getUrl(), $url, true)) {
6✔
262
                $this->removeChild($code);
6✔
263
            }
264
        }
265
        return $this;
6✔
266
    }
267

268
    /**
269
     * @return bool
270
     */
271
    public function hasChildren(): bool
272
    {
273
        return count($this->children) > 0;
×
274
    }
275

276
    /**
277
     * @return bool
278
     */
279
    public function isEmpty(): bool
280
    {
281
        return !$this->hasChildren() && !$this->url;
×
282
    }
283

284
    /**
285
     * @param int $level
286
     * @return array
287
     */
288
    public function toMenu(int $level = 1): array
289
    {
290
        if ($this->coll) {
×
291
            return $this->childrenToMenu($level);
×
292
        }
293

294
        return array_merge([
×
295
            'code' => $this->code,
×
296
            'label' => $this->label,
×
297
            'url' => $this->url,
×
298
            'sort' => $this->sort,
×
299
            'icon' => $this->icon,
×
300
            'isShow' => $this->isShow,
×
301
            'children' => $this->children->toMenu($level),
×
302
        ], $this->metadata);
×
303
    }
304

305
    /**
306
     * @param int $level
307
     * @return array
308
     * @coll
309
     */
310
    protected function childrenToMenu(int $level = 1): array
311
    {
312
        // Sort items
313
        usort($this->attributes, static function (self $childA, self $childB) {
×
314
            return $childA->getSort() < $childB->getSort();
×
315
        });
×
316

317
        $data = [];
×
318
        foreach ($this->attributes as $child) {
×
319
            // Ignore invalid level
320
            if ($child->level !== $level) {
×
321
                continue;
×
322
            }
323

324
            // Remote empty item
325
            if (!$child->isEmpty()) {
×
326
                $data[] = $child->toMenu($level + 1);
×
327
            }
328
        }
329

330
        return $data;
×
331
    }
332
}
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

© 2025 Coveralls, Inc