| 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 |
use ZBateson\MailMimeParser\Header\Part\ParameterPart; |
| 13 |
|
| 14 |
/** |
| 15 |
* Represents a header containing an optional main value part and subsequent |
| 16 |
* name/value pairs. |
| 17 |
* |
| 18 |
* If header doesn't contain a non-parameterized 'main' value part, 'getValue()' |
| 19 |
* will return the value of the first parameter. |
| 20 |
* |
| 21 |
* For example: 'Content-Type: text/html; charset=utf-8; name=test.ext' |
| 22 |
* |
| 23 |
* The 'text/html' portion is considered the 'main' value, and 'charset' and |
| 24 |
* 'name' are added as parameterized name/value pairs. |
| 25 |
* |
| 26 |
* With the Autocrypt header, there is no main value portion, for example: |
| 27 |
* 'Autocrypt: addr=zb@example.com; keydata=b64-data' |
| 28 |
* |
| 29 |
* In that example, calling ```php $header->getValue() ``` would return |
| 30 |
* 'zb@example.com', as would calling ```php $header->getValueFor('addr'); ```. |
| 31 |
* |
| 32 |
* @author Zaahid Bateson |
| 33 |
*/ |
| 34 |
class ParameterHeader extends AbstractHeader |
| 35 |
{ |
| 36 |
/** |
| 37 |
* @var ParameterPart[] key map of lower-case parameter names and associated |
| 38 |
* ParameterParts. |
| 39 |
*/ |
| 40 |
protected $parameters = []; |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns a ParameterConsumer. |
| 44 |
* |
| 45 |
* @return Consumer\AbstractConsumer |
| 46 |
*/ |
| 47 |
protected function getConsumer(ConsumerService $consumerService) |
| 48 |
{ |
| 49 |
return $consumerService->getParameterConsumer(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Overridden to assign ParameterParts to a map of lower-case parameter |
| 54 |
* names to ParameterParts. |
| 55 |
* |
| 56 |
* |
| 57 |
* @return static |
| 58 |
*/ |
| 59 |
protected function setParseHeaderValue(AbstractConsumer $consumer) |
| 60 |
{ |
| 61 |
parent::setParseHeaderValue($consumer); |
| 62 |
foreach ($this->parts as $part) { |
| 63 |
if ($part instanceof ParameterPart) { |
| 64 |
$this->parameters[\strtolower($part->getName())] = $part; |
| 65 |
} |
| 66 |
} |
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns true if a parameter exists with the passed name. |
| 72 |
* |
| 73 |
* @param string $name The parameter to look up. |
| 74 |
*/ |
| 75 |
public function hasParameter(string $name) : bool |
| 76 |
{ |
| 77 |
return isset($this->parameters[\strtolower($name)]); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the value of the parameter with the given name, or $defaultValue |
| 82 |
* if not set. |
| 83 |
* |
| 84 |
* @param string $name The parameter to retrieve. |
| 85 |
* @param string $defaultValue Optional default value (defaulting to null if |
| 86 |
* not provided). |
| 87 |
* @return string|null The parameter's value. |
| 88 |
*/ |
| 89 |
public function getValueFor(string $name, ?string $defaultValue = null) : ?string |
| 90 |
{ |
| 91 |
if (!$this->hasParameter($name)) { |
| 92 |
return $defaultValue; |
| 93 |
} |
| 94 |
return $this->parameters[\strtolower($name)]->getValue(); |
| 95 |
} |
| 96 |
} |
| 97 |
|