validator.php 28.6 KB
Newer Older
1
<?php namespace Laravel; use Closure;
2 3 4 5

class Validator {

	/**
6
	 * The array being validated.
7 8 9
	 *
	 * @var array
	 */
10 11 12 13 14 15 16 17
	public $attributes;

	/**
	 * The post-validation error messages.
	 *
	 * @var Messages
	 */
	public $errors;
18 19

	/**
20 21 22 23
	 * The validation rules.
	 *
	 * @var array
	 */
24
	protected $rules = array();
25 26 27

	/**
	 * The validation messages.
28
	 *
29 30
	 * @var array
	 */
31
	protected $messages = array();
32 33

	/**
34 35 36 37
	 * The database connection that should be used by the validator.
	 *
	 * @var Database\Connection
	 */
38
	protected $db;
39 40

	/**
41 42 43 44 45 46 47
	 * The bundle for which the validation is being run.
	 *
	 * @var string
	 */
	protected $bundle = DEFAULT_BUNDLE;

	/**
48
	 * The language that should be used when retrieving error messages.
49
	 *
50
	 * @var string
51
	 */
52
	protected $language;
53 54

	/**
Taylor Otwell committed
55
	 * The size related validation rules.
56
	 *
57
	 * @var array
58
	 */
Taylor Otwell committed
59
	protected $size_rules = array('size', 'between', 'min', 'max');
60 61

	/**
Taylor Otwell committed
62
	 * The numeric related validation rules.
63 64 65
	 *
	 * @var array
	 */
Taylor Otwell committed
66
	protected $numeric_rules = array('numeric', 'integer');
67 68

	/**
Taylor Otwell committed
69
	 * The registered custom validators.
70 71 72
	 *
	 * @var array
	 */
Taylor Otwell committed
73
	protected static $validators = array();
74 75

	/**
76 77
	 * Create a new validator instance.
	 *
78 79 80
	 * @param  array  $attributes
	 * @param  array  $rules
	 * @param  array  $messages
81 82
	 * @return void
	 */
83
	public function __construct($attributes, $rules, $messages = array())
84
	{
85 86 87 88 89
		foreach ($rules as $key => &$rule)
		{
			$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
		}

90
		$this->rules = $rules;
91
		$this->messages = $messages;
92
		$this->attributes = $attributes;
93 94 95
	}

	/**
96 97
	 * Create a new validator instance.
	 *
98 99 100
	 * @param  array      $attributes
	 * @param  array      $rules
	 * @param  array      $messages
101 102 103 104 105 106 107 108
	 * @return Validator
	 */
	public static function make($attributes, $rules, $messages = array())
	{
		return new static($attributes, $rules, $messages);
	}

	/**
109 110 111 112 113 114 115 116 117 118 119 120
	 * Register a custom validator.
	 *
	 * @param  string   $name
	 * @param  Closure  $validator
	 * @return void
	 */
	public static function register($name, $validator)
	{
		static::$validators[$name] = $validator;
	}

	/**
121 122 123 124
	 * Validate the target array using the specified validation rules.
	 *
	 * @return bool
	 */
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	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
	 */
145 146 147 148 149 150 151 152 153 154 155 156
	public function invalid()
	{
		return ! $this->valid();
	}

	/**
	 * Validate the target array using the specified validation rules.
	 *
	 * @return bool
	 */
	public function valid()
	{
157
		$this->errors = new Messages;
158 159 160

		foreach ($this->rules as $attribute => $rules)
		{
161
			foreach ($rules as $rule) $this->check($attribute, $rule);
162 163 164 165
		}

		return count($this->errors->messages) == 0;
	}
166 167

	/**
168
	 * Evaluate an attribute against a validation rule.
169
	 *
170 171
	 * @param  string  $attribute
	 * @param  string  $rule
172 173
	 * @return void
	 */
174
	protected function check($attribute, $rule)
175
	{
176
		list($rule, $parameters) = $this->parse($rule);
177

178
		$value = array_get($this->attributes, $attribute);
179

180 181 182 183
		// 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.
184
		$validatable = $this->validatable($rule, $attribute, $value);
185

186
		if ($validatable and ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this))
187
		{
188
			$this->error($attribute, $rule, $parameters);
189 190 191 192
		}
	}

	/**
193 194
	 * Determine if an attribute is validatable.
	 *
195 196 197
	 * 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.
198 199 200 201 202 203 204 205
	 *
	 * @param  string  $rule
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validatable($rule, $attribute, $value)
	{
206 207 208 209 210 211 212 213 214 215 216
		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)
	{
217
		return $rule == 'required' or $rule == 'accepted' or $rule == 'required_with';
218 219 220
	}

	/**
221 222 223 224 225 226 227 228 229
	 * 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)
	{
230
		$message = $this->replace($this->message($attribute, $rule), $attribute, $rule, $parameters);
231 232 233 234 235

		$this->errors->add($attribute, $message);
	}

	/**
236 237 238
	 * Validate that a required attribute exists in the attributes array.
	 *
	 * @param  string  $attribute
239
	 * @param  mixed   $value
240 241
	 * @return bool
	 */
