validator.php 25.1 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 217
		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';
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 253 254 255
		elseif ( ! is_null(Input::file($attribute)) and $value['tmp_name'] == '')
		{
			return false;
		}
256 257

		return true;
258 259 260 261 262 263
	}

	/**
	 * Validate that an attribute has a matching confirmation attribute.
	 *
	 * @param  string  $attribute
264
	 * @param  mixed   $value
265 266
	 * @return bool
	 */
267
	protected function validate_confirmed($attribute, $value)
268
	{
269
		return $this->validate_same($attribute, $value, array($attribute.'_confirmation'));
270 271 272 273 274 275 276 277
	}

	/**
	 * Validate that an attribute was "accepted".
	 *
	 * This validation rule implies the attribute is "required".
	 *
	 * @param  string  $attribute
278
	 * @param  mixed   $value
279 280
	 * @return bool
	 */
281
	protected function validate_accepted($attribute, $value)
282
	{
283
		return $this->validate_required($attribute, $value) and ($value == 'yes' or $value == '1');
284 285 286
	}

	/**
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	 * 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];
	}

	/**
317 318 319
	 * Validate that an attribute is numeric.
	 *
	 * @param  string  $attribute
320
	 * @param  mixed   $value
321 322
	 * @return bool
	 */
323
	protected function validate_numeric($attribute, $value)
324
	{
325
		return is_numeric($value);
326 327 328 329 330 331
	}

	/**
	 * Validate that an attribute is an integer.
	 *
	 * @param  string  $attribute
332
	 * @param  mixed   $value
333 334
	 * @return bool
	 */
335
	protected function validate_integer($attribute, $value)
336
	{
337
		return filter_var($value, FILTER_VALIDATE_INT) !== false;
338 339 340 341 342 343
	}

	/**
	 * Validate the size of an attribute.
	 *
	 * @param  string  $attribute
344
	 * @param  mixed   $value
345 346 347
	 * @param  array   $parameters
	 * @return bool
	 */
348
	protected function validate_size($attribute, $value, $parameters)
349
	{
350
		return $this->size($attribute, $value) == $parameters[0];
351 352 353 354 355 356
	}

	/**
	 * Validate the size of an attribute is between a set of values.
	 *
	 * @param  string  $attribute
357
	 * @param  mixed   $value
358 359 360
	 * @param  array   $parameters
	 * @return bool
	 */
361
	protected function validate_between($attribute, $value, $parameters)
362
	{
363 364 365
		$size = $this->size($attribute, $value);

		return $size >= $parameters[0] and $size <= $parameters[1];
366 367 368
	}

	/**
369
	 * Validate the size of an attribute is greater than a minimum value.
370
	 *
371
	 * @param  string  $attribute
372
	 * @param  mixed   $value
373 374
	 * @param  array   $parameters
	 * @return bool
375
	 */
376
	protected function validate_min($attribute, $value, $parameters)
377
	{
378
		return $this->size($attribute, $value) >= $parameters[0];
379 380 381
	}

	/**
382
	 * Validate the size of an attribute is less than a maximum value.
383
	 *
384
	 * @param  string  $attribute
385
	 * @param  mixed   $value
386
	 * @param  array   $parameters
387 388
	 * @return bool
	 */
389
	protected function validate_max($attribute, $value, $parameters)
390
	{
391
		return $this->size($attribute, $value) <= $parameters[0];
392
	}
393

394 395 396 397
	/**
	 * Get the size of an attribute.
	 *
	 * @param  string  $attribute
398
	 * @param  mixed   $value
399 400
	 * @return mixed
	 */
401
	protected function size($attribute, $value)
