/** * will unpack binary string and return new instance of self with properties set * * @param string $data * @return DataHeader * @throws TransformerException */ static function unpack($data) { $info = unpack('Slength', substr($data, 0, 2)); $info += unpack('Lsignature/A4hash/Spid/Cserialized/Ccompressed/Ctype/A*name', substr($data, 2, $info['length'] - 2)); if (self::SIGNATURE !== $info['signature']) { throw TransformerException::signatureMismatch($info['signature'], self::SIGNATURE); } return (new self())->setSize($info['length'])->setHash($info['hash'])->setPid($info['pid'])->setType($info['type'])->setCompressed($info['serialized'])->setSerialized($info['compressed'])->setName($info['name']); }
/** * will unpack binary string and return new instance of self with properties set * * @param string $data * @return DataContainer * @throws TransformerException */ static function unpack($data) { $header = DataHeader::unpack($data); $data = substr($data, $header->getSize()); $hash = hash('crc32b', $data, true); if ($hash !== $header->getHash()) { throw TransformerException::crcMismatch($hash, $header->getHash()); } if ($header->isCompressed()) { $data = gzuncompress($data); } if ($header->isSerialized()) { $data = unserialize($data); } switch ($header->getType()) { case $header::T_BOOLEAN: $data = (bool) $data; break; case $header::T_INTEGER: $data = (int) $data; break; case $header::T_DOUBLE: $data = (double) $data; break; case $header::T_STRING: $data = (string) $data; break; } return new DataContainer($data, $header); }