diff --git a/sources/classes/FileHash.php b/sources/classes/FileHash.php
index 51aa549..3437440 100644
--- a/sources/classes/FileHash.php
+++ b/sources/classes/FileHash.php
@@ -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;
}
+
+ /**
+ * Проверяет, совпадает ли хэш с хэшем/файлом/строкой str
.
+ *
+ * @param string $str Хэш, строка или имя файла.
+ * @param HashGetType $hashBy Тип получения хэша.
+ *
+ * @return bool true
, если совпадают, и false
, если не совпадают.
+ */
+ 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);
+ }
}
\ No newline at end of file
diff --git a/sources/enums/HashGetType.php b/sources/enums/HashGetType.php
index 8d1fbaa..99a1bc6 100644
--- a/sources/enums/HashGetType.php
+++ b/sources/enums/HashGetType.php
@@ -9,7 +9,7 @@ use goodboyalex\php_components_pack\traits\EnumExtensionsTrait;
*
* @author Александр Бабаев
* @package php_components_pack
- * @version 1.0
+ * @version 1.1
* @since 1.0.5
*/
enum HashGetType: int
@@ -17,13 +17,18 @@ enum HashGetType: int
// Подключаю расширение для Enum
use EnumExtensionsTrait;
+ /**
+ * По строке хэша.
+ */
+ case ByHash = 0;
+
/**
* По строке.
*/
- case ByString = 0;
+ case ByString = 1;
/**
* По файлу.
*/
- case ByFile = 1;
+ case ByFile = 2;
}
\ No newline at end of file
diff --git a/tests/classes/FileHashTest.php b/tests/classes/FileHashTest.php
index 851ccba..2d5af03 100644
--- a/tests/classes/FileHashTest.php
+++ b/tests/classes/FileHashTest.php
@@ -12,7 +12,11 @@ class FileHashTest extends TestCase
{
$this->PrepareForTest();
- $fileHash = new FileHash("Тестовое слово");
+ $fileHash = new FileHash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+ $this->assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", $fileHash->Hash);
+
+
+ $fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
$this->assertEquals("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7", $fileHash->Hash);
$fileHash = new FileHash(__DIR__ . '/../data/A.php', HashGetType::ByFile);
@@ -27,6 +31,7 @@ class FileHashTest extends TestCase
require_once __DIR__ . '/../../sources/traits/EnumExtensionsTrait.php';
require_once __DIR__ . '/../../sources/enums/HashGetType.php';
require_once __DIR__ . '/../../sources/extensions/StringExtension.php';
+ require_once __DIR__ . '/../../sources/interfaces/ISerializable.php';
require_once __DIR__ . '/../../sources/classes/FileHash.php';
}
@@ -34,9 +39,49 @@ class FileHashTest extends TestCase
{
$this->PrepareForTest();
- $fileHash = new FileHash("Тестовое слово");
+ $fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
- $this->assertTrue($fileHash->IsEqual(new FileHash("Тестовое слово")));
- $this->assertFalse($fileHash->IsEqual(new FileHash("Тестовое слово2")));
+ $this->assertTrue($fileHash->IsEqual(new FileHash("Тестовое слово", HashGetType::ByString)));
+ $this->assertFalse($fileHash->IsEqual(new FileHash("Тестовое слово2", HashGetType::ByString)));
+ }
+
+ public function testSerialize ()
+ {
+ $this->PrepareForTest();
+
+ $fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
+
+ $serialized = $fileHash->Serialize();
+
+ $this->assertEquals("s:64:\"000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7\";", $serialized);
+ }
+
+ public function testUnSerialize ()
+ {
+ $this->PrepareForTest();
+
+ $fileHash = new FileHash();
+
+ $serialized = "s:64:\"000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7\";";
+
+ $fileHash->UnSerialize($serialized);
+
+ $this->assertEquals("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7", $fileHash->Hash);
+ }
+
+ public function testValidate ()
+ {
+ $this->PrepareForTest();
+
+ $fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
+
+ $this->assertTrue($fileHash->Validate("Тестовое слово", HashGetType::ByString));
+ $this->assertTrue($fileHash->Validate("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7",
+ HashGetType::ByHash));
+ $this->assertFalse($fileHash->Validate("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7",
+ HashGetType::ByString));
+
+ $fileHash = new FileHash(__DIR__ . '/../data/A.php', HashGetType::ByFile);
+ $this->assertTrue($fileHash->Validate(__DIR__ . '/../data/A.php', HashGetType::ByFile));
}
}
\ No newline at end of file