Skip to content

Commit 12da0bd

Browse files
author
Niklas Ekman
authored
Implement new __serialize() and __unserialize() functions (#42)
* Implement new __serialize() and __unserialize() functions * [CircleCI] Fix coding standards
1 parent 129a89f commit 12da0bd

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/Number.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public function __construct(string $number, int $checkDigit = null)
5959
/**
6060
* Create a new number from an input that contains the check digit already
6161
* @param string $input The input that contains the check digit already.
62-
* @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers.
63-
* @throws LuhnAlgorithmExceptionInterface
6462
* @return self
6563
*
64+
* @throws LuhnAlgorithmExceptionInterface
65+
* @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers.
6666
*/
6767
public static function fromString(string $input): self
6868
{
@@ -83,12 +83,9 @@ public static function fromString(string $input): self
8383
return new self($number, $checkDigit);
8484
}
8585

86-
/**
87-
* {@inheritdoc}
88-
*/
89-
public function getNumber(): string
86+
public function __toString(): string
9087
{
91-
return $this->number;
88+
return $this->number . $this->checkDigit;
9289
}
9390

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

102-
public function __toString(): string
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function getNumber(): string
103103
{
104-
return $this->number . $this->checkDigit;
104+
return $this->number;
105105
}
106106

107107
public function serialize(): string
108108
{
109-
return serialize([$this->number, $this->checkDigit]);
109+
return serialize($this->__serialize());
110+
}
111+
112+
public function __serialize(): array
113+
{
114+
return [$this->number, $this->checkDigit];
115+
}
116+
117+
public function unserialize($data): void
118+
{
119+
$this->__unserialize(unserialize($data));
110120
}
111121

112-
public function unserialize($serialized): void
122+
public function __unserialize(array $data): void
113123
{
114-
[$this->number, $this->checkDigit] = unserialize($serialized);
124+
[$this->number, $this->checkDigit] = $data;
115125
}
116126
}

0 commit comments

Comments
 (0)