Skip to content

Commit

Permalink
Implement new __serialize() and __unserialize() functions (#42)
Browse files Browse the repository at this point in the history
* Implement new __serialize() and __unserialize() functions

* [CircleCI] Fix coding standards
  • Loading branch information
Niklas Ekman authored Mar 11, 2022
1 parent 129a89f commit 12da0bd
Showing 1 changed file with 22 additions and 12 deletions.
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;
}
}

0 comments on commit 12da0bd

Please sign in to comment.