method to get all folders within the mailbox added

This commit is contained in:
Clivern 2017-08-14 16:20:25 +02:00
parent 84a937752a
commit 8f22ff80e3
2 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,25 @@
<?php
/**
* @author clivern <hello@clivern.com>
*/
namespace Clivern\Imap\Core\Exception;
/**
* Connection Error Class
*
* @package Clivern\Imap\Core\Exception
*/
class FolderNotExistException extends \Exception
{
/**
* Class Constructor
*
* @param string $folder
*/
public function __construct($folder = null)
{
parent::__construct(sprintf("Mailbox folder not exist: %s", $folder));
}
}

View file

@ -13,6 +13,7 @@ use Clivern\Imap\Core\Message\Header;
use Clivern\Imap\Core\Message\Actions;
use Clivern\Imap\Core\Message\Attachments;
use Clivern\Imap\Core\Message\Body;
use Clivern\Imap\Core\Exception\FolderNotExistException;
/**
* MailBox Class
@ -32,6 +33,12 @@ class MailBox
*/
protected $connection;
/**
* @var array
*/
protected $folders = [];
/**
* Constructor
*
@ -50,7 +57,12 @@ class MailBox
*/
public function setFolder($folder)
{
if( !in_array($folder, $this->getFolders()) ){
throw new FolderNotExistException($folder);
}
$this->folder = $folder;
return $this;
}
@ -125,4 +137,24 @@ class MailBox
$this->connection->survive($this->folder);
imap_expunge($this->connection->getStream());
}
/**
* Get Folders List
*
* @return array
*/
public function getFolders()
{
if( !empty($this->folders) ){
return $this->folders;
}
$this->folders = imap_getmailboxes($this->connection->getStream(), "{" . $this->connection->getServer() . "}", "*");
foreach ($this->folders as $key => $folder) {
$this->folders[$key] = str_replace("{" . $this->connection->getServer() . "}", "", $folder->name);
}
return $this->folders;
}
}