@@ -7,16 +7,11 @@ class CaesarCipher
7
7
const ALPHABET_EN = 'abcdefghijklmnopqrstuvwxyz ' ;
8
8
const ALPHABET_RU = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя ' ;
9
9
10
- private string $ alphabet ;
11
-
12
10
/**
13
- * @param int $shift
14
- * @param string|null $alphabet
11
+ * @param int $shift
12
+ * @param string $alphabet
15
13
*/
16
- public function __construct (private int $ shift , ?string $ alphabet = self ::ALPHABET_RU )
17
- {
18
- $ this ->alphabet = $ alphabet ;
19
- }
14
+ public function __construct (private int $ shift , private string $ alphabet = self ::ALPHABET_RU ) {}
20
15
21
16
/**
22
17
* @param string|null $alphabet
@@ -32,74 +27,57 @@ public function alphabet(?string $alphabet = self::ALPHABET_RU): static
32
27
33
28
/**
34
29
* @param string $text
30
+ * @param int $shift
35
31
*
36
32
* @return string
37
33
*/
38
- public function encrypt (string $ text)
34
+ private function process (string $ text, int $ shift ): string
39
35
{
40
- $ encryptedText = '' ;
36
+ $ processedText = '' ;
37
+ $ alphabetLength = mb_strlen ($ this ->alphabet );
41
38
42
- // Проходим по каждому символу входного текста
43
39
for ($ i = 0 ; $ i < mb_strlen ($ text ); $ i ++) {
44
- $ char = mb_substr ($ text , $ i , 1 ); // Получаем текущий символ
40
+ $ char = mb_substr ($ text , $ i , 1 );
45
41
46
42
// Ищем позицию символа в алфавите
47
43
$ position = mb_strpos ($ this ->alphabet , mb_strtolower ($ char ));
48
44
49
45
// Если символ не найден в алфавите, оставляем его без изменений
50
46
if ($ position === false ) {
51
- $ encryptedText .= $ char ;
52
- } else {
53
- // Сдвигаем позицию символа на заданное количество шагов
54
- $ newPosition = ($ position + $ this ->shift ) % mb_strlen ($ this ->alphabet );
55
- $ encryptedChar = mb_substr ($ this ->alphabet , $ newPosition , 1 );
56
-
57
- // Учитываем регистр символа
58
- if (mb_strtoupper ($ char ) === $ char ) {
59
- $ encryptedText .= mb_strtoupper ($ encryptedChar );
60
- } else {
61
- $ encryptedText .= $ encryptedChar ;
62
- }
47
+ $ processedText .= $ char ;
48
+ continue ;
63
49
}
50
+
51
+ // Сдвигаем позицию символа
52
+ $ newPosition = ($ position + $ shift + $ alphabetLength ) % $ alphabetLength ;
53
+ $ processedChar = mb_substr ($ this ->alphabet , $ newPosition , 1 );
54
+
55
+ // Учитываем регистр символа
56
+ $ processedText .= mb_strtoupper ($ char ) === $ char
57
+ ? mb_strtoupper ($ processedChar )
58
+ : $ processedChar ;
64
59
}
65
60
66
- return $ encryptedText ;
61
+ return $ processedText ;
67
62
}
68
63
69
64
/**
70
- * @param string $encryptedText
65
+ * @param string $text
71
66
*
72
67
* @return string
73
68
*/
74
- public function decrypt (string $ encryptedText )
69
+ public function encrypt (string $ text ): string
75
70
{
76
- $ decryptedText = '' ;
77
- $ shift = mb_strlen ($ this ->alphabet ) - $ this ->shift ;
78
-
79
- // Проходим по каждому символу зашифрованного текста
80
- for ($ i = 0 ; $ i < mb_strlen ($ encryptedText ); $ i ++) {
81
- $ char = mb_substr ($ encryptedText , $ i , 1 ); // Получаем текущий символ
82
-
83
- // Ищем позицию символа в алфавите
84
- $ position = mb_strpos ($ this ->alphabet , mb_strtolower ($ char ));
85
-
86
- // Если символ не найден в алфавите, оставляем его без изменений
87
- if ($ position === false ) {
88
- $ decryptedText .= $ char ;
89
- } else {
90
- // Сдвигаем позицию символа на обратное количество шагов
91
- $ newPosition = ($ position + $ shift ) % mb_strlen ($ this ->alphabet );
92
- $ decryptedChar = mb_substr ($ this ->alphabet , $ newPosition , 1 );
93
-
94
- // Учитываем регистр символа
95
- if (mb_strtoupper ($ char ) === $ char ) {
96
- $ decryptedText .= mb_strtoupper ($ decryptedChar );
97
- } else {
98
- $ decryptedText .= $ decryptedChar ;
99
- }
100
- }
101
- }
71
+ return $ this ->process ($ text , $ this ->shift );
72
+ }
102
73
103
- return $ decryptedText ;
74
+ /**
75
+ * @param string $encryptedText
76
+ *
77
+ * @return string
78
+ */
79
+ public function decrypt (string $ encryptedText ): string
80
+ {
81
+ return $ this ->process ($ encryptedText , -$ this ->shift );
104
82
}
105
83
}
0 commit comments