Factory.php 8.9 KB

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