1/* 2 +------------------------------------------------------------------------+ 3 | Phalcon Framework | 4 +------------------------------------------------------------------------+ 5 | Copyright (c) 2011-2017 Phalcon Team (https://phalconphp.com) | 6 +------------------------------------------------------------------------+ 7 | This source file is subject to the New BSD License that is bundled | 8 | with this package in the file LICENSE.txt. | 9 | | 10 | If you did not receive a copy of the license and are unable to | 11 | obtain it through the world-wide-web, please send an email | 12 | to license@phalconphp.com so we can send you a copy immediately. | 13 +------------------------------------------------------------------------+ 14 | Authors: Andres Gutierrez <andres@phalconphp.com> | 15 | Eduar Carvajal <eduar@phalconphp.com> | 16 +------------------------------------------------------------------------+ 17 */ 18 19namespace Phalcon\Validation; 20 21use Phalcon\Validation; 22use Phalcon\Validation\Exception; 23use Phalcon\Validation\ValidatorInterface; 24 25/** 26 * Phalcon\Validation\Validator 27 * 28 * This is a base class for validators 29 */ 30abstract class Validator implements ValidatorInterface 31{ 32 protected _options; 33 34 /** 35 * Phalcon\Validation\Validator constructor 36 */ 37 public function __construct(array! options = null) 38 { 39 let this->_options = options; 40 } 41 42 /** 43 * Checks if an option has been defined 44 45 * @deprecated since 2.1.0 46 * @see \Phalcon\Validation\Validator::hasOption() 47 */ 48 deprecated public function isSetOption(string! key) -> boolean 49 { 50 return isset this->_options[key]; 51 } 52 53 /** 54 * Checks if an option is defined 55 */ 56 public function hasOption(string! key) -> boolean 57 { 58 return isset this->_options[key]; 59 } 60 61 /** 62 * Returns an option in the validator's options 63 * Returns null if the option hasn't set 64 */ 65 public function getOption(string! key, var defaultValue = null) -> var 66 { 67 var options, value, fieldValue; 68 let options = this->_options; 69 70 if typeof options == "array" { 71 if fetch value, options[key] { 72 /* 73 * If we have attribute it means it's Uniqueness validator, we 74 * can have here multiple fields, so we need to check it 75 */ 76 if key == "attribute" && typeof value == "array" { 77 if fetch fieldValue, value[key] { 78 return fieldValue; 79 } 80 } 81 return value; 82 } 83 } 84 85 return defaultValue; 86 } 87 88 /** 89 * Sets an option in the validator 90 */ 91 public function setOption(string! key, value) -> void 92 { 93 let this->_options[key] = value; 94 } 95 96 /** 97 * Executes the validation 98 */ 99 abstract public function validate(<Validation> validation, string! attribute) -> boolean; 100 101 /** 102 * Prepares a label for the field. 103 */ 104 protected function prepareLabel(<Validation> validation, string! field) -> var 105 { 106 var label; 107 108 let label = this->getOption("label"); 109 if typeof label == "array" { 110 let label = label[field]; 111 } 112 113 if empty label { 114 let label = validation->getLabel(field); 115 } 116 117 return label; 118 } 119 120 /** 121 * Prepares a validation message. 122 */ 123 protected function prepareMessage(<Validation> validation, string! field, string! type, string! option = "message") -> var 124 { 125 var message; 126 127 let message = this->getOption(option); 128 if typeof message == "array" { 129 let message = message[field]; 130 } 131 132 if empty message { 133 let message = validation->getDefaultMessage(type); 134 } 135 136 return message; 137 } 138 139 /** 140 * Prepares a validation code. 141 */ 142 protected function prepareCode(string! field) -> int | null 143 { 144 var code; 145 146 let code = this->getOption("code"); 147 if typeof code == "array" { 148 let code = code[field]; 149 } 150 151 return code; 152 } 153} 154