Abstract.php #1

  • //
  • guest/
  • perforce_software/
  • chronicle/
  • main/
  • library/
  • P4/
  • Validate/
  • Abstract.php
  • View
  • Commits
  • Open Download .zip Download (2 KB)
<?php
/**
 * Provides validator abstract with basic error message handling.
 *
 * @copyright   2011 Perforce Software. All rights reserved.
 * @license     Please see LICENSE.txt in top-level folder of this distribution.
 * @version     <release>/<patch>
 */
abstract class P4_Validate_Abstract implements P4_Validate_Interface
{
    protected   $_value             = null;
    protected   $_messages          = array();
    protected   $_messageTemplates  = array();
    
    /**
     * Get errors for the most recent isValid() check.
     *
     * @return  array   list of error messages.
     */
    public function getMessages()
    {
        return $this->_messages;
    }

    /**
     * Get the message templates for this validator.
     * 
     * @return  array   list of error message templates.
     */
    public function getMessageTemplates()
    {
        return $this->_messageTemplates;
    }

    /**
     * Record an error detected during validation.
     * Replaces '%value%' with the value being validated.
     *
     * @param   string  $messageKey     the id of the message to add.
     */
    protected function _error($messageKey)
    {
        if (!array_key_exists($messageKey, $this->_messageTemplates)) {
            throw new InvalidArgumentException(
                "Cannot set error. Invalid message key given."
            );
        }

        // support %value% substitution.
        $value = is_object($this->_value) 
            ? get_class($this->_value)
            : (string) $this->_value;
        $message = $this->_messageTemplates[$messageKey];
        $message = str_replace('%value%', (string) $value, $message);

        $this->_messages[$messageKey] = $message;
    }

    /**
     * Sets the value being validated and clears the messages.
     *
     * @param   mixed   $value  the value being validated.
     */
    protected function _setValue($value)
    {
        $this->_value    = $value;
        $this->_messages = array();
    }
}
# Change User Description Committed
#1 16170 perforce_software Move Chronicle files to follow new path scheme for branching.
//guest/perforce_software/chronicle/library/P4/Validate/Abstract.php
#1 8972 Matt Attaway Initial add of the Chronicle source code