Witam,
Napisałem ostatnio skrypt szyfrujący wiadomości szyfrem cezara a w ramach nauki OOP postanowiłem zrobić też wersję obiektową.
Nie jestem jednak pewien jak mi to wyszło i czy zachowałem wszystkie standardy PSR więc zwracam się o ocenę do społeczności forum :)
Kod:
<?php
class Cezar
{
private $output = '';
private $decode = '';
public function cipher(string $code, int $key, string $operation, bool $uppercase = false)
{
for ($i = 0; $i < mb_strlen($code); $i++) {
$letter[$i] = mb_substr($code, $i, 1);
if (ctype_alnum($letter[$i]) == false) {
$this->output .= $letter[$i];
continue;
}
if ($operation == 'ENCRYPT' || $operation == 'encrypt') {
$this->decode = ord($letter[$i]) + $key;
for ($j = 0; $j <= 25; $j++)
if ($this->decode == 123 + $j) $this->decode -= 26;
}
else if ($operation == 'DECRYPT' || $operation == 'decrypt') {
$this->decode = ord($letter[$i]) - $key;
for ($j = 0; $j <= 25; $j++)
if ($this->decode == 96 - $j) $this->decode += 26;
}
$this->decode = chr($this->decode);
if (ctype_upper(mb_substr($code, $i, 1))) $this->decode = strtoupper($this->decode);
$this->output .= $this->decode;
}
if ($uppercase==true) $this->output = mb_strtoupper($this->output);
return '<p>Result: ' . $this->output . '</p> Original text: ' . $code;
}
}
$cezar = new Cezar();
echo $cezar->cipher('To skrypt szyfrujacy wiadomosci szyfrem cezara zrobiony w PHP', 3, 'ENCRYPT', false);