Skip to content

Commit df028af

Browse files
authored
Implement \Serializable on Number (#26)
* make number json and php serializable * lint. remove json * cleanup
1 parent 328ebb7 commit df028af

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Number.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* Input for the Luhn Algorithm contains a number and a check digit.
3535
*/
36-
class Number implements NumberInterface
36+
class Number implements NumberInterface, \Serializable
3737
{
3838
/**
3939
* @var string
@@ -114,4 +114,14 @@ public function __toString()
114114
{
115115
return $this->number . $this->checkDigit;
116116
}
117+
118+
public function serialize()
119+
{
120+
return serialize([$this->number, $this->checkDigit]);
121+
}
122+
123+
public function unserialize($serialized)
124+
{
125+
list($this->number, $this->checkDigit) = unserialize($serialized);
126+
}
117127
}

tests/NumberTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,14 @@ public function provideProperties()
121121
"Valid number and check digit (null)" => [123, null],
122122
];
123123
}
124+
125+
public function testSerialize()
126+
{
127+
$number = new Number(133, 7);
128+
$serialized = serialize($number);
129+
$other = unserialize($serialized);
130+
$this->assertInstanceOf(Number::class, $other);
131+
$this->assertEquals(133, $other->getNumber());
132+
$this->assertEquals(7, $other->getCheckDigit());
133+
}
124134
}

0 commit comments

Comments
 (0)