Skip to content

[Serializer] added the ability to pass \ArrayObject to context.callbacks #33106

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);

if (isset($this->defaultContext[self::CALLBACKS])) {
if (!\is_array($this->defaultContext[self::CALLBACKS])) {
throw new InvalidArgumentException(sprintf('The "%s" default context option must be an array of callables.', self::CALLBACKS));
if (!\is_array($this->defaultContext[self::CALLBACKS]) && !$this->defaultContext[self::CALLBACKS] instanceof \ArrayObject) {
throw new InvalidArgumentException(sprintf('The "%s" default context option must be an array or an \ArrayObject of callables.', self::CALLBACKS));
}

foreach ($this->defaultContext[self::CALLBACKS] as $attribute => $callback) {
Expand Down Expand Up @@ -230,7 +230,7 @@ public function setCircularReferenceHandler(callable $circularReferenceHandler)
*
* @throws InvalidArgumentException if a non-callable callback is set
*/
public function setCallbacks(array $callbacks)
public function setCallbacks($callbacks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC break. And I'm not sure it is worth it to change anyway because this method is deprecated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests fall without this change because of the new case "Callbacks as ArrayObject".

Should I rewrite tests of this method to exclude this case from it?
Or is it better to do the type checking in the body of the method (is_array($callbacks) || $callbacks instanceof \ArrayObject) to prevent any illegal usage?

{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "callbacks" key of the context instead.', __METHOD__), E_USER_DEPRECATED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public function normalize($object, $format = null, array $context = [])
}

if (isset($context[self::CALLBACKS])) {
if (!\is_array($context[self::CALLBACKS])) {
throw new InvalidArgumentException(sprintf('The "%s" context option must be an array of callables.', self::CALLBACKS));
if (!\is_array($context[self::CALLBACKS]) && !$context[self::CALLBACKS] instanceof \ArrayObject) {
throw new InvalidArgumentException(sprintf('The "%s" context option must be an array or an \ArrayObject of callables.', self::CALLBACKS));
}

foreach ($context[self::CALLBACKS] as $attribute => $callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ public function provideCallbacks()
[new CallbacksObject(), new CallbacksObject()],
['bar' => 2],
],
'Callbacks as ArrayObject' => [
new class() extends \ArrayObject {
public function offsetExists($index)
{
return true;
}

public function offsetGet($index)
{
return function () use ($index) {
return $index;
};
}
},
'baz',
['bar' => 'bar'],
],
];
}

Expand Down