Simple cipher in php

Ok, I give up.
I’m comfortable with javascript, php is a new language for me.
The thing is, I don’t understand what these tests want. For example, I have to throw exceptions but do I have to exit the exercise, fix the issue… ? I got as far as 11 passed tests and 5 fails and then I revert back to 16 fails and I just don’t understand why. I don’t understand why I’m passing or failing.
The encoding and decoding functions are far from perfect but I don’t feel that that is the core issue, it’s exception handling and classes. I think. Tried downloading exercism, that worked in the past but reinstalled and got new laptop and blablabla. Can’t do it. So, I copy the exercise, I’ve got xampp running and there I can get rid of all the error messages and produce all the echo’s and var_dumps and see what I’m doing and why it’s happening. Then I copy everything back into exercism and … I’m very very tired.
Please help.
Thank you,
Karin

If you share your code and the test outputs, someone might be able to help explain what they are saying :slight_smile:

Please do not use images. Please use codeblocks.

Hi,

Thank you for this very quick reply and of course some code is convenient. I’m posting the whole thing. Thank you very much for taking an interest.

declare(strict_types=1);

class SimpleCipher
{

    public $alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

    public function __construct(string $key = null )
    {
        $this->key = $key;
        $this->ensureKeyIntegrity();
 
    }
    public function ensureKeyIntegrity() {
        if($this->key == null) {
            echo "You have not entered a key.";
            throw new InvalidArgumentException("You have not entered a key.");                 
        }
        elseif(is_string($this->key) && strlen($this->$key) == 0) {
            echo "You have not entered a key.";
            throw new InvalidArgumentException("You haven't entered a text yet.");
        }
        $arr = str_split($this->key);
        foreach($arr as &$char) {
            if(ctype_upper($char)) {
                echo "uppercase: " .  $char . "\n";
                throw new InvalidArgumentException("All characters must be lower case.");             
            } 
            elseif(is_numeric($char)) {
                echo "number: " .  $char . "\n";
                throw new InvalidArgumentException("Numbers are not allowed.");     
            }
        }                    
    }
    public function createNewKey() {
        $index = rand(0, 25); 
        $newkey = $this->alphabet[$index];
        $this->key = $newkey;
    }
    public function encode(string $plainText): string
    { 
        $this->ensureKeyIntegrity();
        $arr = str_split($plainText);

        $lengteKey = strlen($this->key);
        $lengteText = strlen($plainText);
        if($lengteKey < $lengteText) {
            for($x=0, $aantal = $lengteText - $lengteKey; $x <= $aantal; $x++) {
                $this->key .= $this->key[$lengteKey-1];
            }
        }
        
        foreach($arr as $index => &$charact) {
            echo 'key index: ' . $this->key[$index] . '<br>';
            $encodingInt = array_search($this->key[$index], $this->alphabet);
            echo 'encodingInt: ' . $encodingInt . '<br>';
            if((ord($charact) + $encodingInt) > 122 ) {
                $charact = ord($charact) - $encodingInt;
            } else {
                $charact = ord($charact) + $encodingInt;
            }
            $charact= chr($charact);
            echo $charact . '<br>';
        }
        $encodedText = implode($arr);
        echo "encodedText " . $encodedText . '<br>';
        return $encodedText;
    }

    public function decode(string $cipherText): string
    {
        $arr = str_split($cipherText);
        $arr = str_split($cipherText);
        foreach($arr as $index => &$charact) {
            echo 'key index: ' . $this->key[$index] . '<br>';
            $decodingInt = array_search($this->key[$index], $this->alphabet);
            echo 'decodingInt: ' . $decodingInt . '<br>';
            if((ord($charact) - $decodingInt) < 0 ) {
                $charact = ord($charact) + $decodingInt;
            } else {
                $charact = ord($charact) - $decodingInt;
            }
            $charact= chr($charact);
            echo $charact . '<br>';
        }
        $decodedText = implode($arr);        
        var_dump("decodedText " . $decodedText);
        return $decodedText;
    }
}

Greets,
Kairn

Could you also share the test outputs? What test fails? What is the code run and failure message?

Hi @mientje, welcome back :slight_smile:

I have helped you get over some obstacles some time ago, and I’m glad you got along further on the track.

Take a close look at this line. Do you see the place, where you don’t access the property correctly? Fixing this makes your code work again.

Some of the tests create a new instance of your class without providing a key, for example this one:

    public function testRandomCipherKeyIsLetters(): void
    {
        $cipher = new SimpleCipher();
        $this->assertMatchesRegularExpression('/\A[a-z]+\z/', $cipher->key);
    }

The test name should tell you, that for no key given you have to make one yourself, using a source of randomness. PHP has some functions for that, but none of them produces an output directly that would match the regular expression. So you need to write one that produces a valid key.

When you get stuck again or have further questions, we are here to help.