From 4d130ef8e3ff942241b11de2e0f91f6bd33de3e5 Mon Sep 17 00:00:00 2001 From: Katwe Kibuye Date: Sat, 11 Nov 2017 18:33:53 +0200 Subject: [PATCH 1/3] added option for setting fetched emails \\seen flag to seen --- README.md | 12 ++++++++++++ src/Client.php | 52 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5ffa55d..bddcdd9 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,18 @@ $query = QueryBuilder::create('INBOX.Sent') $emails = $client->getEmails($query); ``` +Both `getEmails()` and `getEmailIds()` can take a second optional `boolean` argument. it can be set to fault to indicate change of flag of matched emails to seen. + +```php +$query = QueryBuilder::create('INBOX.Sent') + ->youngerThan(3600) + ->flagSeen(true) + + ->getQuery(); + +$emails = $client->getEmails($query, false); // set to "false" fetched emails' state will be set to seen "true" no change of state will occur. default is true +``` + ### Reading folders ```php diff --git a/src/Client.php b/src/Client.php index 6582435..576d417 100644 --- a/src/Client.php +++ b/src/Client.php @@ -25,6 +25,12 @@ class Client */ private $emailFactory; + /** + * + * @var boolean + */ + private $peek; + public function __construct(Horde_Imap_Client_Socket $hordeClient, EmailFactory $emailFactory = null) { $this->hordeClient = $hordeClient; @@ -63,25 +69,25 @@ public function getFolders() : array return array_keys($this->hordeClient->listMailboxes('*')); } - /** + /** * Finds the emails matching the query. If $query is null, then it will fetch the emails in the inbox. - * + * @param Query $query Description + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return Email[] */ - public function getEmails(Query $query = null) : array - { + public function getEmails(Query $query = null, $peek = true): array { $hordeQuery = new Horde_Imap_Client_Search_Query(); $query = $query ?: new Query; if ($query->getYoungerThan() !== null) { $hordeQuery->intervalSearch( - $query->getYoungerThan(), - Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER + $query->getYoungerThan(), Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER ); } - + $this->setPeek($peek); $this->setFlags($hordeQuery, $query); + return $this->searchAndFetch($query->getFolder(), $hordeQuery); } @@ -89,31 +95,36 @@ public function getEmails(Query $query = null) : array * Finds the email Ids matching the query. If $query is null, then it will fetch the email Ids in the inbox. * * This method is obviously more efficient than getEmails() if you want to synchronize local mails. - * + * + * @param Query $query Description + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return string[] */ - public function getEmailIds(Query $query = null) : array - { + public function getEmailIds(Query $query = null, $peek = true): array { $hordeQuery = new Horde_Imap_Client_Search_Query(); $query = $query ?: new Query; if ($query->getYoungerThan() !== null) { $hordeQuery->intervalSearch( - $query->getYoungerThan(), - Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER + $query->getYoungerThan(), Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER ); } + $this->setPeek($peek); $this->setFlags($hordeQuery, $query); + return $this->search($query->getFolder(), $hordeQuery); } /** + * @param String $id Description + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true + * @param String $folder the folder to get email from * @return Email|null Returns null if the email was not found. */ - public function getEmailFromId(string $id, string $folder = 'INBOX') - { + public function getEmailFromId(string $id, bool $peek = true, string $folder = 'INBOX') { + $this->setPeek($peek); $emails = $this->fetchEmails($folder, [$id]); return (count($emails) > 0) ? $emails[0] : null; @@ -121,10 +132,12 @@ public function getEmailFromId(string $id, string $folder = 'INBOX') /** * @param string[] $ids + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true + * @param String $folder the folder to get emails from * @return Email[] */ - public function getEmailsFromId(array $ids, string $folder = 'INBOX') : array - { + public function getEmailsFromId(array $ids, bool $peek = true, string $folder = 'INBOX'): array { + $this->setPeek($peek); return $this->fetchEmails($folder, $ids); } @@ -220,4 +233,11 @@ private function setFlags(Horde_Imap_Client_Search_Query $hordeQuery,Query $quer } } } + /** + * + * @param boolean $peek + */ + public function setPeek($peek) { + $this->peek = $peek; + } } From 14bf8a0b8cefaa2dc45f4b7db7d1325a8788fd56 Mon Sep 17 00:00:00 2001 From: Katwe Kibuye Date: Sun, 12 Nov 2017 00:31:32 +0200 Subject: [PATCH 2/3] Fixed corrections as suggested by @mnapoli. --- README.md | 4 ++-- src/Client.php | 37 ++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index bddcdd9..e33464d 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ $query = QueryBuilder::create('INBOX.Sent') $emails = $client->getEmails($query); ``` -Both `getEmails()` and `getEmailIds()` can take a second optional `boolean` argument. it can be set to fault to indicate change of flag of matched emails to seen. +Both `getEmails()` and `getEmailIds()` can take a third optional `boolean` argument. it can be set to false to indicate change of flag for fetched emails to seen. ```php $query = QueryBuilder::create('INBOX.Sent') @@ -100,7 +100,7 @@ $query = QueryBuilder::create('INBOX.Sent') ->getQuery(); -$emails = $client->getEmails($query, false); // set to "false" fetched emails' state will be set to seen "true" no change of state will occur. default is true +$emails = $client->getEmails($query,'INBOX', false); // set to "false" fetched emails' state will be set to seen. "true" no change of state will occur. default is true ``` ### Reading folders diff --git a/src/Client.php b/src/Client.php index 576d417..1300216 100644 --- a/src/Client.php +++ b/src/Client.php @@ -71,23 +71,23 @@ public function getFolders() : array /** * Finds the emails matching the query. If $query is null, then it will fetch the emails in the inbox. - * @param Query $query Description * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return Email[] */ - public function getEmails(Query $query = null, $peek = true): array { + public function getEmails(Query $query = null, bool $peek = true): array + { $hordeQuery = new Horde_Imap_Client_Search_Query(); $query = $query ?: new Query; if ($query->getYoungerThan() !== null) { $hordeQuery->intervalSearch( - $query->getYoungerThan(), Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER + $query->getYoungerThan(), + Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER ); } $this->setPeek($peek); $this->setFlags($hordeQuery, $query); - return $this->searchAndFetch($query->getFolder(), $hordeQuery); } @@ -96,34 +96,34 @@ public function getEmails(Query $query = null, $peek = true): array { * * This method is obviously more efficient than getEmails() if you want to synchronize local mails. * - * @param Query $query Description * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return string[] */ - public function getEmailIds(Query $query = null, $peek = true): array { + public function getEmailIds(Query $query = null, bool $peek = true): array + { $hordeQuery = new Horde_Imap_Client_Search_Query(); $query = $query ?: new Query; if ($query->getYoungerThan() !== null) { $hordeQuery->intervalSearch( - $query->getYoungerThan(), Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER + $query->getYoungerThan(), + Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER ); } $this->setPeek($peek); $this->setFlags($hordeQuery, $query); - return $this->search($query->getFolder(), $hordeQuery); } /** - * @param String $id Description - * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @param String $folder the folder to get email from + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return Email|null Returns null if the email was not found. */ - public function getEmailFromId(string $id, bool $peek = true, string $folder = 'INBOX') { + public function getEmailFromId(string $id, string $folder = 'INBOX', bool $peek = true) + { $this->setPeek($peek); $emails = $this->fetchEmails($folder, [$id]); @@ -132,11 +132,12 @@ public function getEmailFromId(string $id, bool $peek = true, string $folder = ' /** * @param string[] $ids - * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @param String $folder the folder to get emails from + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return Email[] */ - public function getEmailsFromId(array $ids, bool $peek = true, string $folder = 'INBOX'): array { + public function getEmailsFromId(array $ids, string $folder = 'INBOX', bool $peek = true) : array + { $this->setPeek($peek); return $this->fetchEmails($folder, $ids); } @@ -191,7 +192,7 @@ private function fetchEmails(string $folder, array $ids) : array $query = new Horde_Imap_Client_Fetch_Query(); $query->envelope(); $query->fullText([ - 'peek' => true, + 'peek' => $this->getPeek(), ]); $query->flags(); @@ -237,7 +238,13 @@ private function setFlags(Horde_Imap_Client_Search_Query $hordeQuery,Query $quer * * @param boolean $peek */ - public function setPeek($peek) { + public function setPeek($peek) + { $this->peek = $peek; } + + public function getPeek() + { + return $this->peek; + } } From b8c98f17fb151690cec99748bd104c14d741dcf5 Mon Sep 17 00:00:00 2001 From: Katwe Kibuye Date: Sun, 12 Nov 2017 01:53:19 +0200 Subject: [PATCH 3/3] Fixed code to follow PSR-2 code formating standards --- src/Query/Query.php | 16 +++++++++++++--- src/Query/QueryBuilder.php | 18 ++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index 6affd64..a149382 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -19,8 +19,16 @@ class Query * @var int */ private $youngerThan; + private $flags = []; - public function getFolder() : string + const FLAG_ANSWERED = 'ANSWERED'; + const FLAG_DELETED = 'DELETED'; + const FLAG_DRAFT = 'DRAFT'; + const FLAG_FLAGED = 'FLAGGED'; + const FLAG_RECENT = 'RECENT'; + const FLAG_SEEN = 'SEEN'; + + public function getFolder(): string { return $this->folder; } @@ -46,11 +54,13 @@ public function setYoungerThan(int $youngerThan) $this->youngerThan = $youngerThan; } - public function setFlags($key,$value){ + public function setFlags($key,$value) + { $this->flags[$key] = $value; } - public function getFlags(){ + public function getFlags() + { return $this->flags; } diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 0443c51..253f451 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -43,32 +43,38 @@ public function youngerThan(int $interval) : self return $this; } - public function flagAnswered($value) { + public function flagAnswered($value) + { $this->query->setFlags(Query::FLAG_ANSWERED, $value); return $this; } - public function flagDeleted($value) { + public function flagDeleted($value) + { $this->query->setFlags(Query::FLAG_DELETED, $value); return $this; } - public function flagDraft($value) { + public function flagDraft($value) + { $this->query->setFlags(Query::FLAG_DRAFT, $value); return $this; } - public function flagFlaged($value) { + public function flagFlaged($value) + { $this->query->setFlags(Query::FLAG_FLAGED, $value); return $this; } - public function flagRecent($value) { + public function flagRecent($value) + { $this->query->setFlags(Query::FLAG_RECENT, $value); return $this; } - public function flagSeen($value) { + public function flagSeen($value) + { $this->query->setFlags(Query::FLAG_SEEN, $value); return $this; }