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.0.6.0 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.18 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.17 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.16 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.15 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.14 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.13 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.12 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.11 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.10 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.9 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.8 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.7 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.6 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.5 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.4 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.3 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.2 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.1 tracks/php/exercises/robot-name/example.php
trackler-2.0.5.0 tracks/php/exercises/robot-name/example.php