From 3e5a03ee83040b5ddea5ee4ea46a30844857a8d5 Mon Sep 17 00:00:00 2001 From: Szymon Janaczek Date: Thu, 13 Oct 2022 13:49:10 +0200 Subject: [PATCH 1/2] Added missing param. --- src/Connection/Protocols/ImapProtocol.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Connection/Protocols/ImapProtocol.php b/src/Connection/Protocols/ImapProtocol.php index f1de8820..1a3c2ad2 100644 --- a/src/Connection/Protocols/ImapProtocol.php +++ b/src/Connection/Protocols/ImapProtocol.php @@ -508,7 +508,7 @@ public function examineOrSelect(string $command = 'EXAMINE', string $folder = 'I switch ($tokens[1]) { case 'EXISTS': case 'RECENT': - $result[strtolower($tokens[1])] = $tokens[0]; + $result[strtolower($tokens[1])] = (int)$tokens[0]; break; case '[UIDVALIDITY': $result['uidvalidity'] = (int)$tokens[2]; @@ -516,6 +516,9 @@ public function examineOrSelect(string $command = 'EXAMINE', string $folder = 'I case '[UIDNEXT': $result['uidnext'] = (int)$tokens[2]; break; + case '[UNSEEN': + $result['unseen'] = (int)$tokens[2]; + break; case '[NONEXISTENT]': throw new RuntimeException("folder doesn't exist"); default: From e7422a068a92604914b41fc03e4b887f8212bef7 Mon Sep 17 00:00:00 2001 From: Szymon Janaczek Date: Thu, 13 Oct 2022 13:51:46 +0200 Subject: [PATCH 2/2] Added getting folder with its status possibility. --- src/Client.php | 40 ++++++++++++++++++++++++++++++++++++++++ src/Folder.php | 14 ++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/Client.php b/src/Client.php index 3721f8e7..1799147d 100755 --- a/src/Client.php +++ b/src/Client.php @@ -495,6 +495,46 @@ public function getFolders(bool $hierarchical = true, string $parent_folder = nu } } + /** + * Get folders list. + * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array. + * + * @param boolean $hierarchical + * @param string|null $parent_folder + * + * @return FolderCollection + * @throws ConnectionFailedException + * @throws FolderFetchingException + * @throws Exceptions\RuntimeException + */ + public function getFoldersWithStatus(bool $hierarchical = true, string $parent_folder = null): FolderCollection { + $this->checkConnection(); + $folders = FolderCollection::make([]); + + $pattern = $parent_folder.($hierarchical ? '%' : '*'); + $items = $this->connection->folders('', $pattern); + + if(is_array($items)){ + foreach ($items as $folder_name => $item) { + $folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]); + + if ($hierarchical && $folder->hasChildren()) { + $pattern = $folder->full_name.$folder->delimiter.'%'; + + $children = $this->getFolders(true, $pattern); + $folder->setChildren($children); + } + + $folder->loadStatus(); + $folders->push($folder); + } + + return $folders; + }else{ + throw new FolderFetchingException("failed to fetch any folders"); + } + } + /** * Open a given folder. * @param string $folder_path diff --git a/src/Folder.php b/src/Folder.php index ef42fcb1..a93209a2 100755 --- a/src/Folder.php +++ b/src/Folder.php @@ -15,6 +15,7 @@ use Carbon\Carbon; use Webklex\PHPIMAP\Exceptions\ConnectionFailedException; use Webklex\PHPIMAP\Exceptions\NotSupportedCapabilityException; +use Webklex\PHPIMAP\Exceptions\RuntimeException; use Webklex\PHPIMAP\Query\WhereQuery; use Webklex\PHPIMAP\Support\FolderCollection; use Webklex\PHPIMAP\Traits\HasEvents; @@ -108,6 +109,9 @@ class Folder { */ public $referral; + /** @var array */ + public $status; + /** * Folder constructor. * @param Client $client @@ -426,6 +430,16 @@ public function getStatus(): array { return $this->examine(); } + /** + * @throws RuntimeException + * @throws ConnectionFailedException + */ + public function loadStatus(): Folder + { + $this->status = $this->getStatus(); + return $this; + } + /** * Examine the current folder *