12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211 |
- <?php namespace Laravel; use Closure;
- class Validator {
- /**
- * The array being validated.
- *
- * @var array
- */
- public $attributes;
- /**
- * The post-validation error messages.
- *
- * @var Messages
- */
- public $errors;
- /**
- * The validation rules.
- *
- * @var array
- */
- protected $rules = array();
- /**
- * The validation messages.
- *
- * @var array
- */
- protected $messages = array();
- /**
- * The database connection that should be used by the validator.
- *
- * @var Database\Connection
- */
- protected $db;
- /**
- * The bundle for which the validation is being run.
- *
- * @var string
- */
- protected $bundle = DEFAULT_BUNDLE;
- /**
- * The language that should be used when retrieving error messages.
- *
- * @var string
- */
- protected $language;
- /**
- * The size related validation rules.
- *
- * @var array
- */
- protected $size_rules = array('size', 'between', 'min', 'max');
- /**
- * The numeric related validation rules.
- *
- * @var array
- */
- protected $numeric_rules = array('numeric', 'integer');
- /**
- * The registered custom validators.
- *
- * @var array
- */
- protected static $validators = array();
- /**
- * Create a new validator instance.
- *
- * @param array $attributes
- * @param array $rules
- * @param array $messages
- * @return void
- */
- public function __construct($attributes, $rules, $messages = array())
- {
- foreach ($rules as $key => &$rule)
- {
- $rule = (is_string($rule)) ? explode('|', $rule) : $rule;
- }
- $this->rules = $rules;
- $this->messages = $messages;
- $this->attributes = $attributes;
- }
- /**
- * Create a new validator instance.
- *
- * @param array $attributes
- * @param array $rules
- * @param array $messages
- * @return Validator
- */
- public static function make($attributes, $rules, $messages = array())
- {
- return new static($attributes, $rules, $messages);
- }
- /**
- * Register a custom validator.
- *
- * @param string $name
- * @param Closure $validator
- * @return void
- */
- public static function register($name, $validator)
- {
- static::$validators[$name] = $validator;
- }
- /**
- * Validate the target array using the specified validation rules.
- *
- * @return bool
- */
- public function passes()
- {
- return $this->valid();
- }
- /**
- * Validate the target array using the specified validation rules.
- *
- * @return bool
- */
- public function fails()
- {
- return $this->invalid();
- }
- /**
- * Validate the target array using the specified validation rules.
- *
- * @return bool
- */
- public function invalid()
- {
- return ! $this->valid();
- }
- /**
- * Validate the target array using the specified validation rules.
- *
- * @return bool
- */
- public function valid()
- {
- $this->errors = new Messages;
- foreach ($this->rules as $attribute => $rules)
- {
- foreach ($rules as $rule) $this->check($attribute, $rule);
- }
- return count($this->errors->messages) == 0;
- }
- /**
- * Evaluate an attribute against a validation rule.
- *
- * @param string $attribute
- * @param string $rule
- * @return void
- */
- protected function check($attribute, $rule)
- {
- list($rule, $parameters) = $this->parse($rule);
- $value = array_get($this->attributes, $attribute);
- // Before running the validator, we need to verify that the attribute and rule
- // combination is actually validatable. Only the "accepted" rule implies that
- // the attribute is "required", so if the attribute does not exist, the other
- // rules will not be run for the attribute.
- $validatable = $this->validatable($rule, $attribute, $value);
- if ($validatable and ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this))
- {
- $this->error($attribute, $rule, $parameters);
- }
- }
- /**
- * Determine if an attribute is validatable.
- *
- * To be considered validatable, the attribute must either exist, or the rule
- * being checked must implicitly validate "required", such as the "required"
- * rule or the "accepted" rule.
- *
- * @param string $rule
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validatable($rule, $attribute, $value)
- {
- return $this->validate_required($attribute, $value) or $this->implicit($rule);
- }
- /**
- * Determine if a given rule implies that the attribute is required.
- *
- * @param string $rule
- * @return bool
- */
- protected function implicit($rule)
- {
- return $rule == 'required' or $rule == 'accepted' or $rule == 'required_with';
- }
- /**
- * Add an error message to the validator's collection of messages.
- *
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return void
- */
- protected function error($attribute, $rule, $parameters)
- {
- $message = $this->replace($this->message($attribute, $rule), $attribute, $rule, $parameters);
- $this->errors->add($attribute, $message);
- }
- /**
- * Validate that a required attribute exists in the attributes array.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_required($attribute, $value)
- {
- if (is_null($value))
- {
- return false;
- }
- elseif (is_string($value) and trim($value) === '')
- {
- return false;
- }
- elseif ( ! is_null(Input::file($attribute)) and is_array($value) and $value['tmp_name'] == '')
- {
- return false;
- }
- return true;
- }
- /**
- * Validate that an attribute exists in the attributes array, if another
- * attribute exists in the attributes array.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_required_with($attribute, $value, $parameters)
- {
- $other = $parameters[0];
- if ($this->validate_required($other, $this->attributes[$other]))
- {
- return $this->validate_required($attribute, $value);
- }
- return true;
- }
- /**
- * Validate that an attribute has a matching confirmation attribute.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_confirmed($attribute, $value)
- {
- return $this->validate_same($attribute, $value, array($attribute.'_confirmation'));
- }
- /**
- * Validate that an attribute was "accepted".
- *
- * This validation rule implies the attribute is "required".
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_accepted($attribute, $value)
- {
- return $this->validate_required($attribute, $value) and ($value == 'yes' or $value == '1');
- }
- /**
- * Validate that an attribute is the same as another attribute.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_same($attribute, $value, $parameters)
- {
- $other = $parameters[0];
- return isset($this->attributes[$other]) and $value == $this->attributes[$other];
- }
- /**
- * Validate that an attribute is different from another attribute.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_different($attribute, $value, $parameters)
- {
- $other = $parameters[0];
- return isset($this->attributes[$other]) and $value != $this->attributes[$other];
- }
- /**
- * Validate that an attribute is numeric.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_numeric($attribute, $value)
- {
- return is_numeric($value);
- }
- /**
- * Validate that an attribute is an integer.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_integer($attribute, $value)
- {
- return filter_var($value, FILTER_VALIDATE_INT) !== false;
- }
- /**
- * Validate the size of an attribute.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_size($attribute, $value, $parameters)
- {
- return $this->size($attribute, $value) == $parameters[0];
- }
- /**
- * Validate the size of an attribute is between a set of values.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_between($attribute, $value, $parameters)
- {
- $size = $this->size($attribute, $value);
- return $size >= $parameters[0] and $size <= $parameters[1];
- }
- /**
- * Validate the size of an attribute is greater than a minimum value.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_min($attribute, $value, $parameters)
- {
- return $this->size($attribute, $value) >= $parameters[0];
- }
- /**
- * Validate the size of an attribute is less than a maximum value.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_max($attribute, $value, $parameters)
- {
- return $this->size($attribute, $value) <= $parameters[0];
- }
- /**
- * Get the size of an attribute.
- *
- * @param string $attribute
- * @param mixed $value
- * @return mixed
- */
- protected function size($attribute, $value)
- {
- // This method will determine if the attribute is a number, string, or file and
- // return the proper size accordingly. If it is a number, the number itself is
- // the size; if it is a file, the kilobytes is the size; if it is a
- // string, the length is the size.
- if (is_numeric($value) and $this->has_rule($attribute, $this->numeric_rules))
- {
- return $this->attributes[$attribute];
- }
- elseif (array_key_exists($attribute, Input::file()))
- {
- return $value['size'] / 1024;
- }
- else
- {
- return Str::length(trim($value));
- }
- }
- /**
- * Validate an attribute is contained within a list of values.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_in($attribute, $value, $parameters)
- {
- return in_array($value, $parameters);
- }
- /**
- * Validate an attribute is not contained within a list of values.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_not_in($attribute, $value, $parameters)
- {
- return ! in_array($value, $parameters);
- }
- /**
- * Validate the uniqueness of an attribute value on a given database table.
- *
- * If a database column is not specified, the attribute will be used.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_unique($attribute, $value, $parameters)
- {
- // We allow the table column to be specified just in case the column does
- // not have the same name as the attribute. It must be within the second
- // parameter position, right after the database table name.
- if (isset($parameters[1]))
- {
- $attribute = $parameters[1];
- }
- $query = $this->db()->table($parameters[0])->where($attribute, '=', $value);
- // We also allow an ID to be specified that will not be included in the
- // uniqueness check. This makes updating columns easier since it is
- // fine for the given ID to exist in the table.
- if (isset($parameters[2]))
- {
- $id = (isset($parameters[3])) ? $parameters[3] : 'id';
- $query->where($id, '<>', $parameters[2]);
- }
- return $query->count() == 0;
- }
- /**
- * Validate the existence of an attribute value in a database table.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_exists($attribute, $value, $parameters)
- {
- if (isset($parameters[1])) $attribute = $parameters[1];
- // Grab the number of elements we are looking for. If the given value is
- // in array, we'll count all of the values in the array, otherwise we
- // can just make sure the count is greater or equal to one.
- $count = (is_array($value)) ? count($value) : 1;
- $query = $this->db()->table($parameters[0]);
- // If the given value is an array, we will check for the existence of
- // all the values in the database, otherwise we'll check for the
- // presence of the single given value in the database.
- if (is_array($value))
- {
- $query = $query->where_in($attribute, $value);
- }
- else
- {
- $query = $query->where($attribute, '=', $value);
- }
- return $query->count() >= $count;
- }
- /**
- * Validate that an attribute is a valid IP.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_ip($attribute, $value)
- {
- return filter_var($value, FILTER_VALIDATE_IP) !== false;
- }
- /**
- * Validate that an attribute is a valid e-mail address.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_email($attribute, $value)
- {
- return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
- }
- /**
- * Validate that an attribute is a valid URL.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_url($attribute, $value)
- {
- return filter_var($value, FILTER_VALIDATE_URL) !== false;
- }
- /**
- * Validate that an attribute is an active URL.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_active_url($attribute, $value)
- {
- $url = str_replace(array('http://', 'https://', 'ftp://'), '', Str::lower($value));
- return (trim($url) !== '') ? checkdnsrr($url) : false;
- }
- /**
- * Validate the MIME type of a file is an image MIME type.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_image($attribute, $value)
- {
- return $this->validate_mimes($attribute, $value, array('jpg', 'png', 'gif', 'bmp'));
- }
- /**
- * Validate that an attribute contains only alphabetic characters.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_alpha($attribute, $value)
- {
- return preg_match('/^([a-z])+$/i', $value);
- }
- /**
- * Validate that an attribute contains only alpha-numeric characters.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_alpha_num($attribute, $value)
- {
- return preg_match('/^([a-z0-9])+$/i', $value);
- }
- /**
- * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_alpha_dash($attribute, $value)
- {
- return preg_match('/^([-a-z0-9_-])+$/i', $value);
- }
- /**
- * Validate that an attribute passes a regular expression check.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_match($attribute, $value, $parameters)
- {
- return preg_match($parameters[0], $value);
- }
- /**
- * Validate the MIME type of a file upload attribute is in a set of MIME types.
- *
- * @param string $attribute
- * @param array $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_mimes($attribute, $value, $parameters)
- {
- if ( ! is_array($value) or array_get($value, 'tmp_name', '') == '') return true;
- foreach ($parameters as $extension)
- {
- if (File::is($extension, $value['tmp_name']))
- {
- return true;
- }
- }
- return false;
- }
- /**
- * Validate that an attribute is an array
- *
- * @param string $attribute
- * @param mixed $value
- * @return bool
- */
- protected function validate_array($attribute, $value)
- {
- return is_array($value);
- }
- /**
- * Validate that an attribute of type array has a specific count
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_count($attribute, $value, $parameters)
- {
- return (is_array($value) && count($value) == $parameters[0]);
- }
- /**
- * Validate that an attribute of type array has a minimum of elements.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_countmin($attribute, $value, $parameters)
- {
- return (is_array($value) && count($value) >= $parameters[0]);
- }
- /**
- * Validate that an attribute of type array has a maximum of elements.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_countmax($attribute, $value, $parameters)
- {
- return (is_array($value) && count($value) <= $parameters[0]);
- }
- /**
- * Validate that an attribute of type array has elements between max and min.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_countbetween($attribute, $value, $parameters)
- {
- return (is_array($value) && count($value) >= $parameters[0] && count($value) <= $parameters[1] );
- }
- /**
- * Validate the date is before a given date.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_before($attribute, $value, $parameters)
- {
- return (strtotime($value) < strtotime($parameters[0]));
- }
- /**
- * Validate the date is after a given date.
- *
- * @param string $attribute
- * @param mixed $value
- * @param array $parameters
- * @return bool
- */
- protected function validate_after($attribute, $value, $parameters)
- {
- return (strtotime($value) > strtotime($parameters[0]));
- }
- /**
- * Get the proper error message for an attribute and rule.
- *
- * @param string $attribute
- * @param string $rule
- * @return string
- */
- protected function message($attribute, $rule)
- {
- $bundle = Bundle::prefix($this->bundle);
- // First we'll check for developer specified, attribute specific messages.
- // These messages take first priority. They allow the fine-grained tuning
- // of error messages for each rule.
- $custom = $attribute.'_'.$rule;
- if (array_key_exists($custom, $this->messages))
- {
- return $this->messages[$custom];
- }
- elseif (Lang::has($custom = "{$bundle}validation.custom.{$custom}", $this->language))
- {
- return Lang::line($custom)->get($this->language);
- }
- // Next we'll check for developer specified, rule specific error messages.
- // These allow the developer to override the error message for an entire
- // rule, regardless of the attribute being validated by that rule.
- elseif (array_key_exists($rule, $this->messages))
- {
- return $this->messages[$rule];
- }
- // If the rule being validated is a "size" rule, we will need to gather
- // the specific size message for the type of attribute being validated,
- // either a number, file, or string.
- elseif (in_array($rule, $this->size_rules))
- {
- return $this->size_message($bundle, $attribute, $rule);
- }
- // If no developer specified messages have been set, and no other special
- // messages apply to the rule, we will just pull the default validation
- // message from the validation language file.
- else
- {
- $line = "{$bundle}validation.{$rule}";
- return Lang::line($line)->get($this->language);
- }
- }
- /**
- * Get the proper error message for an attribute and size rule.
- *
- * @param string $bundle
- * @param string $attribute
- * @param string $rule
- * @return string
- */
- protected function size_message($bundle, $attribute, $rule)
- {
- // There are three different types of size validations. The attribute
- // may be either a number, file, or a string, so we'll check a few
- // things to figure out which one it is.
- if ($this->has_rule($attribute, $this->numeric_rules))
- {
- $line = 'numeric';
- }
- // We assume that attributes present in the $_FILES array are files,
- // which makes sense. If the attribute doesn't have numeric rules
- // and isn't a file, it's a string.
- elseif (array_key_exists($attribute, Input::file()))
- {
- $line = 'file';
- }
- else
- {
- $line = 'string';
- }
- return Lang::line("{$bundle}validation.{$rule}.{$line}")->get($this->language);
- }
- /**
- * Replace all error message place-holders with actual values.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace($message, $attribute, $rule, $parameters)
- {
- $message = str_replace(':attribute', $this->attribute($attribute), $message);
- if (method_exists($this, $replacer = 'replace_'.$rule))
- {
- $message = $this->$replacer($message, $attribute, $rule, $parameters);
- }
- return $message;
- }
- /**
- * Replace all place-holders for the between rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_between($message, $attribute, $rule, $parameters)
- {
- return str_replace(array(':min', ':max'), $parameters, $message);
- }
- /**
- * Replace all place-holders for the size rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_size($message, $attribute, $rule, $parameters)
- {
- return str_replace(':size', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the min rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_min($message, $attribute, $rule, $parameters)
- {
- return str_replace(':min', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the max rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_max($message, $attribute, $rule, $parameters)
- {
- return str_replace(':max', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the in rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_in($message, $attribute, $rule, $parameters)
- {
- return str_replace(':values', implode(', ', $parameters), $message);
- }
- /**
- * Replace all place-holders for the not_in rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_not_in($message, $attribute, $rule, $parameters)
- {
- return str_replace(':values', implode(', ', $parameters), $message);
- }
- /**
- * Replace all place-holders for the mimes rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_mimes($message, $attribute, $rule, $parameters)
- {
- return str_replace(':values', implode(', ', $parameters), $message);
- }
- /**
- * Replace all place-holders for the same rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_same($message, $attribute, $rule, $parameters)
- {
- return str_replace(':other', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the different rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_different($message, $attribute, $rule, $parameters)
- {
- return str_replace(':other', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the before rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_before($message, $attribute, $rule, $parameters)
- {
- return str_replace(':date', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the after rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_after($message, $attribute, $rule, $parameters)
- {
- return str_replace(':date', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the count rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_count($message, $attribute, $rule, $parameters)
- {
- return str_replace(':count', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the countmin rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_countmin($message, $attribute, $rule, $parameters)
- {
- return str_replace(':min', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the countmax rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_countmax($message, $attribute, $rule, $parameters)
- {
- return str_replace(':max', $parameters[0], $message);
- }
- /**
- * Replace all place-holders for the between rule.
- *
- * @param string $message
- * @param string $attribute
- * @param string $rule
- * @param array $parameters
- * @return string
- */
- protected function replace_countbetween($message, $attribute, $rule, $parameters)
- {
- return str_replace(array(':min', ':max'), $parameters, $message);
- }
- /**
- * Get the displayable name for a given attribute.
- *
- * @param string $attribute
- * @return string
- */
- protected function attribute($attribute)
- {
- $bundle = Bundle::prefix($this->bundle);
- // More reader friendly versions of the attribute names may be stored
- // in the validation language file, allowing a more readable version
- // of the attribute name in the message.
- $line = "{$bundle}validation.attributes.{$attribute}";
- if (Lang::has($line, $this->language))
- {
- return Lang::line($line)->get($this->language);
- }
- // If no language line has been specified for the attribute, all of
- // the underscores are removed from the attribute name and that
- // will be used as the attribute name.
- else
- {
- return str_replace('_', ' ', $attribute);
- }
- }
- /**
- * Determine if an attribute has a rule assigned to it.
- *
- * @param string $attribute
- * @param array $rules
- * @return bool
- */
- protected function has_rule($attribute, $rules)
- {
- foreach ($this->rules[$attribute] as $rule)
- {
- list($rule, $parameters) = $this->parse($rule);
- if (in_array($rule, $rules)) return true;
- }
- return false;
- }
- /**
- * Extract the rule name and parameters from a rule.
- *
- * @param string $rule
- * @return array
- */
- protected function parse($rule)
- {
- $parameters = array();
- // The format for specifying validation rules and parameters follows a
- // {rule}:{parameters} formatting convention. For instance, the rule
- // "max:3" specifies that the value may only be 3 characters long.
- if (($colon = strpos($rule, ':')) !== false)
- {
- $parameters = str_getcsv(substr($rule, $colon + 1));
- }
- return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
- }
- /**
- * Set the bundle that the validator is running for.
- *
- * The bundle determines which bundle the language lines will be loaded from.
- *
- * @param string $bundle
- * @return Validator
- */
- public function bundle($bundle)
- {
- $this->bundle = $bundle;
- return $this;
- }
- /**
- * Set the language that should be used when retrieving error messages.
- *
- * @param string $language
- * @return Validator
- */
- public function speaks($language)
- {
- $this->language = $language;
- return $this;
- }
- /**
- * Set the database connection that should be used by the validator.
- *
- * @param Database\Connection $connection
- * @return Validator
- */
- public function connection(Database\Connection $connection)
- {
- $this->db = $connection;
- return $this;
- }
- /**
- * Get the database connection for the Validator.
- *
- * @return Database\Connection
- */
- protected function db()
- {
- if ( ! is_null($this->db)) return $this->db;
- return $this->db = Database::connection();
- }
- /**
- * Dynamically handle calls to custom registered validators.
- */
- public function __call($method, $parameters)
- {
- // First we will slice the "validate_" prefix off of the validator since
- // custom validators aren't registered with such a prefix, then we can
- // just call the method with the given parameters.
- if (isset(static::$validators[$method = substr($method, 9)]))
- {
- return call_user_func_array(static::$validators[$method], $parameters);
- }
- throw new \Exception("Method [$method] does not exist.");
- }
- }
|