Factory.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace Illuminate\Validation;
  3. use Closure;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Contracts\Container\Container;
  6. use Illuminate\Contracts\Translation\Translator;
  7. use Illuminate\Contracts\Validation\Factory as FactoryContract;
  8. class Factory implements FactoryContract
  9. {
  10. /**
  11. * The Translator implementation.
  12. *
  13. * @var \Illuminate\Contracts\Translation\Translator
  14. */
  15. protected $translator;
  16. /**
  17. * The Presence Verifier implementation.
  18. *
  19. * @var \Illuminate\Validation\PresenceVerifierInterface
  20. */
  21. protected $verifier;
  22. /**
  23. * The IoC container instance.
  24. *
  25. * @var \Illuminate\Contracts\Container\Container
  26. */
  27. protected $container;
  28. /**
  29. * All of the custom validator extensions.
  30. *
  31. * @var array
  32. */
  33. protected $extensions = [];
  34. /**
  35. * All of the custom implicit validator extensions.
  36. *
  37. * @var array
  38. */
  39. protected $implicitExtensions = [];
  40. /**
  41. * All of the custom dependent validator extensions.
  42. *
  43. * @var array
  44. */
  45. protected $dependentExtensions = [];
  46. /**
  47. * All of the custom validator message replacers.
  48. *
  49. * @var array
  50. */
  51. protected $replacers = [];
  52. /**
  53. * All of the fallback messages for custom rules.
  54. *
  55. * @var array
  56. */
  57. protected $fallbackMessages = [];
  58. /**
  59. * The Validator resolver instance.
  60. *
  61. * @var Closure
  62. */
  63. protected $resolver;
  64. /**
  65. * Create a new Validator factory instance.
  66. *
  67. * @param \Illuminate\Contracts\Translation\Translator $translator
  68. * @param \Illuminate\Contracts\Container\Container $container
  69. * @return void
  70. */
  71. public function __construct(Translator $translator, Container $container = null)
  72. {
  73. $this->container = $container;
  74. $this->translator = $translator;
  75. }
  76. /**
  77. * Create a new Validator instance.
  78. *
  79. * @param array $data
  80. * @param array $rules
  81. * @param array $messages
  82. * @param array $customAttributes
  83. * @return \Illuminate\Validation\Validator
  84. */
  85. public function make(array $data, array $rules, array $messages = [], array $customAttributes = [])
  86. {
  87. // The presence verifier is responsible for checking the unique and exists data
  88. // for the validator. It is behind an interface so that multiple versions of
  89. // it may be written besides database. We'll inject it into the validator.
  90. $validator = $this->resolve(
  91. $data, $rules, $messages, $customAttributes
  92. );
  93. if (! is_null($this->verifier)) {
  94. $validator->setPresenceVerifier($this->verifier);
  95. }
  96. // Next we'll set the IoC container instance of the validator, which is used to
  97. // resolve out class based validator extensions. If it is not set then these
  98. // types of extensions will not be possible on these validation instances.
  99. if (! is_null($this->container)) {
  100. $validator->setContainer($this->container);
  101. }
  102. $this->addExtensions($validator);
  103. return $validator;
  104. }
  105. /**
  106. * Validate the given data against the provided rules.
  107. *
  108. * @param array $data
  109. * @param array $rules
  110. * @param array $messages
  111. * @param array $customAttributes
  112. * @return array
  113. *
  114. * @throws \Illuminate\Validation\ValidationException
  115. */
  116. public function validate(array $data, array $rules, array $messages = [], array $customAttributes = [])
  117. {
  118. return $this->make($data, $rules, $messages, $customAttributes)->validate();
  119. }
  120. /**
  121. * Resolve a new Validator instance.
  122. *
  123. * @param array $data
  124. * @param array $rules
  125. * @param array $messages
  126. * @param array $customAttributes
  127. * @return \Illuminate\Validation\Validator
  128. */
  129. protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
  130. {
  131. if (is_null($this->resolver)) {
  132. return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
  133. }
  134. return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
  135. }
  136. /**
  137. * Add the extensions to a validator instance.
  138. *
  139. * @param \Illuminate\Validation\Validator $validator
  140. * @return void
  141. */
  142. protected function addExtensions(Validator $validator)
  143. {
  144. $validator->addExtensions($this->extensions);
  145. // Next, we will add the implicit extensions, which are similar to the required
  146. // and accepted rule in that they are run even if the attributes is not in a
  147. // array of data that is given to a validator instances via instantiation.
  148. $validator->addImplicitExtensions($this->implicitExtensions);
  149. $validator->addDependentExtensions($this->dependentExtensions);
  150. $validator->addReplacers($this->replacers);
  151. $validator->setFallbackMessages($this->fallbackMessages);
  152. }
  153. /**
  154. * Register a custom validator extension.
  155. *
  156. * @param string $rule
  157. * @param \Closure|string $extension
  158. * @param string $message
  159. * @return void
  160. */
  161. public function extend($rule, $extension, $message = null)
  162. {
  163. $this->extensions[$rule] = $extension;
  164. if ($message) {
  165. $this->fallbackMessages[Str::snake($rule)] = $message;
  166. }
  167. }
  168. /**
  169. * Register a custom implicit validator extension.
  170. *
  171. * @param string $rule
  172. * @param \Closure|string $extension
  173. * @param string $message
  174. * @return void
  175. */
  176. public function extendImplicit($rule, $extension, $message = null)
  177. {
  178. $this->implicitExtensions[$rule] = $extension;
  179. if ($message) {
  180. $this->fallbackMessages[Str::snake($rule)] = $message;
  181. }
  182. }
  183. /**
  184. * Register a custom dependent validator extension.
  185. *
  186. * @param string $rule
  187. * @param \Closure|string $extension
  188. * @param string $message
  189. * @return void
  190. */
  191. public function extendDependent($rule, $extension, $message = null)
  192. {
  193. $this->dependentExtensions[$rule] = $extension;
  194. if ($message) {
  195. $this->fallbackMessages[Str::snake($rule)] = $message;
  196. }
  197. }
  198. /**
  199. * Register a custom validator message replacer.
  200. *
  201. * @param string $rule
  202. * @param \Closure|string $replacer
  203. * @return void
  204. */
  205. public function replacer($rule, $replacer)
  206. {
  207. $this->replacers[$rule] = $replacer;
  208. }
  209. /**
  210. * Set the Validator instance resolver.
  211. *
  212. * @param \Closure $resolver
  213. * @return void
  214. */
  215. public function resolver(Closure $resolver)
  216. {
  217. $this->resolver = $resolver;
  218. }
  219. /**
  220. * Get the Translator implementation.
  221. *
  222. * @return \Illuminate\Contracts\Translation\Translator
  223. */
  224. public function getTranslator()
  225. {
  226. return $this->translator;
  227. }
  228. /**
  229. * Get the Presence Verifier implementation.
  230. *
  231. * @return \Illuminate\Validation\PresenceVerifierInterface
  232. */
  233. public function getPresenceVerifier()
  234. {
  235. return $this->verifier;
  236. }
  237. /**
  238. * Set the Presence Verifier implementation.
  239. *
  240. * @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier
  241. * @return void
  242. */
  243. public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
  244. {
  245. $this->verifier = $presenceVerifier;
  246. }
  247. }