Location: PHPKode > projects > Ace Framework > Ace-master/Ace/Form/Validator.php
<?php
/**
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the new BSD license.
 *
 * @package     Ace
 * @license     http://www.opensource.org/licenses/BSD-3-Clause New BSD license
 * @since       1.0
 */
abstract class Ace_Form_Validator
{
    /**
     * The global error message
     * @var array 
     */
    protected static $_global_default_messages = array('required' => 'Required field', 'invalid' => 'Invalid');

    /**
     * Parent form
     * @var Ace_Form
     */
    protected $parent = null;

    /**
     * Validator required options
     * @var array 
     */
    protected $required_options = array();

    /**
     * Validator default message
     * @var array 
     */
    protected $default_messages = array();

    /**
     * Validator default options
     * @var array 
     */
    protected $default_options = array();

    /**
     * Validator messages
     * @var array 
     */
    protected $messages = array();

    /**
     * Validator options
     * @var array 
     */
    protected $options = array();

    /**
     * Constructor.
     *
     * @param array $options
     * @param array $messages
     */
    public function __construct($options = array(), $messages = array())
    {
        $this->options  = array_merge(array('required' => true, 'trim' => false, 'empty_value' => null), $this->options);
        $this->messages = array_merge(self::$_global_default_messages, $this->messages);

        $this->addOption('throw_global_error', false);
        $this->addOption('formatter', 'Ace_Form_Validator_Formatter_Default');

        $this->configure($options, $messages);

        $this->setDefaultOptions($this->getOptions());
        $this->setDefaultMessages($this->getMessages());

        $current_option_keys = array_keys($this->options);
        $option_keys = array_keys($options);

        // check option names
        if($diff = array_diff($option_keys, array_merge($current_option_keys, $this->required_options)))
        {
            throw new InvalidArgumentException(sprintf('%s does not support the following options: "%s".', get_class($this), implode('\', \'', $diff)));
        }

        // check error code names
        if($diff = array_diff(array_keys($messages), array_keys($this->messages)))
        {
            throw new InvalidArgumentException(sprintf('%s does not support the following error codes: "%s".', get_class($this), implode('\', \'', $diff)));
        }

        // check required options
        if($diff = array_diff($this->required_options, array_merge($current_option_keys, $option_keys)))
        {
            throw new RuntimeException(sprintf('%s requires the following options: "%s".', get_class($this), implode('\', \'', $diff)));
        }

        $this->options = array_merge($this->options, $options);
        $this->messages = array_merge($this->messages, $messages);
    }

    /**
     * Returns an error message given an error code.
     *
     * @param  string $name
     * @return string
     */
    public function getMessage($name)
    {
        return isset($this->messages[$name]) ? __($this->messages[$name]) : '';
    }

    /**
     * Adds a new error code with a default error message.
     *
     * @param string $name
     * @param string $value
     */
    public function addMessage($name, $value)
    {
        $this->messages[$name] = isset(self::$_global_default_messages[$name]) ? self::$_global_default_messages[$name] : $value;
    }

    /**
     * Changes an error message given the error code.
     *
     * @param string $name
     * @param string $value
     */
    public function setMessage($name, $value)
    {
        if(!in_array($name, array_keys($this->messages)))
        {
            throw new InvalidArgumentException(sprintf('%s does not support the following error code: \'%s\'.', get_class($this), $name));
        }

        $this->messages[$name] = $value;
    }

    /**
     * Returns an array of current error messages.
     *
     * @return array
     */
    public function getMessages()
    {
        $messages = array();
        
        foreach($this->messages as $name => $message)
        {
            $messages[$name] = $this->getMessage($name);
        }

        return $messages;
    }

    /**
     * Changes all error messages.
     *
     * @param array $values
     */
    public function setMessages($values)
    {
        $this->messages = $values;
    }

    /**
     * Gets  an option value.
     *
     * @param  string $name
     * @return mixed
     */
    public function getOption($name)
    {
        return isset($this->options[$name]) ? $this->options[$name] : null;
    }

    /**
     * Adds a new option value with a default value.
     *
     * @param string $name
     * @param mixed $value
     */
    public function addOption($name, $value = null)
    {
        $this->options[$name] = $value;
    }

