Skip to content

Commit a517b73

Browse files
authored
Merge pull request #15 from amculin/test
Test
2 parents 255df3c + b22a108 commit a517b73

10 files changed

+266
-95
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939

4040
- name: Install dependencies
4141
run: composer install --prefer-dist
42+
43+
- name: Run PHPStan
44+
run: vendor/bin/phpstan analyse -l 10 source/ tests/
4245

4346
- name: Run unit tests
4447
run: vendor/bin/phpunit --no-coverage tests/

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
}
2020
},
2121
"require-dev": {
22+
"phpstan/phpstan": "^2.1",
2223
"phpunit/phpunit": "^11.5"
2324
}
2425
}

composer.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/AlnumVigenereCipher.php

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,77 @@
88
* This file is the main class for alpha-numric mode vigenere cipher algortithm
99
*
1010
* @author Fahmi Auliya Tsani <[email protected]>
11-
* @version 0.1
11+
* @version 1.1
1212
*/
13-
1413
class AlnumVigenereCipher extends VigenereCipherBlueprint
1514
{
15+
public function __construct(
16+
public string $data,
17+
public string $key,
18+
public string $process = 'encrypt'
19+
) {
20+
$this->process = $process;
21+
$this->tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
22+
23+
if ($process == ProcessType::ENCRYPT->value) {
24+
$this->plainText = $data;
25+
$this->key = $this->generateKey($key);
26+
27+
if ($this->isValid()) {
28+
$this->encrypt();
29+
}
30+
} else {
31+
$this->cipherText = $data;
32+
$this->key = $this->generateKey($key);
33+
34+
if ($this->isValid()) {
35+
$this->decrypt();
36+
}
37+
}
38+
}
39+
1640
/**
17-
* Default list of acceptable character to be used in vigenere cipher algorithm
18-
* This list cointains alpha-numeric characters including the capitalized
19-
*
20-
* @var string
41+
* @inheritdoc
2142
*/
22-
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
43+
public function isValidKey(string $pattern): bool
44+
{
45+
return preg_match($pattern, $this->key) == 1;
46+
}
2347

48+
/**
49+
* @inheritdoc
50+
*/
51+
public function isValidPlainText(string $pattern): bool
52+
{
53+
return preg_match($pattern, $this->plainText) == 1;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function isValidCipherText(string $pattern): bool
60+
{
61+
return preg_match($pattern, $this->cipherText) == 1;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
2467
public function isValid(): bool
2568
{
2669
try {
2770
$pattern = '/^[a-zA-Z0-9]*$/';
28-
$isValid = preg_match($pattern, $this->key);
2971

30-
if (! $isValid) {
72+
if (! $this->isValidKey($pattern)) {
3173
throw new InvalidAlnumException('Key');
3274
}
3375

3476
if ($this->process == ProcessType::ENCRYPT->value) {
35-
$isValid = preg_match($pattern, $this->plainText) && $isValid;
36-
37-
if (! $isValid) {
77+
if (! $this->isValidPlainText($pattern)) {
3878
throw new InvalidAlnumException('Plain text');
3979
}
4080
} else {
41-
$isValid = preg_match($pattern, $this->cipherText) && $isValid;
42-
43-
if (! $isValid) {
81+
if (! $this->isValidCipherText($pattern)) {
4482
throw new InvalidAlnumException('Cipher text');
4583
}
4684
}
@@ -50,6 +88,7 @@ public function isValid(): bool
5088
return false;
5189
}
5290

91+
$this->setIsValid(true);
5392
return true;
5493
}
5594
}

source/BasicVigenereCipher.php

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,78 @@
88
* This file is the main class for basic vigenere cipher algortithm
99
*
1010
* @author Fahmi Auliya Tsani <[email protected]>
11-
* @version 0.2
11+
* @version 1.1
1212
*/
13-
13+
#[\AllowDynamicProperties]
1414
class BasicVigenereCipher extends VigenereCipherBlueprint
1515
{
16+
public function __construct(
17+
public string $data,
18+
public string $key,
19+
public string $process = 'encrypt'
20+
) {
21+
$this->process = $process;
22+
$this->tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';
23+
24+
if ($process == ProcessType::ENCRYPT->value) {
25+
$this->plainText = $data;
26+
$this->key = $this->generateKey($key);
27+
28+
if ($this->isValid()) {
29+
$this->encrypt();
30+
}
31+
} else {
32+
$this->cipherText = $data;
33+
$this->key = $this->generateKey($key);
34+
35+
if ($this->isValid()) {
36+
$this->decrypt();
37+
}
38+
}
39+
}
40+
1641
/**
1742
* @inheritdoc
1843
*/
19-
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';
44+
public function isValidKey(string $pattern): bool
45+
{
46+
return preg_match($pattern, $this->key) == 1;
47+
}
2048

49+
/**
50+
* @inheritdoc
51+
*/
52+
public function isValidPlainText(string $pattern): bool
53+
{
54+
return preg_match($pattern, $this->plainText) == 1;
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function isValidCipherText(string $pattern): bool
61+
{
62+
return preg_match($pattern, $this->cipherText) == 1;
63+
}
64+
65+
/**
66+
* @inheritdoc
67+
*/
2168
public function isValid(): bool
2269
{
2370
try {
2471
$pattern = '/^[a-z]*$/';
25-
$isValid = preg_match($pattern, $this->key);
2672

27-
if (! $isValid) {
73+
if (! $this->isValidKey($pattern)) {
2874
throw new InvalidBasicException('Key');
2975
}
3076

3177
if ($this->process == ProcessType::ENCRYPT->value) {
32-
$isValid = preg_match($pattern, $this->plainText) && $isValid;
33-
34-
if (! $isValid) {
78+
if (! $this->isValidPlainText($pattern)) {
3579
throw new InvalidBasicException('Plain text');
3680
}
3781
} else {
38-
$isValid = preg_match($pattern, $this->cipherText) && $isValid;
39-
40-
if (! $isValid) {
82+
if (! $this->isValidCipherText($pattern)) {
4183
throw new InvalidBasicException('Cipher text');
4284
}
4385
}
@@ -47,6 +89,8 @@ public function isValid(): bool
4789
return false;
4890
}
4991

92+
$this->setIsValid(true);
93+
5094
return true;
5195
}
5296
}

source/VigenereCipher.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace amculin\cryptography\classic;
33

4+
use amculin\cryptography\classic\VigenereCipherBlueprint;
45
use amculin\cryptography\classic\enums\ProcessType;
56
use amculin\cryptography\classic\enums\VigenereMode;
67

@@ -9,33 +10,36 @@ class VigenereCipher
910
public static function getClassName(string $mode): string
1011
{
1112
$path = 'amculin\cryptography\classic\\';
13+
$className = 'BasicVigenereCipher';
1214

13-
if ($mode == VigenereMode::BASIC->value) {
14-
return $path . 'BasicVigenereCipher';
15-
} elseif ($mode == VigenereMode::ALPHA_NUMERIC->value) {
16-
return $path . 'AlnumVigenereCipher';
15+
if ($mode == VigenereMode::ALPHA_NUMERIC->value) {
16+
$className = 'AlnumVigenereCipher';
1717
}
18+
19+
return $path . $className;
1820
}
1921

20-
public static function encrypt(string $data, string $key, string $mode = 'basic'): string|null
22+
public static function encrypt(string $data, string $key, string $mode = 'basic'): string
2123
{
2224
$className = self::getClassName($mode);
2325

2426
$processName = ProcessType::ENCRYPT->value;
2527

26-
$encrypt = new $className($processName, $data, $key);
28+
/** @var VigenereCipherBlueprint $encrypt */
29+
$encrypt = new $className($data, $key, $processName);
2730

28-
return $encrypt->getCipherText();
31+
return $encrypt->cipherText;
2932
}
3033

31-
public static function decrypt(string $data, string $key, string $mode = 'basic'): string|null
34+
public static function decrypt(string $data, string $key, string $mode = 'basic'): string
3235
{
3336
$className = self::getClassName($mode);
3437

3538
$processName = ProcessType::DECRYPT->value;
3639

37-
$decrypt = new $className($processName, $data, $key);
40+
/** @var VigenereCipherBlueprint $decrypt */
41+
$decrypt = new $className($data, $key, $processName);
3842

39-
return $decrypt->getPlainText();
43+
return $decrypt->plainText;
4044
}
4145
}

0 commit comments

Comments
 (0)