mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-10-11 05:14:55 +02:00
php8 support
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the nelexa/zip package.
|
||||
* (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace PhpZip\Model\Extra\Fields;
|
||||
|
||||
use PhpZip\Exception\ZipException;
|
||||
@@ -11,45 +20,33 @@ use PhpZip\Model\ZipEntry;
|
||||
*/
|
||||
abstract class AbstractUnicodeExtraField implements ZipExtraField
|
||||
{
|
||||
const DEFAULT_VERSION = 0x01;
|
||||
public const DEFAULT_VERSION = 0x01;
|
||||
|
||||
/** @var int */
|
||||
private $crc32;
|
||||
private int $crc32;
|
||||
|
||||
/** @var string */
|
||||
private $unicodeValue;
|
||||
private string $unicodeValue;
|
||||
|
||||
/**
|
||||
* @param int $crc32
|
||||
* @param string $unicodeValue
|
||||
*/
|
||||
public function __construct($crc32, $unicodeValue)
|
||||
public function __construct(int $crc32, string $unicodeValue)
|
||||
{
|
||||
$this->crc32 = (int) $crc32;
|
||||
$this->unicodeValue = (string) $unicodeValue;
|
||||
$this->crc32 = $crc32;
|
||||
$this->unicodeValue = $unicodeValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int the CRC32 checksum of the filename or comment as
|
||||
* encoded in the central directory of the zip file
|
||||
*/
|
||||
public function getCrc32()
|
||||
public function getCrc32(): int
|
||||
{
|
||||
return $this->crc32;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $crc32
|
||||
*/
|
||||
public function setCrc32($crc32)
|
||||
public function setCrc32(int $crc32): void
|
||||
{
|
||||
$this->crc32 = (int) $crc32;
|
||||
$this->crc32 = $crc32;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUnicodeValue()
|
||||
public function getUnicodeValue(): string
|
||||
{
|
||||
return $this->unicodeValue;
|
||||
}
|
||||
@@ -57,7 +54,7 @@ abstract class AbstractUnicodeExtraField implements ZipExtraField
|
||||
/**
|
||||
* @param string $unicodeValue the UTF-8 encoded name to set
|
||||
*/
|
||||
public function setUnicodeValue($unicodeValue)
|
||||
public function setUnicodeValue(string $unicodeValue): void
|
||||
{
|
||||
$this->unicodeValue = $unicodeValue;
|
||||
}
|
||||
@@ -65,41 +62,44 @@ abstract class AbstractUnicodeExtraField implements ZipExtraField
|
||||
/**
|
||||
* Populate data from this array as if it was in local file data.
|
||||
*
|
||||
* @param string $buffer the buffer to read data from
|
||||
* @param ZipEntry|null $entry
|
||||
* @param string $buffer the buffer to read data from
|
||||
* @param ?ZipEntry $entry
|
||||
*
|
||||
* @throws ZipException on error
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function unpackLocalFileData($buffer, ZipEntry $entry = null)
|
||||
public static function unpackLocalFileData(string $buffer, ?ZipEntry $entry = null): self
|
||||
{
|
||||
if (\strlen($buffer) < 5) {
|
||||
throw new ZipException('Unicode path extra data must have at least 5 bytes.');
|
||||
}
|
||||
|
||||
$data = unpack('Cversion/Vcrc32', $buffer);
|
||||
[
|
||||
'version' => $version,
|
||||
'crc32' => $crc32,
|
||||
] = unpack('Cversion/Vcrc32', $buffer);
|
||||
|
||||
if ($data['version'] !== self::DEFAULT_VERSION) {
|
||||
throw new ZipException(sprintf('Unsupported version [%d] for Unicode path extra data.', $data['version']));
|
||||
if ($version !== self::DEFAULT_VERSION) {
|
||||
throw new ZipException(sprintf('Unsupported version [%d] for Unicode path extra data.', $version));
|
||||
}
|
||||
|
||||
$unicodeValue = substr($buffer, 5);
|
||||
|
||||
return new static($data['crc32'], $unicodeValue);
|
||||
return new static($crc32, $unicodeValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate data from this array as if it was in central directory data.
|
||||
*
|
||||
* @param string $buffer the buffer to read data from
|
||||
* @param ZipEntry|null $entry
|
||||
* @param string $buffer the buffer to read data from
|
||||
* @param ?ZipEntry $entry
|
||||
*
|
||||
* @throws ZipException on error
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function unpackCentralDirData($buffer, ZipEntry $entry = null)
|
||||
public static function unpackCentralDirData(string $buffer, ?ZipEntry $entry = null): self
|
||||
{
|
||||
return self::unpackLocalFileData($buffer, $entry);
|
||||
}
|
||||
@@ -110,14 +110,14 @@ abstract class AbstractUnicodeExtraField implements ZipExtraField
|
||||
*
|
||||
* @return string the data
|
||||
*/
|
||||
public function packLocalFileData()
|
||||
public function packLocalFileData(): string
|
||||
{
|
||||
return pack(
|
||||
'CV',
|
||||
self::DEFAULT_VERSION,
|
||||
$this->crc32
|
||||
) .
|
||||
$this->unicodeValue;
|
||||
)
|
||||
. $this->unicodeValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,7 +126,7 @@ abstract class AbstractUnicodeExtraField implements ZipExtraField
|
||||
*
|
||||
* @return string the data
|
||||
*/
|
||||
public function packCentralDirData()
|
||||
public function packCentralDirData(): string
|
||||
{
|
||||
return $this->packLocalFileData();
|
||||
}
|
||||
|
Reference in New Issue
Block a user