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