242
	protected function validate_required($attribute, $value)
243
	{
244 245 246 247 248 249 250 251
		if (is_null($value))
		{
			return false;
		}
		elseif (is_string($value) and trim($value) === '')
		{
			return false;
		}
252
		elseif ( ! is_null(Input::file($attribute)) and is_array($value) and $value['tmp_name'] == '')
253 254 255
		{
			return false;
		}
256 257

		return true;
258 259 260
	}

	/**
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	 * 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;
	}

	/**
282 283 284
	 * Validate that an attribute has a matching confirmation attribute.
	 *
	 * @param  string  $attribute
285
	 * @param  mixed   $value
286 287
	 * @return bool
	 */
288
	protected function validate_confirmed($attribute, $value)
289
	{
290
		return $this->validate_same($attribute, $value, array($attribute.'_confirmation'));
291 292 293 294 295 296 297 298
	}

	/**
	 * Validate that an attribute was "accepted".
	 *
	 * This validation rule implies the attribute is "required".
	 *
	 * @param  string  $attribute
299
	 * @param  mixed   $value
300 301
	 * @return bool
	 */
302
	protected function validate_accepted($attribute, $value)
303
	{
304
		return $this->validate_required($attribute, $value) and ($value == 'yes' or $value == '1');
305 306 307
	}

	/**
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	 * 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];
	}

	/**
338 339 340
	 * Validate that an attribute is numeric.
	 *
	 * @param  string  $attribute
341
	 * @param  mixed   $value
342 343
	 * @return bool
	 */
344
	protected function validate_numeric($attribute, $value)
345
	{
346
		return is_numeric($value);
347 348 349 350 351 352
	}

	/**
	 * Validate that an attribute is an integer.
	 *
	 * @param  string  $attribute
353
	 * @param  mixed   $value
354 355
	 * @return bool
	 */
356
	protected function validate_integer($attribute, $value)
357
	{
358
		return filter_var($value, FILTER_VALIDATE_INT) !== false;
359 360 361 362 363 364
	}

	/**
	 * Validate the size of an attribute.
	 *
	 * @param  string  $attribute
365
	 * @param  mixed   $value
366 367 368
	 * @param  array   $parameters
	 * @return bool
	 */
369
	protected function validate_size($attribute, $value, $parameters)
370
	{
371
		return $this->size($attribute, $value) == $parameters[0];
372 373 374 375 376 377
	}

	/**
	 * Validate the size of an attribute is between a set of values.
	 *
	 * @param  string  $attribute
378
	 * @param  mixed   $value
379 380 381
	 * @param  array   $parameters
	 * @return bool
	 */
382
	protected function validate_between($attribute, $value, $parameters)
383
	{
384 385 386
		$size = $this->size($attribute, $value);

		return $size >= $parameters[0] and $size <= $parameters[1];
387 388 389
	}

	/**
390
	 * Validate the size of an attribute is greater than a minimum value.
391
	 *
392
	 * @param  string  $attribute
393
	 * @param  mixed   $value
394 395
	 * @param  array   $parameters
	 * @return bool
396
	 */
397
	protected function validate_min($attribute, $value, $parameters)
398
	{
399
		return $this->size($attribute, $value) >= $parameters[0];
400 401 402
	}

	/**
403
	 * Validate the size of an attribute is less than a maximum value.
404
	 *
405
	 * @param  string  $attribute
406
	 * @param  mixed   $value
407
	 * @param  array   $parameters
408 409
	 * @return bool
	 */
410
	protected function validate_max($attribute, $value, $parameters)
411
	{
412
		return $this->size($attribute, $value) <= $parameters[0];
413
	}
414

415 416 417 418
	/**
	 * Get the size of an attribute.
	 *
	 * @param  string  $attribute
419
	 * @param  mixed   $value
420 421
	 * @return mixed
	 */
422
	protected function size($attribute, $value)
