PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.21
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.21
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / vendor / wpfluent / framework / src / WPFluent / Validator / ValidationData.php

ValidationData.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.21, at vendor/wpfluent/framework/src/WPFluent/Validator/ValidationData.php

117 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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