20250214-1
[Д] [HashGetType]: Добавлен параметр ByHash (для получения класса хеша путём передачи ему уже вычисленного хэша). [О] [FileHash]: __construct - $hashBy по умолчанию теперь HashGetType::ByHash. [Д] [FileHash]: Добавлены функции сериализации и десериализации (теперь класс реализует интерфейс ISerializable). [Д] [FileHash]: Добавлена функции валидации хэша Validate.
This commit is contained in:
@@ -4,39 +4,35 @@ namespace goodboyalex\php_components_pack\classes;
|
||||
|
||||
use goodboyalex\php_components_pack\enums\HashGetType;
|
||||
use goodboyalex\php_components_pack\extensions\StringExtension;
|
||||
use goodboyalex\php_components_pack\interfaces\ISerializable;
|
||||
|
||||
/**
|
||||
* Класс для работы с хэшем файла или строки.
|
||||
*
|
||||
* @author Александр Бабаев
|
||||
* @package php_components_pack
|
||||
* @version 1.0
|
||||
* @version 1.0.1
|
||||
* @since 1.0.5
|
||||
*/
|
||||
final class FileHash
|
||||
final class FileHash implements ISerializable
|
||||
{
|
||||
/**
|
||||
* @var string $Hash Хэш файла.
|
||||
*/
|
||||
private(set) string $Hash;
|
||||
|
||||
/**
|
||||
* @var HashGetType Тип получения хэша.
|
||||
*/
|
||||
public HashGetType $HashBy;
|
||||
|
||||
/**
|
||||
* Конструктор.
|
||||
*
|
||||
* @param string $str Строка или имя файла.
|
||||
* @param HashGetType $HashBy Тип получения хэша.
|
||||
* @param string $str Хэщ, строка или имя файла.
|
||||
* @param HashGetType $hashBy Тип получения хэша.
|
||||
*/
|
||||
public function __construct (string $str = "", HashGetType $HashBy = HashGetType::ByString)
|
||||
public function __construct (string $str = "", HashGetType $hashBy = HashGetType::ByHash)
|
||||
{
|
||||
$this->HashBy = $HashBy;
|
||||
$this->Hash = match ($HashBy) {
|
||||
$this->Hash = match ($hashBy) {
|
||||
HashGetType::ByString => $this->pGetHash($str),
|
||||
HashGetType::ByFile => $this->pGetFileHash($str)
|
||||
HashGetType::ByFile => $this->pGetFileHash($str),
|
||||
HashGetType::ByHash => $str
|
||||
};
|
||||
}
|
||||
|
||||
@@ -76,4 +72,37 @@ final class FileHash
|
||||
{
|
||||
return StringExtension::Compare($this->Hash, $otherHash->Hash, true) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет, совпадает ли хэш с хэшем/файлом/строкой <code>str</code>.
|
||||
*
|
||||
* @param string $str Хэш, строка или имя файла.
|
||||
* @param HashGetType $hashBy Тип получения хэша.
|
||||
*
|
||||
* @return bool <code>true</code>, если совпадают, и <code>false</code>, если не совпадают.
|
||||
*/
|
||||
public function Validate (string $str, HashGetType $hashBy): bool
|
||||
{
|
||||
return match ($hashBy) {
|
||||
HashGetType::ByString => $this->pGetHash($str) == $this->Hash,
|
||||
HashGetType::ByFile => $this->pGetFileHash($str) == $this->Hash,
|
||||
HashGetType::ByHash => $str == $this->Hash,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function Serialize (): string
|
||||
{
|
||||
return serialize($this->Hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function UnSerialize (string $serialized): void
|
||||
{
|
||||
$this->Hash = unserialize($serialized);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user