|
3 | 3 | namespace Dcblogdev\MsGraph\Resources\Emails; |
4 | 4 |
|
5 | 5 | use Dcblogdev\MsGraph\Facades\MsGraph; |
| 6 | +use Dcblogdev\MsGraph\Validators\GraphQueryValidator; |
6 | 7 | use Exception; |
7 | 8 |
|
8 | 9 | class Emails extends MsGraph |
@@ -114,42 +115,39 @@ public function skip(string $skip): static |
114 | 115 | /** |
115 | 116 | * @throws Exception |
116 | 117 | */ |
117 | | - public function get(string $folderId = '', array $params = []): array |
| 118 | + public function get(string $folderIdOrName = 'Inbox', array $params = []): array |
118 | 119 | { |
| 120 | + GraphQueryValidator::validate($params); |
| 121 | + |
119 | 122 | $top = request('top', $this->top); |
120 | 123 | $skip = request('skip', $this->skip); |
121 | 124 |
|
122 | | - if ($top === null) { |
123 | | - $top = 100; |
| 125 | + if ($top === '') { |
| 126 | + $top = 25; |
124 | 127 | } |
125 | 128 |
|
126 | | - if ($skip === null) { |
| 129 | + if ($skip === '') { |
127 | 130 | $skip = 0; |
128 | 131 | } |
129 | 132 |
|
130 | 133 | if ($params === []) { |
131 | | - $params = http_build_query([ |
| 134 | + $params = [ |
132 | 135 | '$top' => $top, |
133 | 136 | '$skip' => $skip, |
134 | 137 | '$count' => 'true', |
135 | | - ]); |
136 | | - } else { |
137 | | - $params = http_build_query($params); |
| 138 | + ]; |
138 | 139 | } |
139 | 140 |
|
140 | | - $folder = $folderId == '' ? 'Inbox' : $folderId; |
141 | | - |
142 | | - // get inbox from folders list |
143 | | - $folder = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$folder')"); |
144 | | - |
145 | | - if (isset($folder['value'][0])) { |
146 | | - // folder id |
147 | | - $folderId = $folder['value'][0]['id']; |
| 141 | + if ($this->isId($folderIdOrName)) { |
| 142 | + $folder = MsGraph::emails()->folders()->find($folderIdOrName); |
| 143 | + } else { |
| 144 | + $folder = MsGraph::emails()->folders()->findByName($folderIdOrName); |
| 145 | + } |
148 | 146 |
|
149 | | - // get messages from folderId |
150 | | - return MsGraph::get("me/mailFolders/$folderId/messages?".$params); |
| 147 | + if ($folder !== []) { |
| 148 | + return MsGraph::get("me/mailFolders/".$folder['id']."/messages?".http_build_query($params)); |
151 | 149 | } else { |
152 | | - throw new Exception('email folder not found'); |
| 150 | + throw new Exception('Email folder not found'); |
153 | 151 | } |
154 | 152 | } |
155 | 153 |
|
@@ -339,4 +337,10 @@ protected function prepareEmail(): array |
339 | 337 |
|
340 | 338 | return $envelope; |
341 | 339 | } |
| 340 | + |
| 341 | + private function isId(string $value): bool |
| 342 | + { |
| 343 | + // IDs are long, contain uppercase/lowercase letters, numbers, hyphens, dots, underscores, and end with '=' |
| 344 | + return preg_match('/^[A-Za-z0-9\-_]+={0,2}$/', $value) && strlen($value) > 50; |
| 345 | + } |
342 | 346 | } |
0 commit comments