402
	{
403 404 405 406
	 	// This method will determine if the attribute is a number, string, or file and
	 	// return the proper size accordingly. If it is a number, then number itself is
	 	// the size; if it is a file, the size is kilobytes in the size; if it is a
	 	// string, the length is the size.
407
		if (is_numeric($value) and $this->has_rule($attribute, $this->numeric_rules))
408 409 410
		{
			return $this->attributes[$attribute];
		}
411
		elseif (array_key_exists($attribute, Input::file()))
412 413 414 415 416 417 418
		{
			return $value['size'] / 1024;
		}
		else
		{
			return Str::length(trim($value));
		}
419 420 421
	}

	/**
422 423 424
	 * Validate an attribute is contained within a list of values.
	 *
	 * @param  string  $attribute
425
	 * @param  mixed   $value
426 427 428
	 * @param  array   $parameters
	 * @return bool
	 */
429
	protected function validate_in($attribute, $value, $parameters)
430
	{
431
		return in_array($value, $parameters);
432 433 434 435 436 437
	}

	/**
	 * Validate an attribute is not contained within a list of values.
	 *
	 * @param  string  $attribute
438
	 * @param  mixed   $value
439 440
	 * @param  array   $parameters
	 * @return bool
441
	 */
442
	protected function validate_not_in($attribute, $value, $parameters)
443
	{
444
		return ! in_array($value, $parameters);
445 446 447 448 449
	}

	/**
	 * Validate the uniqueness of an attribute value on a given database table.
	 *
450
	 * If a database column is not specified, the attribute will be used.
451
	 *
452
	 * @param  string  $attribute
453
	 * @param  mixed   $value
454 455 456
	 * @param  array   $parameters
	 * @return bool
	 */
457
	protected function validate_unique($attribute, $value, $parameters)
