Skip to content

Commit

Permalink
Add methods for casting between signed and unsigned counterparts
Browse files Browse the repository at this point in the history
  • Loading branch information
olafkryus committed Apr 4, 2020
1 parent 15e34a8 commit d4a3e78
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/DataType/Byte.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN

parent::__construct($value, $endianness);
}

/**
* @return UnsignedByte
* @throws \Exception
*/
public function asUnsigned(): UnsignedByte
{
return new UnsignedByte($this->__toString(), $this->getEndianness());
}

/**
* @return UnsignedByte
* @throws \Exception
*/
public function toUnsigned(): UnsignedByte
{
if ($this->toInt() < 0) {
throw new \Exception("Value too small for type Unsigned Byte.");
}

return $this->asUnsigned();
}
}
22 changes: 22 additions & 0 deletions src/DataType/Dword.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN

parent::__construct($value, $endianness);
}

/**
* @return UnsignedDword
* @throws \Exception
*/
public function asUnsigned(): UnsignedDword
{
return new UnsignedDword($this->__toString(), $this->getEndianness());
}

/**
* @return UnsignedDword
* @throws \Exception
*/
public function toUnsigned(): UnsignedDword
{
if ($this->toInt() < 0) {
throw new \Exception("Value too small for type Unsigned Dword.");
}

return $this->asUnsigned();
}
}
24 changes: 24 additions & 0 deletions src/DataType/UnsignedByte.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN
parent::__construct($value, $endianness, false);
}

/**
* @return Byte
* @throws \Exception
*/
public function asSigned(): Byte
{
return new Byte($this->__toString(), $this->getEndianness());
}

/**
* @return Byte
* @throws \Exception
*/
public function toSigned(): Byte
{
$value = $this->asSigned();

if ($value->toInt() < 0) {
throw new \Exception("Value too big for type Byte.");
}

return $value;
}

/**
* @return int
*/
Expand Down
24 changes: 24 additions & 0 deletions src/DataType/UnsignedDword.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN
parent::__construct($value, $endianness, false);
}

/**
* @return Dword
* @throws \Exception
*/
public function asSigned(): Dword
{
return new Dword($this->__toString(), $this->getEndianness());
}

/**
* @return Dword
* @throws \Exception
*/
public function toSigned(): Dword
{
$value = $this->asSigned();

if ($value->toInt() < 0) {
throw new \Exception("Value too big for type Dword.");
}

return $value;
}

/**
* @return UnsignedWord
* @throws \Exception
Expand Down
24 changes: 24 additions & 0 deletions src/DataType/UnsignedWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN
parent::__construct($value, $endianness, false);
}

/**
* @return Word
* @throws \Exception
*/
public function asSigned(): Word
{
return new Word($this->__toString(), $this->getEndianness());
}

/**
* @return Word
* @throws \Exception
*/
public function toSigned(): Word
{
$value = $this->asSigned();

if ($value->toInt() < 0) {
throw new \Exception("Value too big for type Word.");
}

return $value;
}

/**
* @return UnsignedByte
* @throws \Exception
Expand Down
22 changes: 22 additions & 0 deletions src/DataType/Word.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@ public function __construct(string $value, int $endianness = BinaryValue::ENDIAN

parent::__construct($value, $endianness);
}

/**
* @return UnsignedWord
* @throws \Exception
*/
public function asUnsigned(): UnsignedWord
{
return new UnsignedWord($this->__toString(), $this->getEndianness());
}

/**
* @return UnsignedWord
* @throws \Exception
*/
public function toUnsigned(): UnsignedWord
{
if ($this->toInt() < 0) {
throw new \Exception("Value too small for type Unsigned Word.");
}

return $this->asUnsigned();
}
}

0 comments on commit d4a3e78

Please sign in to comment.