<?php
/**
 * Implementation of the http(s) url validator
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * The Zend Framework validator for http(s) urls.
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class MKLib_UrlValid extends Zend_Validate_Abstract
{
    /** message */
    const MSG_URL = 'notUrlString';

    /** error message template
     * @var array
     */
    protected $_messageTemplates = array(self::MSG_URL => "'%value%' is not valid Url");

    /**
     * Main validation function
     * @param string $value
     * @return bool
     */
    public function isValid($value)
    {
        $this->_setValue($value);
        $str = strtolower($value);
        if (!preg_match("|^http(s)?://|", $str)) {
            $str = "http://" . $str;
        }
        if (!preg_match('|^http(s)?://[a-z0-9-]+(\.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|', $str)) {
            //only one message, so self MSG_URL is used
            $this->_error();
            return false;
        }
        return true;
    }
}
