Guest User

Untitled

a guest
May 27th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. if (! function_exists('validationRules'))
  5. {
  6. /**
  7. * return the validation rules of provided type
  8. * from validation config
  9. * @param string $validationType
  10. * @param string|array|null $ruleKeys , rules to return
  11. * @param array $variables
  12. * @return array examples :
  13. * examples :
  14. * validationRules('release',[
  15. * 'file_example'=>['type','size'],
  16. * 'description',
  17. * ])
  18. * this will return complete description rules, file_example rules
  19. * are array so it will return only requested
  20. *
  21. * example 2 :
  22. * validationRules('release',[
  23. * 'file_example',
  24. * 'description',
  25. * ])
  26. * this will return complete description rules and of file_example
  27. * also as we know file_example rules are array and
  28. * user did't specified sub rule so we will
  29. * return all of it's rule
  30. *
  31. * example 3 :
  32. * validationRules('release')
  33. * this will return all rules of release in format
  34. * accepted by laravel validator
  35. *
  36. * example 4 :
  37. * validationRules('form','form_name',['FORM_NAME'=>12,'RELEASE_ID'=>12])
  38. * this will return all rules of form_name as following
  39. * required|string|regex:/^[a-z0-9\_]{1,30}$/|unique:meta_forms,form_name,null,id,release_id,51
  40. *
  41. * if you see the form rules there are two variables FORM_NAME and RELEASE_ID
  42. * that are being replaced by our third argument
  43. *
  44. * @author Dawood Ikhlaq <dawood.ikhlaq@nubilaria.com>
  45. */
  46. function validationRules($validationType, $ruleKeys = null, array $variables = null)
  47. {
  48. if(is_string($ruleKeys))
  49. {
  50. $rules = config("validation.$validationType.$ruleKeys");
  51. if(is_array($rules))
  52. {
  53. $rules = implode('|',$rules);
  54. }
  55. return replaceRuleArrayInString([$ruleKeys=>$rules],$variables);
  56. }
  57. if(!$ruleKeys)
  58. {
  59. $ruleKeys = array_keys(config("validation.$validationType"));
  60. }
  61. $validationRules = [];
  62. foreach ($ruleKeys as $key => $value)
  63. {
  64. if(is_string($value))
  65. {
  66. $rules = config("validation.$validationType.$value");
  67. if(is_array($rules))
  68. {
  69. $rules = implode('|',$rules);
  70. }
  71. $validationRules[$value] = $rules;
  72. continue;
  73. }
  74. $subKeys = [];
  75. foreach ($value as $subKey)
  76. {
  77. if(!config("validation.$validationType.$key.$subKey"))
  78. {
  79. $subKeys[] = $subKey;
  80. continue;
  81. }
  82. $subKeys[] = config("validation.$validationType.$key.$subKey");
  83. }
  84. $validationRules[$key] = implode('|',$subKeys);
  85. }
  86. return replaceRuleArrayInString($validationRules,$variables);
  87. }
  88. }
  89.  
  90. if (! function_exists('replaceRuleArrayInString'))
  91. {
  92. /**
  93. * search variables in rule and replace with provided value in variablesToReplace
  94. * @param array $validationRules
  95. * @param array|null $variablesToReplace
  96. * @return array
  97. * @author Dawood Ikhlaq <dawood.ikhlaq@nubilaria.com>
  98. */
  99. function replaceRuleArrayInString(array $validationRules, array $variablesToReplace = null)
  100. {
  101. if(!count($variablesToReplace))
  102. {
  103. return $validationRules;
  104. }
  105. $validationRulesFinal = [];
  106. foreach ($validationRules as $key => $rule)
  107. {
  108. foreach ($variablesToReplace as $variable => $value)
  109. {
  110. $rule = str_replace("%$variable%",$value,$rule);
  111. }
  112. $validationRulesFinal[$key] = $rule;
  113. }
  114. return $validationRulesFinal;
  115. }
  116. }
  117.  
  118.  
  119. if (! function_exists('validateDataAgainstRules'))
  120. {
  121.  
  122. /**
  123. * get the validation rules of provided type
  124. * from validation config and validate that
  125. * against provided data
  126. * @param string $validationType
  127. * @param string|array|null $ruleKeys
  128. * @param array $data
  129. * @param array $variables
  130. * @author Dawood Ikhlaq <dawood.ikhlaq@nubilaria.com>
  131. */
  132. function validateDataAgainstRules($validationType, $ruleKeys = null, array $data, array $variables = null)
  133. {
  134. $validationRules = validationRules($validationType,$ruleKeys, $variables);
  135. $validator = Validator::make($data, $validationRules);
  136. if($validator->fails())
  137. {
  138. throw new CustomValidationException($validator);
  139. }
  140. }
  141. }
Add Comment
Please, Sign In to add comment