<?php

/**
 * Goweb Sitemanager 5 Framework
 * 
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Validate
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */

/**
 * Goweb_Validate
 */
Goweb::loadClass('Zend_Validate_Abstract');
Goweb::loadClass('Zend_Uri_Http');

/**
 * Classe para validar do tipo endereço
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Validate
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_Validate_Url extends Zend_Validate_Abstract {
    /**
     * Validation failure message key for when the value contains non-url
     */
    const INVALID_URL = 'invalidUrl';
    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::INVALID_URL   => "'%value%' is not a valid URL.",
    );

    /**
     * Constructor
     *
     * @param  string  $errorMessage
     */
    public function __construct($errorMessage) {
        foreach (array_keys($this->_messageTemplates) as $key) $this->setMessage($errorMessage, $key);
    }
    
    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value contains an url
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $valueString = (string) $value;
        $this->_setValue($valueString);

        if (!Zend_Uri_Http::check($value)) {
            $this->_error(self::INVALID_URL);
            return false;
        }
        return true;
    } 
    
}