From 683fc57528828ddb76dc15dbb4db1c74fff004c2 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Thu, 23 Apr 2020 21:18:31 +0200 Subject: [PATCH 1/3] make number json and php serializable --- src/Contract/LuhnAlgorithmInterface.php | 2 +- src/Number.php | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Contract/LuhnAlgorithmInterface.php b/src/Contract/LuhnAlgorithmInterface.php index 33ef196..dff332c 100644 --- a/src/Contract/LuhnAlgorithmInterface.php +++ b/src/Contract/LuhnAlgorithmInterface.php @@ -41,9 +41,9 @@ interface LuhnAlgorithmInterface * * @param NumberInterface $number The number to validate. * + * @throws MissingCheckDigitException If the check digit in the number is not set. * @return bool true if number is valid, false otherwise. * - * @throws MissingCheckDigitException If the check digit in the number is not set. */ public function isValid(NumberInterface $number): bool; diff --git a/src/Number.php b/src/Number.php index ef6e44a..492dafb 100644 --- a/src/Number.php +++ b/src/Number.php @@ -33,7 +33,7 @@ /** * Input for the Luhn Algorithm contains a number and a check digit. */ -class Number implements NumberInterface +class Number implements NumberInterface, \JsonSerializable, \Serializable { /** * @var string @@ -67,9 +67,9 @@ public function __construct(string $number, int $checkDigit = null) * * @param string $input The input that contains the check digit already. * + * @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers. * @return self * - * @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers. */ public static function fromString(string $input): self { @@ -114,4 +114,22 @@ public function __toString() { return $this->number . $this->checkDigit; } + + public function serialize() + { + return serialize([$this->number, $this->checkDigit]); + } + + public function unserialize($serialized) + { + list($this->number, $this->checkDigit) = unserialize($serialized); + } + + public function jsonSerialize() + { + return [ + "number" => $this->number, + "checkDigit" => $this->checkDigit, + ]; + } } From 51eb223998064ed8149f9b221f60e5c11b525847 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Thu, 23 Apr 2020 21:24:02 +0200 Subject: [PATCH 2/3] lint. remove json --- src/Number.php | 10 +--------- tests/NumberTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Number.php b/src/Number.php index 492dafb..b5e7a23 100644 --- a/src/Number.php +++ b/src/Number.php @@ -33,7 +33,7 @@ /** * Input for the Luhn Algorithm contains a number and a check digit. */ -class Number implements NumberInterface, \JsonSerializable, \Serializable +class Number implements NumberInterface, \Serializable { /** * @var string @@ -124,12 +124,4 @@ public function unserialize($serialized) { list($this->number, $this->checkDigit) = unserialize($serialized); } - - public function jsonSerialize() - { - return [ - "number" => $this->number, - "checkDigit" => $this->checkDigit, - ]; - } } diff --git a/tests/NumberTest.php b/tests/NumberTest.php index 0e410e0..2eed66e 100644 --- a/tests/NumberTest.php +++ b/tests/NumberTest.php @@ -121,4 +121,14 @@ public function provideProperties() "Valid number and check digit (null)" => [123, null], ]; } + + public function testSerialize() + { + $number = new Number(133, 7); + $serialized = serialize($number); + $other = unserialize($serialized); + $this->assertInstanceOf(Number::class, $other); + $this->assertEquals(133, $other->getNumber()); + $this->assertEquals(7, $other->getCheckDigit()); + } } From 04d01e2a9df918a72df8e7e8c93d0f7a92bbc700 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Thu, 23 Apr 2020 21:25:22 +0200 Subject: [PATCH 3/3] cleanup --- src/Contract/LuhnAlgorithmInterface.php | 2 +- src/Number.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Contract/LuhnAlgorithmInterface.php b/src/Contract/LuhnAlgorithmInterface.php index dff332c..33ef196 100644 --- a/src/Contract/LuhnAlgorithmInterface.php +++ b/src/Contract/LuhnAlgorithmInterface.php @@ -41,9 +41,9 @@ interface LuhnAlgorithmInterface * * @param NumberInterface $number The number to validate. * - * @throws MissingCheckDigitException If the check digit in the number is not set. * @return bool true if number is valid, false otherwise. * + * @throws MissingCheckDigitException If the check digit in the number is not set. */ public function isValid(NumberInterface $number): bool; diff --git a/src/Number.php b/src/Number.php index b5e7a23..baf9d28 100644 --- a/src/Number.php +++ b/src/Number.php @@ -67,9 +67,9 @@ public function __construct(string $number, int $checkDigit = null) * * @param string $input The input that contains the check digit already. * - * @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers. * @return self * + * @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers. */ public static function fromString(string $input): self {