423
	{
424
	 	// This method will determine if the attribute is a number, string, or file and
Chris Berthe committed
425 426
	 	// 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
427
	 	// string, the length is the size.
428
		if (is_numeric($value) and $this->has_rule($attribute, $this->numeric_rules))
429 430 431
		{
			return $this->attributes[$attribute];
		}
432
		elseif (array_key_exists($attribute, Input::file()))
433 434 435 436 437 438 439
		{
			return $value['size'] / 1024;
		}
		else
		{
			return Str::length(trim($value));
		}
440 441 442
	}

	/**
443 444 445
	 * Validate an attribute is contained within a list of values.
	 *
	 * @param  string  $attribute
446
	 * @param  mixed   $value
447 448 449
	 * @param  array   $parameters
	 * @return bool
	 */
450
	protected function validate_in($attribute, $value, $parameters)
451
	{
452
		return in_array($value, $parameters);
453 454 455 456 457 458
	}

	/**
	 * Validate an attribute is not contained within a list of values.
	 *
	 * @param  string  $attribute
459
	 * @param  mixed   $value
460 461
	 * @param  array   $parameters
	 * @return bool
462
	 */
463
	protected function validate_not_in($attribute, $value, $parameters)
464
	{
465
		return ! in_array($value, $parameters);
466 467 468 469 470
	}

	/**
	 * Validate the uniqueness of an attribute value on a given database table.
	 *
471
	 * If a database column is not specified, the attribute will be used.
472
	 *
473
	 * @param  string  $attribute
474
	 * @param  mixed   $value
475 476 477
	 * @param  array   $parameters
	 * @return bool
	 */
478
	protected function validate_unique($attribute, $value, $parameters)
479
	{
480
		// We allow the table column to be specified just in case the column does
Taylor Otwell committed
481
		// not have the same name as the attribute. It must be within the second
482
		// parameter position, right after the database table name.
483 484 485 486
		if (isset($parameters[1]))
		{
			$attribute = $parameters[1];
		}
487

488
		$query = $this->db()->table($parameters[0])->where($attribute, '=', $value);
489

490 491 492 493
		// 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]))
494
		{
495 496 497
			$id = (isset($parameters[3])) ? $parameters[3] : 'id';

			$query->where($id, '<>', $parameters[2]);
498
		}
499

500
		return $query->count() == 0;
501
	}
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

	/**
	 * 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;
	}

Han Lin Yap committed
537 538 539 540 541 542 543 544 545 546 547
	/**
	 * 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;
	}
548 549

	/**
550
	 * Validate that an attribute is a valid e-mail address.
551 552
	 *
	 * @param  string  $attribute
553
	 * @param  mixed   $value
554 555
	 * @return bool
	 */
556
	protected function validate_email($attribute, $value)
557
	{
558
		return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
559 560 561
	}

	/**
562
	 * Validate that an attribute is a valid URL.
563 564
	 *
	 * @param  string  $attribute
565
	 * @param  mixed   $value
566 567
	 * @return bool
	 */
568
	protected function validate_url($attribute, $value)
569
	{
570
		return filter_var($value, FILTER_VALIDATE_URL) !== false;
571 572 573 574 575 576
	}

	/**
	 * Validate that an attribute is an active URL.
	 *
	 * @param  string  $attribute
577
	 * @param  mixed   $value
578 579
	 * @return bool
	 */
580
	protected function validate_active_url($attribute, $value)
581
	{
582
		$url = str_replace(array('http://', 'https://', 'ftp://'), '', Str::lower($value));
583

584
		return (trim($url) !== '') ? checkdnsrr($url) : false;
585 586 587 588 589 590
	}

	/**
	 * Validate the MIME type of a file is an image MIME type.
	 *
	 * @param  string  $attribute
591
	 * @param  mixed   $value
592 593
	 * @return bool
	 */
594
	protected function validate_image($attribute, $value)
595
	{
596
		return $this->validate_mimes($attribute, $value, array('jpg', 'png', 'gif', 'bmp'));
597
	}
598

599
	/**
600
	 * Validate that an attribute contains only alphabetic characters.
601 602
	 *
	 * @param  string  $attribute
603
	 * @param  mixed   $value
604 605
	 * @return bool
	 */
606
	protected function validate_alpha($attribute, $value)
607
	{
608
		return preg_match('/^([a-z])+$/i', $value);
609 610 611
	}

	/**
612
	 * Validate that an attribute contains only alpha-numeric characters.
613 614
	 *
	 * @param  string  $attribute
615
	 * @param  mixed   $value
616 617
	 * @return bool
	 */
618
	protected function validate_alpha_num($attribute, $value)
