Sha256: 0fcb0b7b38e7719e0d0dd9e9a7b9d6cb475525bf4fa9fc3da218ed367e4bc921

Contents?: true

Size: 1.97 KB

Versions: 42

Compression:

Stored size: 1.97 KB

Contents

<?php

class Bob
{
    /**
     * Respond to an input string
     *
     * @param string $str
     * @return string
     */
    public function respondTo($str)
    {
        $str = $this->prepareText($str);

        if ($this->isSilence($str)) {
            return "Fine. Be that way!";
        }

        if ($this->isYelling($str)) {
            return "Whoa, chill out!";
        }

        if ($this->isQuestion($str)) {
            return "Sure.";
        }

        return "Whatever.";
    }


    /**
     * Test if the string is being yelled
     *
     * @param string $str
     * @return bool
     */
    private function isAllCapitals($str)
    {
        return strtoupper($str) == $str;
    }

    /**
     * Test is the string contains characters
     *
     * The RegEx matches a single character belonging to the "letter" category
     * @link http://www.regular-expressions.info/unicode.html
     *
     * @param string $str
     * @return int
     */
    private function containsCharacters($str)
    {
        return preg_match('/\p{L}/', $str);
    }

    /**
     * Test if something is being shouted
     *
     * @param $str
     * @return bool
     */
    private function isYelling($str)
    {
        return $this->containsCharacters($str) && $this->isAllCapitals($str);
    }

    /**
     * Test if the string is a question
     *
     * The RegEx looks for a question mark at the end of the sentence
     *
     * @param string $str
     * @return int
     */
    private function isQuestion($str)
    {
        return preg_match('/\?$/', $str);
    }

    /**
     * Test if the string contains something Bob can respond to
     *
     * @param string $str
     * @return bool
     */
    private function isSilence($str)
    {
        return empty($str);
    }

    /**
     * Remove excessive white space and remove non-printing characters
     *
     * @param string $str
     * @return string
     */
    private function prepareText($str)
    {
        return trim($str);
    }
}

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
trackler-2.2.1.104 tracks/php/exercises/bob/example.php
trackler-2.2.1.103 tracks/php/exercises/bob/example.php
trackler-2.2.1.102 tracks/php/exercises/bob/example.php
trackler-2.2.1.101 tracks/php/exercises/bob/example.php
trackler-2.2.1.100 tracks/php/exercises/bob/example.php
trackler-2.2.1.99 tracks/php/exercises/bob/example.php
trackler-2.2.1.98 tracks/php/exercises/bob/example.php
trackler-2.2.1.97 tracks/php/exercises/bob/example.php
trackler-2.2.1.96 tracks/php/exercises/bob/example.php
trackler-2.2.1.95 tracks/php/exercises/bob/example.php
trackler-2.2.1.94 tracks/php/exercises/bob/example.php
trackler-2.2.1.93 tracks/php/exercises/bob/example.php
trackler-2.2.1.92 tracks/php/exercises/bob/example.php
trackler-2.2.1.91 tracks/php/exercises/bob/example.php
trackler-2.2.1.90 tracks/php/exercises/bob/example.php
trackler-2.2.1.89 tracks/php/exercises/bob/example.php
trackler-2.2.1.88 tracks/php/exercises/bob/example.php
trackler-2.2.1.87 tracks/php/exercises/bob/example.php
trackler-2.2.1.86 tracks/php/exercises/bob/example.php
trackler-2.2.1.85 tracks/php/exercises/bob/example.php