/
SupportLegacyModels.php
169 lines (129 loc) · 4.78 KB
/
SupportLegacyModels.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Livewire\Features\SupportLegacyModels;
use Livewire\ComponentHook;
use function Livewire\on;
/**
* Depends on: SupportValidation for ->missingRuleFor() method on component. (inside ModelSynth)
*/
class SupportLegacyModels extends ComponentHook
{
protected static $rules;
static function provide()
{
// Only enable this feature if config option is set to `true`.
if (config('livewire.legacy_model_binding', false) !== true) return;
app('livewire')->propertySynthesizer([
EloquentModelSynth::class,
EloquentCollectionSynth::class,
]);
on('flush-state', function() {
static::flushRules();
});
}
static function flushRules()
{
static::$rules = null;
}
static function hasRuleFor($component, $path) {
$path = str($path)->explode('.');
$has = false;
$end = false;
$segmentRules = static::getRules($component);
foreach ($path as $key => $segment) {
if ($end) {
throw new \LogicException('Something went wrong');
}
if (!is_numeric($segment) && array_key_exists($segment, $segmentRules)) {
$segmentRules = $segmentRules[$segment];
$has = true;
continue;
}
if (is_numeric($segment) && array_key_exists('*', $segmentRules)) {
$segmentRules = $segmentRules['*'];
$has = true;
continue;
}
if (is_numeric($segment) && in_array('*', $segmentRules, true)) {
$has = true;
$end = true;
continue;
}
if (in_array($segment, $segmentRules, true)) {
$has = true;
$end = true;
continue;
}
$has = false;
}
return $has;
}
static function missingRuleFor($component, $path) {
return ! static::hasRuleFor($component, $path);
}
static function getRules($component) {
return static::$rules[$component->getId()] ??= static::processRules($component);
}
static function getRulesFor($component, $key)
{
$rules = static::getRules($component);
$propertyWithStarsInsteadOfNumbers = static::ruleWithNumbersReplacedByStars($key);
return static::dataGetWithoutWildcardSupport(
$rules,
$propertyWithStarsInsteadOfNumbers,
data_get($rules, $key, []),
);
}
static function dataGetWithoutWildcardSupport($array, $key, $default)
{
$segments = explode('.', $key);
$first = array_shift($segments);
if (! isset($array[$first])) {
return value($default);
}
$value = $array[$first];
if (count($segments) > 0) {
return static::dataGetWithoutWildcardSupport($value, implode('.', $segments), $default);
}
return $value;
}
static function ruleWithNumbersReplacedByStars($dotNotatedProperty)
{
// Convert foo.0.bar.1 -> foo.*.bar.*
return (string) str($dotNotatedProperty)
// Replace all numeric indexes with an array wildcard: (.0., .10., .007.) => .*.
// In order to match overlapping numerical indexes (foo.1.2.3.4.name),
// We need to use a positive look-behind, that's technically all the magic here.
// For better understanding, see: https://regexr.com/5d1n3
->replaceMatches('/(?<=(\.))\d+\./', '*.')
// Replace all numeric indexes at the end of the name with an array wildcard
// (Same as the previous regex, but ran only at the end of the string)
// For better undestanding, see: https://regexr.com/5d1n6
->replaceMatches('/\.\d+$/', '.*');
}
protected static function processRules($component)
{
$rules = array_keys($component->getRules());
return static::convertDotNotationToArrayNotation($rules);
}
protected static function convertDotNotationToArrayNotation($rules)
{
return static::recusivelyProcessDotNotation($rules);
}
protected static function recusivelyProcessDotNotation($rules)
{
$singleRules = [];
$groupedRules = [];
foreach ($rules as $key => $value) {
$value = str($value);
if (!$value->contains('.')) {
$singleRules[] = (string) $value;
continue;
}
$groupedRules[(string) $value->before('.')][] = (string) $value->after('.');
}
foreach ($groupedRules as $key => $value) {
$groupedRules[$key] = static::recusivelyProcessDotNotation($value);
}
return $singleRules + $groupedRules;
}
}