619
	{
620
		return preg_match('/^([a-z0-9])+$/i', $value);
621 622 623
	}

	/**
624
	 * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
625 626
	 *
	 * @param  string  $attribute
627
	 * @param  mixed   $value
628 629
	 * @return bool
	 */
630
	protected function validate_alpha_dash($attribute, $value)
631
	{
632
		return preg_match('/^([-a-z0-9_-])+$/i', $value);
633 634 635
	}

	/**
636 637 638 639
	 * Validate that an attribute passes a regular expression check.
	 *
	 * @param  string  $attribute
	 * @param  mixed   $value
Sergii Grebeniuk committed
640
	 * @param  array   $parameters
641 642 643 644 645 646 647 648
	 * @return bool
	 */
	protected function validate_match($attribute, $value, $parameters)
	{
		return preg_match($parameters[0], $value);
	}

	/**
649 650 651
	 * Validate the MIME type of a file upload attribute is in a set of MIME types.
	 *
	 * @param  string  $attribute
652
	 * @param  array   $value
653 654 655
	 * @param  array   $parameters
	 * @return bool
	 */
656
	protected function validate_mimes($attribute, $value, $parameters)
657
	{
658
		if ( ! is_array($value) or array_get($value, 'tmp_name', '') == '') return true;
659

660 661
		foreach ($parameters as $extension)
		{
662
			if (File::is($extension, $value['tmp_name']))
Taylor Otwell committed
663 664 665
			{
				return true;
			}
666 667 668 669 670 671
		}

		return false;
	}

	/**
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
	 * 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)
	{
Tobsn committed
693
		return (is_array($value) && count($value) == $parameters[0]);
694 695 696 697 698 699 700 701 702 703 704 705
	}

	/**
	 * 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)
	{
Tobsn committed
706
		return (is_array($value) && count($value) >= $parameters[0]);
707 708 709 710 711 712 713 714 715 716 717 718
	}

	/**
	 * 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)
	{
Tobsn committed
719
		return (is_array($value) && count($value) <= $parameters[0]);
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	}

	/**
	 * 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] );
	}

	/**
736 737 738 739 740 741 742 743 744 745
	 * 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]));
746
	}
747 748 749 750 751 752 753 754 755 756 757 758

	/**
	 * 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]));
759
	}
760 761

	/**
762 763 764 765 766 767
	 * Get the proper error message for an attribute and rule.
	 *
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @return string
	 */
768
	protected function message($attribute, $rule)
