Flikore Validator
  • Api
  • Namespace
  • Class
  • Tree
  • Deprecated

Namespaces

  • Flikore
    • Validator
      • Exception
      • Interfaces
      • Intl
      • Validators

Classes

  • ValidationChoice
  • ValidationCombo
  • ValidationKey
  • ValidationSet
  • ValidationValue
  • Validator
  1 <?php
  2 
  3 /**
  4  * The MIT License
  5  *
  6  * Copyright 2014 George Marques <george at georgemarques.com.br>.
  7  *
  8  * Permission is hereby granted, free of charge, to any person obtaining a copy
  9  * of this software and associated documentation files (the "Software"), to deal
 10  * in the Software without restriction, including without limitation the rights
 11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  * copies of the Software, and to permit persons to whom the Software is
 13  * furnished to do so, subject to the following conditions:
 14  *
 15  * The above copyright notice and this permission notice shall be included in
 16  * all copies or substantial portions of the Software.
 17  *
 18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  * THE SOFTWARE.
 25  */
 26 
 27 namespace Flikore\Validator;
 28 
 29 /**
 30  * The base for validation classes.
 31  *
 32  * @author George Marques <george at georgemarques.com.br>
 33  * @version 0.5.2
 34  * @since 0.1
 35  * @license http://opensource.org/licenses/MIT MIT
 36  * @copyright (c) 2014, George Marques
 37  * @package Flikore\Validator
 38  */
 39 abstract class Validator implements Interfaces\IValidator
 40 {
 41 
 42     /**
 43      * The error message for this validator.
 44      * @var string The error message for this validator.
 45      */
 46     protected $message = 'The %key% must be valid.';
 47 
 48     /**
 49      * Stores the values to change in the template.
 50      * @var array Stores the values to change in the template.
 51      */
 52     protected $values = array(
 53         'key' => 'value'
 54     );
 55 
 56     /**
 57      * Checks if the value passes the validation test.
 58      * @param mixed $value The value to test.
 59      * @return boolean Whether it passes the test or not.
 60      */
 61     public function validate($value)
 62     {
 63         return $this->doValidate($value);
 64     }
 65 
 66     /**
 67      * Checks if the value passes the validation test and throws
 68      * an exception if not.
 69      * @param mixed $value The value to test.
 70      * @throws Exception\ValidatorException
 71      */
 72     public function assert($value)
 73     {
 74         if (!$this->doValidate($value))
 75         {
 76             throw new Exception\ValidatorException($this->getErrorMessage());
 77         }
 78     }
 79 
 80     /**
 81      * Gets the error message for this validation.
 82      * This should work whether or not there was a test before.
 83      * @return string The error message.
 84      */
 85     public function getErrorMessage()
 86     {
 87         return $this->applyTemplate();
 88     }
 89 
 90     /**
 91      * Sets the error message for this validator.
 92      * @param string $message The message.
 93      */
 94     public function setErrorMessage($message)
 95     {
 96         $this->message = $message;
 97     }
 98 
 99     /**
100      * Adds a new key-value pair to be replaced by the templating engine.
101      * This does not check if it's replacing a specific validator value.
102      * 
103      * @param string $key The key to replace (in the template as "%key%")
104      * @param string $value The value to be inserted instead of the key.
105      */
106     public function addKeyValue($key, $value)
107     {
108         if(is_object($value) && !method_exists($value, '__toString'))
109         {
110             $value = get_class($value);
111         }
112         $this->values[$key] = (string) $value;
113     }
114 
115     /**
116      * Applies the template message to a formed one.
117      * @return string The formed message.
118      */
119     protected function applyTemplate()
120     {
121         $message = Intl\GetText::_d('Flikore.Validator', $this->message);
122         foreach ($this->values as $key => $value)
123         {
124             $message = str_replace("%$key%", Intl\GetText::_d('Flikore.Validator', $value), $message);
125         }
126         return $message;
127     }
128 
129     /**
130      * Executes the real validation so it can be reused.
131      * @param mixed $value The value to validate.
132      * @return boolean Whether the value pass the validation.
133      */
134     protected abstract function doValidate($value);
135     
136     /**
137      * Checks if a value is considered empty, so the derived 
138      * validators can have a standard.
139      * 
140      * @param mixed $value The value to check.
141      * @return boolean Whether it is empty or not.
142      */
143     protected function isEmpty($value)
144     {
145         if (is_null($value) || $value === '')
146         {
147             return true;
148         }
149         else
150         {
151             return false;
152         }
153     }
154 }
155 
Flikore Validator API documentation generated by ApiGen 2.8.0