<?php
/**
 * @see Zend_Validate_Abstract
 */
require_once 'Zend/Validate/Abstract.php';


/**
 * @category   Malf
 * @package    Malf_Validate
 */

class Zend_Validate_MalfLoginUnique extends Zend_Validate_Abstract
{
    /**
     * Validation failure message key for when the value given is already used by registered user
     */
    const LOGIN_NOT_UNIQUE = 'loginNotUnique';

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::LOGIN_NOT_UNIQUE    => "Login '%value%' is already used by registered user.",
    );

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is not login used by registered user
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $valueString = (string) $value;
        $this->_setValue($value);
        
        $users = new Users();
        $row = $users->fetchRow($users->select()->where('login LIKE ?', $value));


        if ($row) {
            $this->_error(self::LOGIN_NOT_UNIQUE);
            return false;
        }

        return true;
    }

}
