denpamusic/php-levin

View on GitHub
src/Types/SignedInt.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace Denpa\Levin\Types;

abstract class SignedInt extends Type
{
    /**
     * @return int
     */
    public function machineEndianness(): int
    {
        return (int) (!unpack('S', "\x01\x00")[1] === 1);
    }

    /**
     * @return string
     */
    public function toBinary(): string
    {
        $endian = $this->machineEndianness() == self::BE ?
            $this->isBigEndian() : !$this->isBigEndian();

        return $endian ? parent::toBinary() : strrev(parent::toBinary());
    }
}