-
-
Notifications
You must be signed in to change notification settings - Fork 252
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
Implement bulk-move #306
Implement bulk-move #306
Changes from 4 commits
4735d31
abb0e5c
f19d9c7
2b8817b
d2541fe
81624fc
389dea6
e333801
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
use DateTimeInterface; | ||
use Ddeboer\Imap\Exception\InvalidSearchCriteriaException; | ||
use Ddeboer\Imap\Exception\MessageMoveException; | ||
use Ddeboer\Imap\Search\ConditionInterface; | ||
use Ddeboer\Imap\Search\LogicalOperator\All; | ||
|
||
|
@@ -118,35 +119,27 @@ public function getStatus(int $flags = null): \stdClass | |
/** | ||
* Bulk Set Flag for Messages. | ||
* | ||
* @param string $flag \Seen, \Answered, \Flagged, \Deleted, and \Draft | ||
* @param array|string $numbers Message numbers | ||
* @param string $flag \Seen, \Answered, \Flagged, \Deleted, and \Draft | ||
* @param array|MessageIteratorInterface|string $numbers Message numbers | ||
* | ||
* @return bool | ||
*/ | ||
public function setFlag(string $flag, $numbers): bool | ||
{ | ||
if (\is_array($numbers)) { | ||
$numbers = \implode(',', $numbers); | ||
} | ||
|
||
return \imap_setflag_full($this->resource->getStream(), (string) $numbers, $flag, \ST_UID); | ||
return \imap_setflag_full($this->resource->getStream(), $this->prepareMessageIds($numbers), $flag, \ST_UID); | ||
} | ||
|
||
/** | ||
* Bulk Clear Flag for Messages. | ||
* | ||
* @param string $flag \Seen, \Answered, \Flagged, \Deleted, and \Draft | ||
* @param array|string $numbers Message numbers | ||
* @param string $flag \Seen, \Answered, \Flagged, \Deleted, and \Draft | ||
* @param array|MessageIteratorInterface|string $numbers Message numbers | ||
* | ||
* @return bool | ||
*/ | ||
public function clearFlag(string $flag, $numbers): bool | ||
{ | ||
if (\is_array($numbers)) { | ||
$numbers = \implode(',', $numbers); | ||
} | ||
|
||
return \imap_clearflag_full($this->resource->getStream(), (string) $numbers, $flag, \ST_UID); | ||
return \imap_clearflag_full($this->resource->getStream(), $this->prepareMessageIds($numbers), $flag, \ST_UID); | ||
} | ||
|
||
/** | ||
|
@@ -247,4 +240,54 @@ public function getThread(): array | |
|
||
return false !== $tree ? $tree : []; | ||
} | ||
|
||
/** | ||
* Bulk move messages. | ||
* | ||
* @param array|MessageIteratorInterface|string $numbers Message numbers | ||
* @param MailboxInterface $mailbox Destination Mailbox to move the messages to | ||
* | ||
* @throws \Ddeboer\Imap\Exception\MessageMoveException | ||
*/ | ||
public function move($numbers, MailboxInterface $mailbox) | ||
{ | ||
if (!\imap_mail_move($this->resource->getStream(), $this->prepareMessageIds($numbers), $mailbox->getEncodedName(), \CP_UID)) { | ||
throw new MessageMoveException(\sprintf('Messages cannot be moved to "%s"', $mailbox->getName())); | ||
} | ||
} | ||
|
||
/** | ||
* Bulk copy messages. | ||
* | ||
* @param array|MessageIteratorInterface|string $numbers Message numbers | ||
* @param MailboxInterface $mailbox Destination Mailbox to copy the messages to | ||
* | ||
* @throws \Ddeboer\Imap\Exception\MessageMoveException | ||
*/ | ||
public function copy($numbers, MailboxInterface $mailbox) | ||
{ | ||
if (!\imap_mail_copy($this->resource->getStream(), $this->prepareMessageIds($numbers), $mailbox->getEncodedName(), \CP_UID)) { | ||
throw new MessageMoveException(\sprintf('Messages cannot be copied to "%s"', $mailbox->getName())); | ||
} | ||
} | ||
|
||
/** | ||
* Prepare message ids for the use with bulk functions. | ||
* | ||
* @param array|MessageIteratorInterface|string $messageIds Message numbers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right. fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or not. This is now breaking the build since the static analyser complains. I could fix this by typehinting the returned variable in the test - but is that such a good idea?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The bad thing is |
||
* | ||
* @return string | ||
*/ | ||
private function prepareMessageIds($messageIds): string | ||
{ | ||
if ($messageIds instanceof MessageIterator) { | ||
$messageIds = $messageIds->getArrayCopy(); | ||
} | ||
|
||
if (\is_array($messageIds)) { | ||
$messageIds = \implode(',', $messageIds); | ||
} | ||
|
||
return (string) $messageIds; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MessageCopyException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed too.