Sha256: 9fbbfe011bd86aec4bcda65d9830cc7ae86161efe08e9be44748678320b8e5ab

Contents?: true

Size: 1.97 KB

Versions: 280

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

280 entries across 280 versions & 1 rubygems

Version Path
trackler-2.2.1.62 tracks/php/exercises/bob/example.php
trackler-2.2.1.61 tracks/php/exercises/bob/example.php
trackler-2.2.1.60 tracks/php/exercises/bob/example.php
trackler-2.2.1.59 tracks/php/exercises/bob/example.php
trackler-2.2.1.58 tracks/php/exercises/bob/example.php
trackler-2.2.1.57 tracks/php/exercises/bob/example.php
trackler-2.2.1.56 tracks/php/exercises/bob/example.php
trackler-2.2.1.55 tracks/php/exercises/bob/example.php
trackler-2.2.1.54 tracks/php/exercises/bob/example.php
trackler-2.2.1.53 tracks/php/exercises/bob/example.php
trackler-2.2.1.52 tracks/php/exercises/bob/example.php
trackler-2.2.1.51 tracks/php/exercises/bob/example.php
trackler-2.2.1.50 tracks/php/exercises/bob/example.php
trackler-2.2.1.49 tracks/php/exercises/bob/example.php
trackler-2.2.1.48 tracks/php/exercises/bob/example.php
trackler-2.2.1.47 tracks/php/exercises/bob/example.php
trackler-2.2.1.46 tracks/php/exercises/bob/example.php
trackler-2.2.1.45 tracks/php/exercises/bob/example.php
trackler-2.2.1.44 tracks/php/exercises/bob/example.php
trackler-2.2.1.43 tracks/php/exercises/bob/example.php