<?php
/**
 * Implementation of the validator for more url
 *
 * @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
 */

/**
 * Validator for more urls, each url can be separated by defined separator.
 *
 * @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_MoreUrlValid extends Zend_Validate_Abstract
{

    const MSG_URL = 'urlString';
    
    protected $_messageTemplates = array(self::MSG_URL => "'%value%' is not sequence of urls");

    /**
     * separator for the urls, default separator is space
     * @var string
     */
    private $_separator = " ";

    /**
     * Separator setter.
     * @var $separator string
     */
    public function setSeparator($separator)
    {
        $this->_separator = $separator;
    }

    public function isValid($value)
    {
        $this->_setValue($value);

        $urlArr = explode(" ", $value);
        $urlValidator = new MKLib_UrlValid();

        foreach($urlArr as $url) {
            //local url e.g. "/blog/entry.php", add http://serverName
            if (preg_match("|^/[a-z]|", $url)) {
                $url = MKLib_AppBaseUrl::getUri() . $url;
            }
            //validate one URL
            if (!$urlValidator->isValid($url)) {
                $this->_error();
                return false;
            }
        }
        return true;
    }
}
