Folders.php
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Lib\Imap\Parse\Folder;
/**
* 文件夹
* @author:dc
* @time 2024/9/20 9:33
* Class Folder
* @package Lib\Imap\Parse
*/
class Folders{
/**
* @var Folder[]
*/
protected array $item = [];
/**
* 原始头信息
* @var string
*/
protected array $raw = [];
/**
* Header constructor.
* @param array $raw
*/
public function __construct(array $raw)
{
$this->raw = $raw;
$this->parse();
}
/**
* 解析目录
* @author:dc
* @time 2024/9/20 10:07
*/
private function parse(){
foreach ($this->raw as $item){
// 解析源数据
if(preg_match('/^\*\sLIST\s\(([\\a-z\s]{0,})\)\s\"(.*)\"\s\"?(.*)\"?$/Ui',$item,$m)){
if($m[1]){
$flags = explode(' ',$m[1]);
$flags = array_map(function ($v){
$v = trim($v);
$v = trim($v,'\\');
return $v;
},$flags);
}else{
$flags = [];
}
$this->item[] = new Folder(trim(trim($m[3]),'"'),$flags, !str_contains($m[1], 'NoSelect'), $this);
}
}
}
/**
* 获取顶级栏目 一级目录
* @return Folder[]
* @author:dc
* @time 2024/9/20 11:39
*/
public function getTopFolder():array {
return array_filter($this->item,function ($folder){
return substr_count($folder->folder,'/') == 0;
});
}
/**
* 查找当前目录的下级目录
* @param Folder $folder
* @return Folder[]
* @author:dc
* @time 2024/9/19 17:54
*/
public function findChild(Folder $folder):array {
$fs = [];
foreach ($this->item as $item){
// 跳过相同目录
if($folder->folder == $item->folder) continue;
if(strpos($item->folder,$folder->folder) !== 0) continue;
$f = substr($item->folder,strlen($folder->folder),999);
$f = ltrim($f,'/');
if(substr_count($f,'/') == 0){
// $item['child'] = $this->findChild($item['folder']);
$fs[] = $item;
}
}
return $fs;
}
/**
* 获取所有目录 list 不是tree结构
* @return Folder[]
* @author:dc
* @time 2024/9/20 11:43
*/
public function all():array{
return $this->item;
}
}