Sha256: a811612cbbcd795aaba7a9e910a83b0e37a289688f992d2fd3a71a262d99111f

Contents?: true

Size: 1.62 KB

Versions: 367

Compression:

Stored size: 1.62 KB

Contents

<?php
class Robot
{
    private $name;

    public function __construct()
    {
        $this->reset();
    }

    /**
     * Get Robot name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Reset name
     */
    public function reset()
    {
        $this->name = NamesRegistry::connect()->getNewName();
    }
}

class NamesRegistry
{
    private $names = [];
    private static $letters;
    private static $registry;

    private function __construct()
    {

    }

    /**
     * Get NamesRegistry singleton
     *
     * @return NamesRegistry
     */
    public static function connect()
    {
        if (empty(self::$registry)) {
            self::$registry = new NamesRegistry();
            self::$letters = range('A', 'Z');
        }
        return self::$registry;
    }

    /**
     * Get new unique robot name
     *
     * @todo Names rotation not implemented. Task requires all robots
     * to have unique names even if there are obsolete names.
     * @todo If there are plans to use Robot::reset() more than at least 200-300k times,
     * it should be better to generate the full list of possible names at __construct
     * and randomly reduce it on demand.
     * @todo No fallback in case of over 676000 names.
     *
     * @return string New Robot name     *
     */
    public function getNewName()
    {
        do {
            shuffle(self::$letters);
            $name = self::$letters[0] . self::$letters[1] . sprintf('%03d', mt_rand(0, 999));
        } while (!empty($this->names[$name]));
        $this->names[$name] = true;
        return $name;
    }
}

Version data entries

367 entries across 367 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.179 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.178 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.177 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.176 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.175 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.174 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.173 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.172 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.171 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.170 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.169 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.167 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.166 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.165 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.164 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.163 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.162 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.161 tracks/php/exercises/robot-name/example.php
trackler-2.2.1.160 tracks/php/exercises/robot-name/example.php