| 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; |
| 9 |
|
| 10 |
use ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer; |
| 11 |
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService; |
| 12 |
|
| 13 |
/** |
| 14 |
* Abstract base class representing a mime email's header. |
| 15 |
* |
| 16 |
* The base class sets up the header's consumer for parsing, sets the name of |
| 17 |
* the header, and calls the consumer to parse the header's value. |
| 18 |
* |
| 19 |
* AbstractHeader::getConsumer is an abstract method that must be overridden to |
| 20 |
* return an appropriate Consumer\AbstractConsumer type. |
| 21 |
* |
| 22 |
* @author Zaahid Bateson |
| 23 |
*/ |
| 24 |
abstract class AbstractHeader implements IHeader |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var string the name of the header |
| 28 |
*/ |
| 29 |
protected $name; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var IHeaderPart[] the header's parts (as returned from the consumer) |
| 33 |
*/ |
| 34 |
protected $parts; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string the raw value |
| 38 |
*/ |
| 39 |
protected $rawValue; |
| 40 |
|
| 41 |
/** |
| 42 |
* Assigns the header's name and raw value, then calls getConsumer and |
| 43 |
* setParseHeaderValue to extract a parsed value. |
| 44 |
* |
| 45 |
* @param ConsumerService $consumerService For parsing the value. |
| 46 |
* @param string $name Name of the header. |
| 47 |
* @param string $value Value of the header. |
| 48 |
*/ |
| 49 |
public function __construct(ConsumerService $consumerService, string $name, string $value) |
| 50 |
{ |
| 51 |
$this->name = $name; |
| 52 |
$this->rawValue = $value; |
| 53 |
|
| 54 |
$consumer = $this->getConsumer($consumerService); |
| 55 |
$this->setParseHeaderValue($consumer); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the header's Consumer |
| 60 |
* |
| 61 |
* @return AbstractConsumer |
| 62 |
*/ |
| 63 |
abstract protected function getConsumer(ConsumerService $consumerService); |
| 64 |
|
| 65 |
/** |
| 66 |
* Calls the consumer and assigns the parsed parts to member variables. |
| 67 |
* |
| 68 |
* The default implementation assigns the returned value to $this->part. |
| 69 |
* |
| 70 |
* @return static |
| 71 |
*/ |
| 72 |
protected function setParseHeaderValue(AbstractConsumer $consumer) |
| 73 |
{ |
| 74 |
$this->parts = $consumer($this->rawValue); |
| 75 |
return $this; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @return IHeaderPart[] |
| 80 |
*/ |
| 81 |
public function getParts() : array |
| 82 |
{ |
| 83 |
return $this->parts; |
| 84 |
} |
| 85 |
|
| 86 |
public function getValue() : ?string |
| 87 |
{ |
| 88 |
if (!empty($this->parts)) { |
| 89 |
return $this->parts[0]->getValue(); |
| 90 |
} |
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
public function getRawValue() : string |
| 95 |
{ |
| 96 |
return $this->rawValue; |
| 97 |
} |
| 98 |
|
| 99 |
public function getName() : string |
| 100 |
{ |
| 101 |
return $this->name; |
| 102 |
} |
| 103 |
|
| 104 |
public function __toString() : string |
| 105 |
{ |
| 106 |
return "{$this->name}: {$this->rawValue}"; |
| 107 |
} |
| 108 |
} |
| 109 |
|