Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility of loading Folder status #298

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/Connection/Protocols/ImapProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,17 @@ 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];
break;
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:
Expand Down
14 changes: 14 additions & 0 deletions src/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,6 +109,9 @@ class Folder {
*/
public $referral;

/** @var array */
public $status;

/**
* Folder constructor.
* @param Client $client
Expand Down Expand Up @@ -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
*
Expand Down