|
4 | 4 |
|
5 | 5 | use Binaryk\LaravelRestify\Contracts\RestifySearchable; |
6 | 6 | use Binaryk\LaravelRestify\Filters\MatchFilter; |
| 7 | +use Binaryk\LaravelRestify\Http\Requests\RepositoryStoreRequest; |
7 | 8 | use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; |
| 9 | +use Illuminate\JsonSchema\Types\ArrayType; |
| 10 | +use Illuminate\JsonSchema\Types\BooleanType; |
| 11 | +use Illuminate\JsonSchema\Types\IntegerType; |
| 12 | +use Illuminate\JsonSchema\Types\NumberType; |
8 | 13 |
|
9 | 14 | trait CanMatch |
10 | 15 | { |
@@ -36,7 +41,10 @@ public function matchable(mixed $column = null, ?string $type = null): self |
36 | 41 | } |
37 | 42 |
|
38 | 43 | $this->matchableColumn = $column ?? $this->getAttribute(); |
39 | | - $this->matchableType = $type ?? $this->guessMatchType(); |
| 44 | + $this->matchableType = $type ?? $this->guessMatchType( |
| 45 | + // we'll use the store request to identify rules and guess types |
| 46 | + app(RepositoryStoreRequest::class) |
| 47 | + ); |
40 | 48 |
|
41 | 49 | return $this; |
42 | 50 | } |
@@ -118,56 +126,15 @@ public function getMatchType(RestifyRequest $request = null): ?string |
118 | 126 | return $this->matchableType; |
119 | 127 | } |
120 | 128 |
|
121 | | - protected function guessMatchType(): string |
| 129 | + protected function guessMatchType(RestifyRequest $request): string |
122 | 130 | { |
123 | | - // Use field type detection from Field class if available |
124 | | - if (method_exists($this, 'guessFieldType')) { |
125 | | - $fieldType = $this->guessFieldType(); |
126 | | - |
127 | | - return match ($fieldType) { |
128 | | - 'boolean' => RestifySearchable::MATCH_BOOL, |
129 | | - 'number' => RestifySearchable::MATCH_INTEGER, |
130 | | - 'array' => RestifySearchable::MATCH_ARRAY, |
131 | | - default => RestifySearchable::MATCH_TEXT, |
132 | | - }; |
133 | | - } |
134 | | - |
135 | | - // Fallback to attribute name patterns |
136 | | - $attribute = $this->getAttribute(); |
137 | | - |
138 | | - if (! is_string($attribute)) { |
139 | | - return RestifySearchable::MATCH_TEXT; |
140 | | - } |
141 | | - |
142 | | - $attribute = strtolower($attribute); |
143 | | - |
144 | | - // Boolean patterns |
145 | | - if (preg_match('/^(is_|has_|can_|should_|will_|was_|were_)/', $attribute) || |
146 | | - in_array($attribute, |
147 | | - ['active', 'enabled', 'disabled', 'verified', 'published', 'featured', 'public', 'private'])) { |
148 | | - return RestifySearchable::MATCH_BOOL; |
149 | | - } |
150 | | - |
151 | | - // Number patterns |
152 | | - if (preg_match('/_(id|count|number|amount|price|cost|total|sum|quantity|qty)$/', $attribute) || |
153 | | - in_array($attribute, |
154 | | - ['id', 'age', 'year', 'month', 'day', 'hour', 'minute', 'second', 'weight', 'height', 'size'])) { |
155 | | - return RestifySearchable::MATCH_INTEGER; |
156 | | - } |
157 | | - |
158 | | - // Date patterns |
159 | | - if (preg_match('/_(at|date|time)$/', $attribute) || |
160 | | - in_array($attribute, |
161 | | - ['created_at', 'updated_at', 'deleted_at', 'published_at', 'birthday', 'date_of_birth'])) { |
162 | | - return RestifySearchable::MATCH_DATETIME; |
163 | | - } |
164 | | - |
165 | | - // Array patterns (JSON fields) |
166 | | - if (preg_match('/_(json|data|metadata|config|settings|options|tags)$/', $attribute)) { |
167 | | - return RestifySearchable::MATCH_ARRAY; |
168 | | - } |
169 | | - |
170 | | - // Default to text matching |
171 | | - return RestifySearchable::MATCH_TEXT; |
| 131 | + $fieldType = $this->guessFieldType($request); |
| 132 | + |
| 133 | + return match (get_class($fieldType)) { |
| 134 | + ArrayType::class => RestifySearchable::MATCH_ARRAY, |
| 135 | + BooleanType::class => RestifySearchable::MATCH_BOOL, |
| 136 | + IntegerType::class, NumberType::class => RestifySearchable::MATCH_INTEGER, |
| 137 | + default => 'string', |
| 138 | + }; |
172 | 139 | } |
173 | 140 | } |
0 commit comments