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

miaoxing / admin / 14778320908

01 May 2025 03:41PM UTC coverage: 20.332%. Remained the same
14778320908

push

github

semantic-release-bot
chore(release): publish

See CHANGELOG.md for more details.

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 ReqQueryTrait;
36
    use SnowflakeTrait;
37
    use SoftDeleteTrait;
38
    use TreeTrait;
39

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

46
    protected $parentIdColumn = 'parentId';
47

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

215
        return $item;
30✔
216
    }
217

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

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

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

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

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

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

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

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

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

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

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

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