458
	{
459
		// We allow the table column to be specified just in case the column does
Taylor Otwell committed
460
		// not have the same name as the attribute. It must be within the second
461
		// parameter position, right after the database table name.
462 463 464 465
		if (isset($parameters[1]))
		{
			$attribute = $parameters[1];
		}
466

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

469 470 471 472
		// 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]))
473
		{
474 475 476
			$id = (isset($parameters[3])) ? $parameters[3] : 'id';

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

479
		return $query->count() == 0;
480
	}
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

	/**
	 * 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
516 517 518 519 520 521 522 523 524 525 526
	/**
	 * 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;
	}
527 528

	/**
529
	 * Validate that an attribute is a valid e-mail address.
530 531
	 *
	 * @param  string  $attribute
532
	 * @param  mixed   $value
533 534
	 * @return bool
	 */
535
	protected function validate_email($attribute, $value)
536
	{
537
		return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
538 539 540
	}

	/**
541
	 * Validate that an attribute is a valid URL.
542 543
	 *
	 * @param  string  $attribute
544
	 * @param  mixed   $value
545 546
	 * @return bool
	 */
547
	protected function validate_url($attribute, $value)
548
	{
549
		return filter_var($value, FILTER_VALIDATE_URL) !== false;
550 551 552 553 554 555
	}

	/**
	 * Validate that an attribute is an active URL.
	 *
	 * @param  string  $attribute
556
	 * @param  mixed   $value
557 558
	 * @return bool
	 */
559
	protected function validate_active_url($attribute, $value)
560
	{
561
		$url = str_replace(array('http://', 'https://', 'ftp://'), '', Str::lower($value));
562

563 564 565 566 567 568 569
		return checkdnsrr($url);
	}

	/**
	 * Validate the MIME type of a file is an image MIME type.
	 *
	 * @param  string  $attribute
570
	 * @param  mixed   $value
571 572
	 * @return bool
	 */
573
	protected function validate_image($attribute, $value)
574
	{
575
		return $this->validate_mimes($attribute, $value, array('jpg', 'png', 'gif', 'bmp'));
576
	}
577

578
	/**
579
	 * Validate that an attribute contains only alphabetic characters.
580 581
	 *
	 * @param  string  $attribute
582
	 * @param  mixed   $value
583 584
	 * @return bool
	 */
585
	protected function validate_alpha($attribute, $value)
586
	{
587
		return preg_match('/^([a-z])+$/i', $value);
588 589 590
	}

	/**
591
	 * Validate that an attribute contains only alpha-numeric characters.
592 593
	 *
	 * @param  string  $attribute
594
	 * @param  mixed   $value
595 596
	 * @return bool
	 */
597
	protected function validate_alpha_num($attribute, $value)
598
	{
599
		return preg_match('/^([a-z0-9])+$/i', $value);
600 601 602
	}

	/**
603
	 * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
604 605
	 *
	 * @param  string  $attribute
606
	 * @param  mixed   $value
607 608
	 * @return bool
	 */
609
	protected function validate_alpha_dash($attribute, $value)
610
	{
611
		return preg_match('/^([-a-z0-9_-])+$/i', $value);
612 613 614
	}

	/**
615 616 617 618 619 620 621 622 623 624 625 626
	 * Validate that an attribute passes a regular expression check.
	 *
	 * @param  string  $attribute
	 * @param  mixed   $value
	 * @return bool
	 */
	protected function validate_match($attribute, $value, $parameters)
	{
		return preg_match($parameters[0], $value);
	}

	/**
627 628 629
	 * Validate the MIME type of a file upload attribute is in a set of MIME types.
	 *
	 * @param  string  $attribute
630
	 * @param  array   $value
631 632 633
	 * @param  array   $parameters
	 * @return bool
	 */
634
	protected function validate_mimes($attribute, $value, $parameters)
635
	{
636
		if ( ! is_array($value) or array_get($value, 'tmp_name', '') == '') return true;
637

638 639
		foreach ($parameters as $extension)
		{
640
			if (File::is($extension, $value['tmp_name']))
Taylor Otwell committed
641 642 643
			{
				return true;
			}
644 645 646 647 648 649
		}

		return false;
	}

	/**
650 651 652 653 654 655 656 657 658 659
	 * 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]));
660
	}
661 662 663 664 665 666 667 668 669 670 671 672

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

	/**
676 677 678 679 680 681
	 * Get the proper error message for an attribute and rule.
	 *
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @return string
	 */
682
	protected function message($attribute, $rule)
683
	{
684 685
		$bundle = Bundle::prefix($this->bundle);

686 687
		// First we'll check for developer specified, attribute specific messages.
		// These messages take first priority. They allow the fine-grained tuning
688
		// of error messages for each rule.
689 690 691 692 693 694
		$custom = $attribute.'_'.$rule;

		if (array_key_exists($custom, $this->messages))
		{
			return $this->messages[$custom];
		}
695
		elseif (Lang::has($custom = "{$bundle}validation.custom.{$custom}", $this->language))
696
		{
697
			return Lang::line($custom)->get($this->language);
698
		}
699

700 701 702
		// 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.
703 704 705
		elseif (array_key_exists($rule, $this->messages))
		{
			return $this->messages[$rule];
706
		}
707

708 709 710
		// 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
711
		elseif (in_array($rule, $this->size_rules))
712
		{
713
			return $this->size_message($bundle, $attribute, $rule);
714
		}
715

716 717 718
		// 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.
719 720
		else
		{
721 722 723
			$line = "{$bundle}validation.{$rule}";

			return Lang::line($line)->get($this->language);
724 725 726 727
		}
	}

	/**
728 729 730 731 732 733 734 735 736 737
	 * 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
738 739
		// may be either a number, file, or a string, so we'll check a few
		// things to figure out which one it is.
740 741 742 743
		if ($this->has_rule($attribute, $this->numeric_rules))
		{
			$line = 'numeric';
		}
Taylor Otwell committed
744 745 746
		// 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 as file, it's a string.
747 748 749 750 751 752 753 754 755
		elseif (array_key_exists($attribute, Input::file()))
		{
			$line = 'file';
		}
		else
		{
			$line = 'string';
		}

756
		return Lang::line("{$bundle}validation.{$rule}.{$line}")->get($this->language);
757 758 759
	}

	/**
760 761 762 763 764 765 766 767
	 * Replace all error message place-holders with actual values.
	 *
	 * @param  string  $message
	 * @param  string  $attribute
	 * @param  string  $rule
	 * @param  array   $parameters
	 * @return string
	 */
768
	protected function replace($message, $attribute, $rule, $parameters)
769
	{
770
		$message = str_replace(':attribute', $this->attribute($attribute), $message);
771

772
		if (method_exists($this, $replacer = 'replace_'.$rule))
773
		{
774 775
			$message = $this->$replacer($message, $attribute, $rule, $parameters);
		}
776

777 778
		return $message;
	}
779

780 781 782 783 784 785 786 787 788 789 790 791 792
	/**
	 * 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);
	}
793

794 795 796 797 798 799 800 801 802 803 804 805 806
	/**
	 * 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);
	}
807

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
	/**
	 * 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)
860 861 862 863 864 865 866 867 868 869 870 871 872 873
	{
		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_mimes($message, $attribute, $rule, $parameters)
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
	{
		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);
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
	 * 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);
932
	}
933 934

	/**
935 936 937 938 939 940 941
	 * Get the displayable name for a given attribute.
	 *
	 * @param  string  $attribute
	 * @return string
	 */
	protected function attribute($attribute)
	{
942 943 944 945
		$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
946
		// of the attribute name in the message.
947 948 949
		$line = "{$bundle}validation.attributes.{$attribute}";

		$display = Lang::line($line)->get($this->language);
950

Taylor Otwell committed
951 952 953 954 955 956 957 958 959
		// 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 attribtue name.
		if (is_null($display))
		{
			return str_replace('_', ' ', $attribute);
		}

		return $display;
960 961 962
	}

	/**
963 964 965 966 967 968 969 970 971 972 973 974
	 * 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);

975
			if (in_array($rule, $rules)) return true;
976 977 978 979 980 981
		}

		return false;
	}

	/**
982
	 * Extract the rule name and parameters from a rule.
983 984 985 986 987 988
	 *
	 * @param  string  $rule
	 * @return array
	 */
	protected function parse($rule)
	{
989 990
		$parameters = array();

991
		// The format for specifying validation rules and parameters follows a
992 993
		// {rule}:{parameters} formatting convention. For instance, the rule
		// "max:3" specifies that the value may only be 3 characters long.
994 995
		if (($colon = strpos($rule, ':')) !== false)
		{
996
			$parameters = str_getcsv(substr($rule, $colon + 1));
997
		}
998

999
		return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
1000 1001
	}

1002
	/**
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	 * 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;
	}

	/**
1017 1018
	 * Set the language that should be used when retrieving error messages.
	 *
1019
	 * @param  string     $language
1020 1021
	 * @return Validator
	 */
1022
	public function speaks($language)
1023 1024 1025 1026 1027
	{
		$this->language = $language;
		return $this;
	}

1028 1029 1030
	/**
	 * Set the database connection that should be used by the validator.
	 *
1031
	 * @param  Database\Connection  $connection
1032 1033
	 * @return Validator
	 */
1034
	public function connection(Database\Connection $connection)
1035
	{
1036
		$this->db = $connection;
1037 1038 1039
		return $this;
	}

1040
	/**
1041 1042
	 * Get the database connection for the Validator.
	 *
Phill Sparks committed
1043
	 * @return Database\Connection
1044 1045 1046 1047 1048 1049 1050 1051 1052
	 */
	protected function db()
	{
		if ( ! is_null($this->db)) return $this->db;

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

	/**
1053 1054 1055 1056
	 * Dynamically handle calls to custom registered validators.
	 */
	public function __call($method, $parameters)
	{
1057 1058 1059
		// 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.
1060 1061 1062 1063 1064
		if (isset(static::$validators[$method = substr($method, 9)]))
		{
			return call_user_func_array(static::$validators[$method], $parameters);
		}

1065
		throw new \Exception("Method [$method] does not exist.");
1066 1067
	}

1068
}