769
	{
770 771
		$bundle = Bundle::prefix($this->bundle);

772 773
		// First we'll check for developer specified, attribute specific messages.
		// These messages take first priority. They allow the fine-grained tuning
774
		// of error messages for each rule.
775 776 777 778 779 780
		$custom = $attribute.'_'.$rule;

		if (array_key_exists($custom, $this->messages))
		{
			return $this->messages[$custom];
		}
781
		elseif (Lang::has($custom = "{$bundle}validation.custom.{$custom}", $this->language))
782
		{
783
			return Lang::line($custom)->get($this->language);
784
		}
785

786 787 788
		// 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.
789 790 791
		elseif (array_key_exists($rule, $this->messages))
		{
			return $this->messages[$rule];
792
		}
793

794 795 796
		// 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.
Taylor Otwell committed
797
		elseif (in_array($rule, $this->size_rules))
798
		{
799
			return $this->size_message($bundle, $attribute, $rule);
800
		}
801

802 803 804
		// 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.
805 806
		else
		{
807 808 809
			$line = "{$bundle}validation.{$rule}";

			return Lang::line($line)->get($this->language);
810 811 812 813
		}
	}

	/**
814 815 816 817 818 819 820 821 822 823
	 * 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
Taylor Otwell committed
824 825
		// may be either a number, file, or a string, so we'll check a few
		// things to figure out which one it is.
826 827 828 829
		if ($this->has_rule($attribute, $this->numeric_rules))
		{
			$line = 'numeric';
		}
Taylor Otwell committed
830 831
		// We assume that attributes present in the $_FILES array are files,
		// which makes sense. If the attribute doesn't have numeric rules
Chris Berthe committed
832
		// and isn't a file, it's a string.
833 834 835 836 837 838 839 840 841
		elseif (array_key_exists($attribute, Input::file()))
		{
			$line = 'file';
		}
		else
		{
			$line = 'string';
		}

842
		return Lang::line("{$bundle}validation.{$rule}.{$line}")->get($this->language);
843 844 845
	}

	/**
846 847 848 849 850 851 852 853
	 * Replace all error message place-holders with actual values.
	 *
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
854
	protected function replace($message, $attribute, $rule, $parameters)
855
	{
856
		$message = str_replace(':attribute', $this->attribute($attribute), $message);
857

858
		if (method_exists($this, $replacer = 'replace_'.$rule))
859
		{
860 861
			$message = $this->$replacer($message, $attribute, $rule, $parameters);
		}
862

863 864
		return $message;
	}
865

866 867 868 869 870 871 872 873 874 875 876 877 878
	/**
	 * 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);
	}
879

880 881 882 883 884 885 886 887 888 889 890 891 892
	/**
	 * 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);
	}
893

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
	/**
	 * 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)
946 947 948 949 950
	{
		return str_replace(':values', implode(', ', $parameters), $message);
	}

	/**
Chris Berthe committed
951
	 * Replace all place-holders for the mimes rule.
952 953 954 955 956 957 958 959
	 *
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
	protected function replace_mimes($message, $attribute, $rule, $parameters)
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	{
		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);
990 991 992
	}

	/**
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	 * 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);
1018
	}
1019 1020

	/**
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	 * 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)
	{
Tobsn committed
1031
		return str_replace(':count', $parameters[0], $message);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	}

	/**
	 * 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)
	{
Tobsn committed
1045
		return str_replace(':min', $parameters[0], $message);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	}

	/**
	 * 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)
	{
Tobsn committed
1059
		return str_replace(':max', $parameters[0], $message);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	}

	/**
	 * 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);
	}

	/**
1077 1078 1079 1080 1081 1082 1083
	 * Get the displayable name for a given attribute.
	 *
	 * @param  string  $attribute
	 * @return string
	 */
	protected function attribute($attribute)
	{
1084 1085 1086 1087
		$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
Taylor Otwell committed
1088
		// of the attribute name in the message.
1089 1090
		$line = "{$bundle}validation.attributes.{$attribute}";

1091 1092 1093 1094
		if (Lang::has($line, $this->language))
		{
			return Lang::line($line)->get($this->language);
		}
1095

Taylor Otwell committed
1096 1097
		// If no language line has been specified for the attribute, all of
		// the underscores are removed from the attribute name and that
1098
		// will be used as the attribute name.
1099
		else
Taylor Otwell committed
1100 1101 1102
		{
			return str_replace('_', ' ', $attribute);
		}
1103 1104 1105
	}

	/**
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	 * 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);

1118
			if (in_array($rule, $rules)) return true;
1119 1120 1121 1122 1123 1124
		}

		return false;
	}

	/**
1125
	 * Extract the rule name and parameters from a rule.
1126 1127 1128 1129 1130 1131
	 *
	 * @param  string  $rule
	 * @return array
	 */
	protected function parse($rule)
	{
1132 1133
		$parameters = array();

1134
		// The format for specifying validation rules and parameters follows a
1135 1136
		// {rule}:{parameters} formatting convention. For instance, the rule
		// "max:3" specifies that the value may only be 3 characters long.
1137 1138
		if (($colon = strpos($rule, ':')) !== false)
		{
1139
			$parameters = str_getcsv(substr($rule, $colon + 1));
1140
		}
1141

1142
		return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
1143 1144
	}

1145
	/**
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	 * 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;
	}

	/**
1160 1161
	 * Set the language that should be used when retrieving error messages.
	 *
1162
	 * @param  string     $language
1163 1164
	 * @return Validator
	 */
1165
	public function speaks($language)
1166 1167 1168 1169 1170
	{
		$this->language = $language;
		return $this;
	}

1171 1172 1173
	/**
	 * Set the database connection that should be used by the validator.
	 *
1174
	 * @param  Database\Connection  $connection
1175 1176
	 * @return Validator
	 */
1177
	public function connection(Database\Connection $connection)
1178
	{
1179
		$this->db = $connection;
1180 1181 1182
		return $this;
	}

1183
	/**
1184 1185
	 * Get the database connection for the Validator.
	 *
Phill Sparks committed
1186
	 * @return Database\Connection
1187 1188 1189 1190 1191 1192 1193 1194 1195
	 */
	protected function db()
	{
		if ( ! is_null($this->db)) return $this->db;

		return $this->db = Database::connection();
	}

	/**
1196 1197 1198 1199
	 * Dynamically handle calls to custom registered validators.
	 */
	public function __call($method, $parameters)
	{
1200 1201 1202
		// 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.
1203 1204 1205 1206 1207
		if (isset(static::$validators[$method = substr($method, 9)]))
		{
			return call_user_func_array(static::$validators[$method], $parameters);
		}

1208
		throw new \Exception("Method [$method] does not exist.");
1209 1210
	}

1211
}