-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] deprecate HeaderBag::get() returning an array and add all($key) instead #32122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,10 +58,18 @@ public function __toString() | |
/** | ||
* Returns the headers. | ||
* | ||
* @param string|null $key The name of the headers to return or null to get them all | ||
* | ||
* @return array An array of headers | ||
*/ | ||
public function all() | ||
public function all(/*string $key = null*/) | ||
{ | ||
if (1 <= \func_num_args() && null !== $key = func_get_arg(0)) { | ||
$key = str_replace('_', '-', strtolower($key)); | ||
|
||
return $this->headers[$key] ?? []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't solve the problem in the linked issue (the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's fine to me, the proposed interface is a nice and consistent update |
||
} | ||
|
||
return $this->headers; | ||
} | ||
|
||
|
@@ -103,28 +111,21 @@ public function add(array $headers) | |
* | ||
* @param string $key The header name | ||
* @param string|null $default The default value | ||
* @param bool $first Whether to return the first value or all header values | ||
* | ||
* @return string|string[]|null The first header value or default value if $first is true, an array of values otherwise | ||
* @return string|null The first header value or default value | ||
*/ | ||
public function get($key, $default = null, $first = true) | ||
public function get($key, $default = null) | ||
Simperfit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$key = str_replace('_', '-', strtolower($key)); | ||
$headers = $this->all(); | ||
$headers = $this->all((string) $key); | ||
if (2 < \func_num_args()) { | ||
@trigger_error(sprintf('Passing a third argument to "%s()" is deprecated since Symfony 4.4, use method "all()" instead', __METHOD__), E_USER_DEPRECATED); | ||
|
||
if (!\array_key_exists($key, $headers)) { | ||
if (null === $default) { | ||
return $first ? null : []; | ||
if (!func_get_arg(2)) { | ||
return $headers; | ||
} | ||
|
||
return $first ? $default : [$default]; | ||
} | ||
|
||
if ($first) { | ||
return \count($headers[$key]) ? $headers[$key][0] : $default; | ||
} | ||
|
||
return $headers[$key]; | ||
return $headers[0] ?? $default; | ||
} | ||
|
||
/** | ||
|
@@ -181,7 +182,7 @@ public function has($key) | |
*/ | ||
public function contains($key, $value) | ||
{ | ||
return \in_array($value, $this->get($key, null, false)); | ||
return \in_array($value, $this->all((string) $key)); | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a no-go for me. It introduces another of #31317 which this PR tried to fix.
Depending on the arguments it returns an array of arrays or an array of strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's the same as on the Messenger Envelope but that was on my todo to fix as was a pain to work with.