-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move imap flag encoding to helper
Signed-off-by: Daniel Kesselberg <[email protected]>
- Loading branch information
Showing
5 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Exception; | ||
|
||
use Exception; | ||
|
||
class ImapFlagEncodingException extends Exception { | ||
public static function create($label): ImapFlagEncodingException { | ||
return new self( | ||
'Failed to convert the given label "' . $label . '" to UTF7-IMAP', | ||
0, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\IMAP; | ||
|
||
use OCA\Mail\Exception\ImapFlagEncodingException; | ||
|
||
class ImapFlag { | ||
/** | ||
* @throws ImapFlagEncodingException | ||
*/ | ||
public function create(string $label): string { | ||
$flag = str_replace(' ', '_', $label); | ||
$flag = mb_convert_encoding($flag, 'UTF7-IMAP', 'UTF-8'); | ||
|
||
if ($flag === false) { | ||
throw ImapFlagEncodingException::create($label); | ||
} | ||
|
||
return '$' . strtolower(mb_strcut($flag, 0, 63)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Unit\IMAP; | ||
|
||
use ChristophWurst\Nextcloud\Testing\TestCase; | ||
use OCA\Mail\IMAP\ImapFlag; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
||
class ImapFlagTest extends TestCase { | ||
|
||
private ImapFlag $imapFlag; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->imapFlag = new ImapFlag(); | ||
} | ||
|
||
/** | ||
* @dataProvider dataCreate | ||
*/ | ||
public function testCreate(string $label, string $expected): void { | ||
$actual = $this->imapFlag->create($label); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function dataCreate(): array { | ||
return [ | ||
'umlauts and lowercase' => [ | ||
'Test ÄÖÜ', | ||
'$test_&amqa1gdc-' | ||
], | ||
'maximum 63 characters' => [ | ||
'1234567890123456789012345678901234567890123456789012345678901234', | ||
'$123456789012345678901234567890123456789012345678901234567890123', | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters