PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.21
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / app / Services / fluentvalidator / src / ValidationData.php

ValidationData.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.21, at app/Services/fluentvalidator/src/ValidationData.php

169 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentValidator;
4
5 class ValidationData
6 {
7 public static function initializeAndGatherData($attribute, $masterData)
8 {
9 $data = Arr::dot(static::initializeAttributeOnData($attribute, $masterData));
10
11 return array_merge($data, static::extractValuesForWildcards(
12 $masterData, $data, $attribute
13 ));
14 }
15
16 /**
17 * Gather a copy of the attribute data filled with any missing attributes.
18 *
19 * @param string $attribute
20 * @param array $masterData
21 *
22 * @return array
23 */
24 protected static function initializeAttributeOnData($attribute, $masterData)
25 {
26 $explicitPath = static::getLeadingExplicitAttributePath($attribute);
27
28 $data = static::extractDataFromPath($explicitPath, $masterData);
29
30 if (! fluentform_mb_strpos($attribute, '*') !== false || substr($attribute, -1) === '*') {
31 return $data;
32 }
33
34 return static::data_set($data, $attribute, null, true);
35 }
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 *
44 * @return array
45 */
46 protected static function extractValuesForWildcards($masterData, $data, $attribute)
47 {
48 $keys = [];
49
50 $pattern = str_replace('\*', '[^\.]+', preg_quote($attribute));
51
52 foreach ($data as $key => $value) {
53 if ((bool) preg_match('/^'.$pattern.'/', $key, $matches)) {
54 $keys[] = $matches[0];
55 }
56 }
57
58 $keys = array_unique($keys);
59
60 $data = [];
61
62 foreach ($keys as $key) {
63 $data[$key] = Arr::get($masterData, $key);
64 }
65
66 return $data;
67 }
68
69 /**
70 * Extract data based on the given dot-notated path.
71 *
72 * Used to extract a sub-section of the data for faster iteration.
73 *
74 * @param string $attribute
75 * @param array $masterData
76 *
77 * @return array
78 */
79 public static function extractDataFromPath($attribute, $masterData)
80 {
81 $results = [];
82
83 $value = Arr::get($masterData, $attribute, '__missing__');
84
85 if ($value != '__missing__') {
86 Arr::set($results, $attribute, $value);
87 }
88
89 return $results;
90 }
91
92 /**
93 * Get the explicit part of the attribute name.
94 *
95 * E.g. 'foo.bar.*.baz' -> 'foo.bar'
96 *
97 * Allows us to not spin through all of the flattened data for some operations.
98 *
99 * @param string $attribute
100 *
101 * @return string
102 */
103 public static function getLeadingExplicitAttributePath($attribute)
104 {
105 return rtrim(explode('*', $attribute)[0], '.') ?: null;
106 }
107
108 /**
109 * Set an item on an array or object using dot notation.
110 *
111 * @param mixed $target
112 * @param string|array $key
113 * @param mixed $value
114 * @param bool $overwrite
115 *
116 * @return mixed
117 */
118 function data_set(&$target, $key, $value, $overwrite = true)
119 {
120 $segments = is_array($key) ? $key : explode('.', $key);
121
122 if (($segment = array_shift($segments)) === '*') {
123 if (! Arr::accessible($target)) {
124 $target = [];
125 }
126
127 if ($segments) {
128 foreach ($target as &$inner) {
129 static::data_set($inner, $segments, $value, $overwrite);
130 }
131 } elseif ($overwrite) {
132 foreach ($target as &$inner) {
133 $inner = $value;
134 }
135 }
136 } elseif (Arr::accessible($target)) {
137 if ($segments) {
138 if (! Arr::exists($target, $segment)) {
139 $target[$segment] = [];
140 }
141
142 static::data_set($target[$segment], $segments, $value, $overwrite);
143 } elseif ($overwrite || ! Arr::exists($target, $segment)) {
144 $target[$segment] = $value;
145 }
146 } elseif (is_object($target)) {
147 if ($segments) {
148 if (! isset($target->{$segment})) {
149 $target->{$segment} = [];
150 }
151
152 static::data_set($target->{$segment}, $segments, $value, $overwrite);
153 } elseif ($overwrite || ! isset($target->{$segment})) {
154 $target->{$segment} = $value;
155 }
156 } else {
157 $target = [];
158
159 if ($segments) {
160 static::data_set($target[$segment], $segments, $value, $overwrite);
161 } elseif ($overwrite) {
162 $target[$segment] = $value;
163 }
164 }
165
166 return $target;
167 }
168 }
169