Skip to content

Implement new __serialize() and __unserialize() functions #42

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

Merged
merged 2 commits into from
Mar 11, 2022
Merged
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
34 changes: 22 additions & 12 deletions src/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public function __construct(string $number, int $checkDigit = null)
/**
* Create a new number from an input that contains the check digit already
* @param string $input The input that contains the check digit already.
* @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers.
* @throws LuhnAlgorithmExceptionInterface
* @return self
*
* @throws LuhnAlgorithmExceptionInterface
* @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers.
*/
public static function fromString(string $input): self
{
Expand All @@ -83,12 +83,9 @@ public static function fromString(string $input): self
return new self($number, $checkDigit);
}

/**
* {@inheritdoc}
*/
public function getNumber(): string
public function __toString(): string
{
return $this->number;
return $this->number . $this->checkDigit;
}

/**
Expand All @@ -99,18 +96,31 @@ public function getCheckDigit(): ?int
return $this->checkDigit;
}

public function __toString(): string
/**
* {@inheritdoc}
*/
public function getNumber(): string
{
return $this->number . $this->checkDigit;
return $this->number;
}

public function serialize(): string
{
return serialize([$this->number, $this->checkDigit]);
return serialize($this->__serialize());
}

public function __serialize(): array
{
return [$this->number, $this->checkDigit];
}

public function unserialize($data): void
{
$this->__unserialize(unserialize($data));
}

public function unserialize($serialized): void
public function __unserialize(array $data): void
{
[$this->number, $this->checkDigit] = unserialize($serialized);
[$this->number, $this->checkDigit] = $data;
}
}