zbateson /
mail-mime-parser
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the ZBateson\MailMimeParser project. |
||
| 4 | * |
||
| 5 | * @license http://opensource.org/licenses/bsd-license.php BSD |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace ZBateson\MailMimeParser\Header\Part; |
||
| 9 | |||
| 10 | use Psr\Log\LoggerInterface; |
||
| 11 | use ZBateson\MbWrapper\MbWrapper; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Represents a mime header comment -- text in a structured mime header |
||
| 15 | * value existing within parentheses. |
||
| 16 | * |
||
| 17 | * @author Zaahid Bateson |
||
| 18 | */ |
||
| 19 | class CommentPart extends ContainerPart |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var HeaderPartFactory used to create intermediate parts. |
||
| 23 | */ |
||
| 24 | protected HeaderPartFactory $partFactory; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string the contents of the comment |
||
| 28 | */ |
||
| 29 | protected string $comment; |
||
| 30 | |||
| 31 | 17 | public function __construct( |
|
| 32 | LoggerInterface $logger, |
||
| 33 | MbWrapper $charsetConverter, |
||
| 34 | HeaderPartFactory $partFactory, |
||
| 35 | array $children |
||
| 36 | ) { |
||
| 37 | 17 | $this->partFactory = $partFactory; |
|
| 38 | 17 | parent::__construct($logger, $charsetConverter, $children); |
|
| 39 | 17 | $this->comment = $this->value; |
|
| 40 | 17 | $this->value = ''; |
|
| 41 | 17 | $this->isSpace = true; |
|
| 42 | 17 | $this->canIgnoreSpacesBefore = true; |
|
| 43 | 17 | $this->canIgnoreSpacesAfter = true; |
|
| 44 | } |
||
| 45 | |||
| 46 | 17 | protected function getValueFromParts(array $parts) : string |
|
| 47 | { |
||
| 48 | 17 | $partFactory = $this->partFactory; |
|
| 49 | 17 | return parent::getValueFromParts(\array_map( |
|
| 50 | 17 | function($p) use ($partFactory) { |
|
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 51 | 17 | if ($p instanceof CommentPart) { |
|
| 52 | 1 | return $partFactory->newQuotedLiteralPart([$partFactory->newToken('(' . $p->getComment() . ')')]); |
|
| 53 | 17 | } elseif ($p instanceof QuotedLiteralPart) { |
|
| 54 | 1 | return $partFactory->newQuotedLiteralPart([$partFactory->newToken('"' . \str_replace('(["\\])', '\$1', $p->getValue()) . '"')]); |
|
| 55 | } |
||
| 56 | 17 | return $p; |
|
| 57 | 17 | }, |
|
| 58 | 17 | $parts |
|
| 59 | 17 | )); |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns the comment's text. |
||
| 64 | */ |
||
| 65 | 2 | public function getComment() : string |
|
| 66 | { |
||
| 67 | 2 | return $this->comment; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns an empty string. |
||
| 72 | */ |
||
| 73 | 2 | public function getValue() : string |
|
| 74 | { |
||
| 75 | 2 | return ''; |
|
| 76 | } |
||
| 77 | } |
||
| 78 |