1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Validation;
13:
14: use Chromabits\Nucleus\Meditation\Arguments;
15: use Chromabits\Nucleus\Meditation\Boa;
16: use Chromabits\Nucleus\Meditation\Exceptions\InvalidArgumentException;
17: use Chromabits\Nucleus\Meditation\SpecFactory;
18:
19: /**
20: * Class ValidatorFactory.
21: *
22: * @author Eduardo Trujillo <ed@chromabits.com>
23: * @package Chromabits\Nucleus\Validation
24: */
25: class ValidatorFactory extends SpecFactory
26: {
27: /**
28: * @var array
29: */
30: protected $messages;
31:
32: /**
33: * Construct an instance of a ValidatorFactory.
34: */
35: public function __construct()
36: {
37: parent::__construct();
38:
39: $this->messages = [];
40: }
41:
42: /**
43: * @return static
44: */
45: public static function define()
46: {
47: return new static();
48: }
49:
50: /**
51: * Add a custom message for a field.
52: *
53: * @param $field
54: * @param $message
55: *
56: * @throws InvalidArgumentException
57: * @return $this
58: */
59: public function message($field, $message)
60: {
61: Arguments::define(Boa::string(), Boa::string())
62: ->check($field, $message);
63:
64: $this->messages[$field] = $message;
65:
66: return $this;
67: }
68:
69: /**
70: * Build an instance of a Validator.
71: *
72: * @return Validator
73: */
74: public function make()
75: {
76: return new Validator(parent::make(), $this->messages);
77: }
78: }
79: