lithium\g11n\catalog\adapter\Gettext::_parseMo PHP Method

_parseMo() protected method

Parses machine object (MO) format, independent of the machine's endian it was created on. Both 32bit and 64bit systems are supported.
protected _parseMo ( resource $stream ) : array
$stream resource
return array
    protected function _parseMo($stream)
    {
        $stat = fstat($stream);
        if ($stat['size'] < self::MO_HEADER_SIZE) {
            throw new RangeException("MO stream content has an invalid format.");
        }
        $magic = unpack('V1', fread($stream, 4));
        $magic = hexdec(substr(dechex(current($magic)), -8));
        if ($magic == self::MO_LITTLE_ENDIAN_MAGIC) {
            $isBigEndian = false;
        } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
            $isBigEndian = true;
        } else {
            throw new RangeException("MO stream content has an invalid format.");
        }
        $header = array('formatRevision' => null, 'count' => null, 'offsetId' => null, 'offsetTranslated' => null, 'sizeHashes' => null, 'offsetHashes' => null);
        foreach ($header as &$value) {
            $value = $this->_readLong($stream, $isBigEndian);
        }
        extract($header);
        $data = array();
        for ($i = 0; $i < $count; $i++) {
            $singularId = $pluralId = null;
            $translated = null;
            $context = null;
            fseek($stream, $offsetId + $i * 8);
            $length = $this->_readLong($stream, $isBigEndian);
            $offset = $this->_readLong($stream, $isBigEndian);
            if ($length < 1) {
                continue;
            }
            fseek($stream, $offset);
            $singularId = fread($stream, $length);
            if (strpos($singularId, "") !== false) {
                list($singularId, $pluralId) = explode("", $singularId);
            }
            if (strpos($singularId, "") !== false) {
                list($context, $singularId) = explode("", $singularId);
            }
            fseek($stream, $offsetTranslated + $i * 8);
            $length = $this->_readLong($stream, $isBigEndian);
            $offset = $this->_readLong($stream, $isBigEndian);
            fseek($stream, $offset);
            $translated = fread($stream, $length);
            if (strpos($translated, "") !== false) {
                $translated = explode("", $translated);
            }
            $ids = array('singular' => $singularId, 'plural' => $pluralId);
            $data = $this->_merge($data, compact('ids', 'translated', 'context'));
        }
        return $data;
    }