26 const TOKEN_REGEX =
'(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)';
29 private $parameters = [];
31 public function __construct(
string $name,
string $value, array $parameters = [])
33 parent::__construct($name, $value);
35 foreach ($parameters as $k => $v) {
39 if (
'content-type' !== strtolower($name)) {
59 $this->parameters = $parameters;
67 return $this->parameters;
72 $body = parent::getBodyAsString();
73 foreach ($this->parameters as $name => $value) {
74 if (
null !== $value) {
75 $body .=
'; '.$this->createParameter($name, $value);
88 protected function toTokens(
string $string =
null): array
90 $tokens = parent::toTokens(parent::getBodyAsString());
93 foreach ($this->parameters as $name => $value) {
94 if (
null !== $value) {
96 $tokens[\count($tokens) - 1] .=
';';
97 $tokens = array_merge($tokens, $this->
generateTokenLines(
' '.$this->createParameter($name, $value)));
107 private function createParameter(
string $name,
string $value): string
114 $firstLineOffset = 0;
117 if (!preg_match(
'/^'.self::TOKEN_REGEX.
'$/D', $value)) {
120 if (!preg_match(
'/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $value)) {
129 if ($encoded || \strlen($value) > $maxValueLength) {
130 if (
null !== $this->encoder) {
131 $value = $this->encoder->encodeString($origValue, $this->
getCharset(), $firstLineOffset, $maxValueLength);
139 $valueLines = $this->encoder ? explode(
"\r\n", $value) : [$value];
142 if (\count($valueLines) > 1) {
144 foreach ($valueLines as $i => $line) {
145 $paramLines[] = $name.
'*'.$i.$this->getEndOfParameterValue($line,
true, 0 === $i);
148 return implode(
";\r\n ", $paramLines);
150 return $name.$this->getEndOfParameterValue($valueLines[0], $encoded,
true);
159 private function getEndOfParameterValue(
string $value,
bool $encoded =
false,
bool $firstLine =
false): string
161 if (!preg_match(
'/^'.self::TOKEN_REGEX.
'$/D', $value)) {
162 $value =
'"'.$value.
'"';
168 $prepend =
'*='.$this->getCharset().
"'".$this->
getLanguage().
"'";
172 return $prepend.$value;