Skip to content

Commit

Permalink
Merge pull request #137 from wmde/serialize
Browse files Browse the repository at this point in the history
Implement _serialize()/__unserialize()
  • Loading branch information
mariushoch authored Oct 17, 2022
2 parents 7a80d7c + 38f2f48 commit af68301
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/DataValues/DecimalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ public function serialize() {
return serialize( $this->value );
}

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

/**
* @see Serializable::unserialize
*
Expand All @@ -198,6 +202,10 @@ public function unserialize( $data ) {
$this->__construct( unserialize( $data ) );
}

public function __unserialize( array $data ): void {
$this->__construct( $data[0] );
}

/**
* @see DataValue::getType
*
Expand Down
14 changes: 11 additions & 3 deletions src/DataValues/QuantityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ public static function newFromNumber( $amount, $unit = '1', $upperBound = null,
* @return string
*/
public function serialize() {
return serialize( [
return serialize( $this->__serialize() );
}

public function __serialize(): array {
return [
$this->amount,
$this->unit,
$this->upperBound,
$this->lowerBound,
] );
];
}

/**
Expand All @@ -118,7 +122,11 @@ public function serialize() {
* @param string $data
*/
public function unserialize( $data ) {
list( $amount, $unit, $upperBound, $lowerBound ) = unserialize( $data );
$this->__unserialize( unserialize( $data ) );
}

public function __unserialize( array $data ): void {
list( $amount, $unit, $upperBound, $lowerBound ) = $data;
$this->__construct( $amount, $unit, $upperBound, $lowerBound );
}

Expand Down
14 changes: 11 additions & 3 deletions src/DataValues/UnboundedQuantityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ protected static function asDecimalValue( $name, $number, DecimalValue $default
* @return string
*/
public function serialize() {
return serialize( [
return serialize( $this->__serialize() );
}

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

/**
Expand All @@ -127,7 +131,11 @@ public function serialize() {
* @param string $data
*/
public function unserialize( $data ) {
list( $amount, $unit ) = unserialize( $data );
$this->__unserialize( unserialize( $data ) );
}

public function __unserialize( array $data ): void {
list( $amount, $unit ) = $data;
$this->__construct( $amount, $unit );
}

Expand Down
4 changes: 3 additions & 1 deletion tests/DataValues/DecimalValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ public function testTrailingNewlineRobustness() {
$value = DecimalValue::newFromArray( "-0.0\n" );

$this->assertTrue( $value->isZero() );
$this->assertSame( 'C:23:"DataValues\DecimalValue":11:{s:4:"+0.0";}', serialize( $value ) );
$this->assertSame( '+0.0', $value->getValue(), 'getValue' );
$this->assertSame( '+0.0', $value->getArrayValue(), 'getArrayValue' );
$this->assertSame( '+0.0', $value->__toString(), '__toString' );
$this->assertSame( '0', $value->getFractionalPart(), 'getFractionalPart' );

$referenceValue = unserialize( 'C:23:"DataValues\DecimalValue":11:{s:4:"+0.0";}' );
$this->assertTrue( $value->equals( $referenceValue ), 'equal to hard-coded serialization' );
}

/**
Expand Down

0 comments on commit af68301

Please sign in to comment.