Skip to content

Commit

Permalink
[User] Remove deprecated \Serializable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sad270 committed May 2, 2022
1 parent 50be917 commit 8ee908c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Sylius/Behat/spec/Service/SecurityServiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function it_logs_user_in(
) {
$shopUser->getRoles()->willReturn(['ROLE_USER']);
$shopUser->getPassword()->willReturn('xyz');
$shopUser->serialize()->willReturn('serialized_user');
$shopUser->__serialize()->willReturn(['serialized_user']);

$session->set('_security_shop', Argument::any())->shouldBeCalled();
$session->save()->shouldBeCalled();
Expand Down
30 changes: 22 additions & 8 deletions src/Sylius/Component/User/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ public function setEncoderName(?string $encoderName): void
/**
* The serialized data have to contain the fields used by the equals method and the username.
*/
public function serialize(): string
public function __serialize(): array
{
return serialize([
return [
$this->password,
$this->salt,
$this->usernameCanonical,
Expand All @@ -405,18 +405,22 @@ public function serialize(): string
$this->enabled,
$this->id,
$this->encoderName,
]);
];
}

/**
* @param string $serialized
* @internal
*/
public function unserialize($serialized): void
public function serialize(): string
{
return serialize($this->__serialize());
}

public function __unserialize(array $serialized): void
{
$data = unserialize($serialized);
// add a few extra elements in the array to ensure that we have enough keys when unserializing
// older data which does not include all properties.
$data = array_merge($data, array_fill(0, 2, null));
$serialized = array_merge($serialized, array_fill(0, 2, null));

[
$this->password,
Expand All @@ -427,7 +431,17 @@ public function unserialize($serialized): void
$this->enabled,
$this->id,
$this->encoderName,
] = $data;
] = $serialized;
}

/**
* @param string $serialized
*
* @internal
*/
public function unserialize($serialized): void
{
$this->__unserialize(unserialize($serialized));
}

protected function hasExpired(?\DateTimeInterface $date): bool
Expand Down
4 changes: 4 additions & 0 deletions src/Sylius/Component/User/Model/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ public function getOAuthAccount(string $provider): ?UserOAuthInterface;
public function addOAuthAccount(UserOAuthInterface $oauth): void;

public function setEncoderName(?string $encoderName): void;

public function __serialize(): array;

public function __unserialize(array $data): void;
}

0 comments on commit 8ee908c

Please sign in to comment.