public function isValidUsername($username, $checkDb = false, $ignoreUserId = null)
    {
        /**
         * Check username doesn't contain invalid characters - we're actively looking
         * for characters which are invalid so we can say "Hey! The following
         * characters are invalid" rather than making the user guess, y'know, 'cause
         * we're good guys.
         */

        $invalidChars = '/[^a-zA-Z0-9\-_\.]/';

        //  Minimum length of the username
        $minLength = 2;

        // --------------------------------------------------------------------------

        //  Check for illegal characters
        $containsInvalidChars = preg_match($invalidChars, $username);

        if ($containsInvalidChars) {

            $msg = 'Username can only contain alpha numeric characters, underscores, periods and dashes (no spaces).';

            $this->setError($msg);
            return false;
        }

        // --------------------------------------------------------------------------

        //  Check length
        if (strlen($username) < $minLength) {
            $this->setError('Usernames must be at least ' . $minLength . ' characters long.');
            return false;
        }

        // --------------------------------------------------------------------------

        if ($checkDb) {

            $oDb = Factory::service('Database');
            $oDb->where('username', $username);

            if (!empty($ignoreUserId)) {
                $oDb->where('id !=', $ignoreUserId);
            }

            if ($oDb->count_all_results($this->table)) {
                $this->setError('Username is already in use.');
                return false;
            }
        }

        return true;
    }