ValidationData.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Illuminate\Validation;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Str;
  5. class ValidationData
  6. {
  7. /**
  8. * Initialize and gather data for given attribute.
  9. *
  10. * @param string $attribute
  11. * @param array $masterData
  12. * @return array
  13. */
  14. public static function initializeAndGatherData($attribute, $masterData)
  15. {
  16. $data = Arr::dot(static::initializeAttributeOnData($attribute, $masterData));
  17. return array_merge($data, static::extractValuesForWildcards(
  18. $masterData, $data, $attribute
  19. ));
  20. }
  21. /**
  22. * Gather a copy of the attribute data filled with any missing attributes.
  23. *
  24. * @param string $attribute
  25. * @param array $masterData
  26. * @return array
  27. */
  28. protected static function initializeAttributeOnData($attribute, $masterData)
  29. {
  30. $explicitPath = static::getLeadingExplicitAttributePath($attribute);
  31. $data = static::extractDataFromPath($explicitPath, $masterData);
  32. if (! Str::contains($attribute, '*') || Str::endsWith($attribute, '*')) {
  33. return $data;
  34. }
  35. return data_set($data, $attribute, null, true);
  36. }
  37. /**
  38. * Get all of the exact attribute values for a given wildcard attribute.
  39. *
  40. * @param array $masterData
  41. * @param array $data
  42. * @param string $attribute
  43. * @return array
  44. */
  45. protected static function extractValuesForWildcards($masterData, $data, $attribute)
  46. {
  47. $keys = [];
  48. $pattern = str_replace('\*', '[^\.]+', preg_quote($attribute));
  49. foreach ($data as $key => $value) {
  50. if ((bool) preg_match('/^'.$pattern.'/', $key, $matches)) {
  51. $keys[] = $matches[0];
  52. }
  53. }
  54. $keys = array_unique($keys);
  55. $data = [];
  56. foreach ($keys as $key) {
  57. $data[$key] = Arr::get($masterData, $key);
  58. }
  59. return $data;
  60. }
  61. /**
  62. * Extract data based on the given dot-notated path.
  63. *
  64. * Used to extract a sub-section of the data for faster iteration.
  65. *
  66. * @param string $attribute
  67. * @param array $masterData
  68. * @return array
  69. */
  70. public static function extractDataFromPath($attribute, $masterData)
  71. {
  72. $results = [];
  73. $value = Arr::get($masterData, $attribute, '__missing__');
  74. if ($value !== '__missing__') {
  75. Arr::set($results, $attribute, $value);
  76. }
  77. return $results;
  78. }
  79. /**
  80. * Get the explicit part of the attribute name.
  81. *
  82. * E.g. 'foo.bar.*.baz' -> 'foo.bar'
  83. *
  84. * Allows us to not spin through all of the flattened data for some operations.
  85. *
  86. * @param string $attribute
  87. * @return string
  88. */
  89. public static function getLeadingExplicitAttributePath($attribute)
  90. {
  91. return rtrim(explode('*', $attribute)[0], '.') ?: null;
  92. }
  93. }