    /**
     * Changes an option value.
     *
     * @param string $name
     * @param mixed $value
     * @return Ace_Form_Validator
     */
    public function setOption($name, $value)
    {
        if(!in_array($name, array_merge(array_keys($this->options), $this->required_options)))
        {
            throw new InvalidArgumentException(sprintf('%s does not support the following option: \'%s\'.', get_class($this), $name));
        }

        $this->options[$name] = $value;

        return $this;
    }

    /**
     * Returns true if the option exists.
     *
     * @param  string $name
     * @return bool
     */
    public function hasOption($name)
    {
        return isset($this->options[$name]);
    }

    /**
     * Returns all options.
     *
     * @return array
     */
    public function getOptions()
    {
        return $this->options;
    }

    /**
     * Changes all options.
     *
     * @param array $values
     */
    public function setOptions($values)
    {
        $this->options = $values;
    }

    /**
     * Adds a required option.
     *
     * @param string $name
     * @return Ace_Form_Validator
     */
    public function addRequiredOption($name)
    {
        $this->required_options[] = $name;

        return $this;
    }

    /**
     * Returns all required option names.
     *
     * @return array
     */
    public function getRequiredOptions()
    {
        return $this->required_options;
    }

    /**
     * Sets the parent form
     *
     * @param Ace_Form $parent
     */
    public function setParent(Ace_Form $parent)
    {
        $this->parent = $parent;
    }

    /**
     * Gets the parent form
     *
     * @return Ace_Form
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Sets the default message for a given name.
     *
     * @param string $name
     * @param string $message
     */
    static public function setDefaultMessage($name, $message)
    {
        self::$_global_default_messages[$name] = $message;
    }

    /**
     * Returns true if the value is empty.
     *
     * @param  mixed $value
     * @return bool
     */
    protected function isEmpty($value)
    {
        return in_array($value, array(null, '', array()), true);
    }

    /**
     * Returns an empty value for this validator.
     *
     * @return mixed
     */
    protected function getEmptyValue()
    {
        return $this->getOption('empty_value');
    }

    /**
     * Returns an array of all error codes for this validator.
     *
     * @return array
     * @see getDefaultMessages()
     */
    final public function getErrorCodes()
    {
        return array_keys($this->getDefaultMessages());
    }

    /**
     * Returns default messages for all possible error codes.
     *
     * @return array
     */
    public function getDefaultMessages()
    {
        return $this->default_messages;
    }

    /**
     * Sets default messages for all possible error codes.
     *
     * @param array $messages
     */
    protected function setDefaultMessages($messages)
    {
        $this->default_messages = $messages;
    }

    /**
     * Returns default option values.
     *
     * @return array
     */
    public function getDefaultOptions()
    {
        return $this->default_options;
    }

    /**
     * Sets default option values.
     *
     * @param array $options
     */
    protected function setDefaultOptions($options)
    {
        $this->default_options = $options;
    }

    /**
     ** Cleans value.
     *
     * @param  mixed $value
     * @return mixed
     */
    public function clean($value)
    {
        $clean = $value;

        if($this->options['trim'] && is_string($clean))
        {
            $clean = trim($clean);
        }

        // empty value ?
        if($this->isEmpty($clean))
        {
            // required ?
            if($this->options['required'])
            {
                throw new Ace_Form_Validator_Exception($this, 'required');
            }

            return $this->getEmptyValue();
        }

        $class = $this->getOption('formatter');

        if(!class_exists($this->getOption('formatter')))
        {
            throw new RuntimeException(sprintf('The formatter class "%s" was not found', $class));
        }
        
        $formatter = new $class;
        return $formatter->format($this->doClean($clean));
    }

    /**
     * Configures the current validator.
     *
     * This method allows each validator to add options and error messages
     * during validator creation.
     *
     * @param array $options
     * @param array $messages
     *
     * @see __construct()
     */
    protected function configure($options = array(), $messages = array()){}

    /**
     ** Cleans value.
     *
     * @see clean
     */
    abstract protected function doClean($value);
}
Return current item: Ace Framework