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


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

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

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

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is not email 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('email LIKE ?', $value));


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

        return true;
    }

}
