Skip to content

Commit eb674ad

Browse files
authored
Merge pull request #102 from dcblogdev/folders-find-by-name
Folders find by name
2 parents 650193d + 16a789e commit eb674ad

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

docs/v3/msgraph/emails.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ MsGraph::emails();
1414

1515
Return a list of emails
1616

17+
If no options are set emails are loaded from the inbox folder.
18+
1719
```php
18-
MsGraph::emails()->get();
20+
MsGraph::emails()->get($folderIdOrName = 'Inbox', $params = []);
1921
```
2022

21-
By default, only 10 emails are returned this can be changed by either using GET requests or pass an array of option to get()
23+
By default, only 25 emails are returned this can be changed by either using GET requests or pass an array of option to get()
2224

2325
Option 1: GET Request
2426

@@ -34,21 +36,21 @@ The default array that is used internally is below, you can override these optio
3436

3537
```php
3638
[
37-
"\$orderby" => "displayName",
38-
"\$top" => $top,
39-
"\$skip" => $skip,
40-
"\$count" => "true",
39+
'$orderby' => "displayName",
40+
'$top' => $top,
41+
'$skip' => $skip,
42+
'$count' => "true",
4143
]
4244
```
4345

4446
This would look like this:
4547

4648
```php
47-
MsGraph::emails()->get([
48-
"\$orderby" => "displayName",
49-
"\$top" => 15,
50-
"\$skip" => 0,
51-
"\$count" => "true",
49+
MsGraph::emails()->get('Inbox', [
50+
'$orderby' => "displayName",
51+
'$top' => 15,
52+
'$skip' => 0,
53+
'$count' => "true",
5254
]);
5355
```
5456

@@ -259,6 +261,12 @@ MsGraph::emails()->folders()->get(array $params = [], bool $sort = false, array
259261
MsGraph::emails()->folders()->find($id)
260262
```
261263

264+
## Get folder by name
265+
266+
```php
267+
MsGraph::emails()->folders()->findByName($name)
268+
```
269+
262270
## Create folder
263271

264272
```php

src/Resources/Emails/Emails.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Dcblogdev\MsGraph\Resources\Emails;
44

55
use Dcblogdev\MsGraph\Facades\MsGraph;
6+
use Dcblogdev\MsGraph\Validators\GraphQueryValidator;
67
use Exception;
78

89
class Emails extends MsGraph
@@ -114,42 +115,39 @@ public function skip(string $skip): static
114115
/**
115116
* @throws Exception
116117
*/
117-
public function get(string $folderId = '', array $params = []): array
118+
public function get(string $folderIdOrName = 'Inbox', array $params = []): array
118119
{
120+
GraphQueryValidator::validate($params);
121+
119122
$top = request('top', $this->top);
120123
$skip = request('skip', $this->skip);
121124

122-
if ($top === null) {
123-
$top = 100;
125+
if ($top === '') {
126+
$top = 25;
124127
}
125128

126-
if ($skip === null) {
129+
if ($skip === '') {
127130
$skip = 0;
128131
}
129132

130133
if ($params === []) {
131-
$params = http_build_query([
134+
$params = [
132135
'$top' => $top,
133136
'$skip' => $skip,
134137
'$count' => 'true',
135-
]);
136-
} else {
137-
$params = http_build_query($params);
138+
];
138139
}
139140

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+
}
148146

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));
151149
} else {
152-
throw new Exception('email folder not found');
150+
throw new Exception('Email folder not found');
153151
}
154152
}
155153

@@ -339,4 +337,10 @@ protected function prepareEmail(): array
339337

340338
return $envelope;
341339
}
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+
}
342346
}

src/Resources/Emails/Folders.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public function find(string $id): array
3636
return MsGraph::get('me/mailFolders/'.$id);
3737
}
3838

39+
public function findByName(string $name): array
40+
{
41+
$response = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$name')");
42+
return $response['value'][0] ?? [];
43+
}
44+
3945
public function store(array $data): array
4046
{
4147
EmailFolderStoreValidator::validate($data);

0 commit comments

Comments
 (0)