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

Mailbox: add renaming capability #540

Merged
merged 8 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
9 changes: 9 additions & 0 deletions src/Exception/RenameMailboxException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Ddeboer\Imap\Exception;

final class RenameMailboxException extends AbstractException
{
}
23 changes: 23 additions & 0 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Ddeboer\Imap\Exception\InvalidSearchCriteriaException;
use Ddeboer\Imap\Exception\MessageCopyException;
use Ddeboer\Imap\Exception\MessageMoveException;
use Ddeboer\Imap\Exception\RenameMailboxException;
use Ddeboer\Imap\Search\ConditionInterface;
use Ddeboer\Imap\Search\LogicalOperator\All;

Expand Down Expand Up @@ -41,6 +42,28 @@ public function getName(): string
return $this->name;
}

public function renameTo(string $name = null): bool
{
if (null === $name) {
return false;
}
$encodedName = \mb_convert_encoding($name, 'UTF7-IMAP', 'UTF-8');
$oldFullName = $this->getFullEncodedName();
$newFullName = \preg_replace('/' . \preg_quote(\mb_convert_encoding($this->name, 'UTF7-IMAP', 'UTF-8')) . '$/', $encodedName, $oldFullName);
if (null === $newFullName) {
return false;
}

$return = \imap_renamemailbox($this->resource->getStream(), $oldFullName, $newFullName);
if (false === $return) {
throw new RenameMailboxException('Could not rename mailbox');
}
$this->name = $name;
$this->info->name = $newFullName;

return true;
}

public function getEncodedName(): string
{
/** @var string $name */
Expand Down
5 changes: 5 additions & 0 deletions src/MailboxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ interface MailboxInterface extends \Countable, \IteratorAggregate
*/
public function getName(): string;

/**
* Set new mailbox name.
*/
public function renameTo(string $name = null): bool;

/**
* Get mailbox encoded path.
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abstract class AbstractTest extends TestCase
public const SPECIAL_CHARS = 'A_\\|!"£$%&()=?àèìòùÀÈÌÒÙ<>-@#[]_ß_б_π_€_✔_你_يد_Z_';

protected ?string $mailboxName;
protected ?string $altName;

final protected function getConnection(): ConnectionInterface
{
Expand All @@ -40,6 +41,7 @@ final protected function createMailbox(ConnectionInterface $connection = null):
{
$connection = $connection ?? $this->getConnection();
$this->mailboxName = \uniqid('mailbox_' . self::SPECIAL_CHARS);
$this->altName = \uniqid('mailbox_' . self::SPECIAL_CHARS);

return $connection->createMailbox($this->mailboxName);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public function testGetName(): void
static::assertSame($this->mailboxName, $this->mailbox->getName());
}

public function testRenameTo(): void
{
static::assertNotSame($this->mailboxName, $this->altName);

static::assertTrue($this->mailbox->renameTo($this->altName));
static::assertSame($this->altName, $this->mailbox->getName());

static::assertTrue($this->mailbox->renameTo($this->mailboxName));
static::assertSame($this->mailboxName, $this->mailbox->getName());
}

public function testGetFullEncodedName(): void
{
static::assertIsString($this->mailboxName);
Expand Down