fizzbuzz


See On Github

Data

Contributor

Generic placeholder thumbnail

by src

in php

Tags

Source Code

<?php

echo implode("\n", array_map(function($i){
    if ($i % 15 === 0) {
        return 'FizzBuzz';
    }
    if ($i % 3 === 0) {
        return 'Fizz';
    }
    if ($i % 5 === 0) {
        return 'Buzz';
    }

    return $i;

}, range(1,100)));
<?php

/**
 * @param int $number
 *
 * @return string|int
 */
function fizzBuzz($number)
{
    if ($number % 15 === 0) {
        return 'FizzBuzz';
    }

    if ($number % 3 === 0) {
        return 'Fizz';
    }

    if ($number % 5 === 0) {
        return 'Buzz';
    }

    return $number;
}
<?php

/**
 * All classes in the same file for a bird eye view
 * Usualy use one file per interface, class within a namespace
 */

interface FizzBuzzRuleInterface
{
    /**
     * @param int $number
     *
     * @return bool
     */
    public function matchRule($number);

    /**
     * @return string
     */
    public function getResponse();
}

class FizzBuzzRule implements FizzBuzzRuleInterface
{
    /**
     * @var int
     */
    protected $number;

    /**
     * @var string
     */
    protected $output;

    /**
     * @param int $number
     * @param string $output
     */
    public function __construct($number, $output)
    {
        $this->number = $number;
        $this->output = $output;
    }

    /**
     * @param int $number
     *
     * @return bool
     */
    public function matchRule($number)
    {
        return $number % $this->number === 0;
    }

    /**
     * @return string
     */
    public function getResponse()
    {
        return $this->output;
    }
}

class FizzBuzz
{
    /**
     * @var array
     */
    protected $rules = [];

    /**
     * @param FizzBuzzRuleInterface $ruleInterface
     *
     * @return $this
     */
    public function addRule(FizzBuzzRuleInterface $ruleInterface)
    {
        $this->rules[] = $ruleInterface;

        return $this;
    }

    /**
     * @param int $number
     *
     * @return string|int
     */
    public function execute($number)
    {
        /** @var FizzBuzzRuleInterface $rule */
        foreach ($this->rules as $rule) {
            if ($rule->matchRule($number)) {
                return $rule->getResponse();
            }
        }

        return $number;
    }
}

/*
$fizzBuzz = new FizzBuzz();
$fizzBuzz
    ->addRule(new FizzBuzzRule(15, 'FizzBuzz')) // add as many rules as you like without changing any class
    ->addRule(new FizzBuzzRule(3, 'Fizz'))
    ->addRule(new FizzBuzzRule(5, 'Buzz'))
;

// variant 1
for ($i = 1; $i <= 100; $i++) {
    echo $fizzBuzz->execute($i);
    echo "\n";
}

// variant 2
echo implode("\n", array_map([$fizzBuzz, 'execute'], range(1, 100)));
*/
<?php

for ($i=1; $i<=100; $i++) {
    if ($i % 15 === 0) {
        echo "FizzBuzz";

    } elseif ($i % 3 === 0) {
        echo 'Fizz';

    } elseif ($i % 5 === 0) {
        echo 'Buzz';

    } else {
        echo $i;
    }

    echo "\n";
}