|
7 | 7 | use Illuminate\JsonSchema\JsonSchema; |
8 | 8 | use Illuminate\JsonSchema\Types\Type; |
9 | 9 |
|
| 10 | +/** |
| 11 | + * @mixin \Binaryk\LaravelRestify\Fields\Field |
| 12 | + */ |
10 | 13 | trait FieldMcpSchemaDetection |
11 | 14 | { |
12 | 15 | /** |
@@ -51,6 +54,26 @@ public function resolveJsonSchema(JsonSchema $schema, Repository $repository): ? |
51 | 54 | return $schemaField; |
52 | 55 | } |
53 | 56 |
|
| 57 | + /** |
| 58 | + * Guess the field type based on validation rules, field class, and attribute patterns. |
| 59 | + */ |
| 60 | + public function guessFieldType(): string |
| 61 | + { |
| 62 | + $ruleType = $this->guessTypeFromValidationRules(); |
| 63 | + if ($ruleType) { |
| 64 | + return $ruleType; |
| 65 | + } |
| 66 | + |
| 67 | + // Check attribute name patterns |
| 68 | + $attributeType = $this->guessTypeFromAttributeName(); |
| 69 | + if ($attributeType) { |
| 70 | + return $attributeType; |
| 71 | + } |
| 72 | + |
| 73 | + // Default to string |
| 74 | + return 'string'; |
| 75 | + } |
| 76 | + |
54 | 77 | /** |
55 | 78 | * Generate a comprehensive description for the field. |
56 | 79 | */ |
@@ -213,4 +236,117 @@ protected function getStringExamples(string $attribute): array |
213 | 236 |
|
214 | 237 | return ['sample text', 'example value']; |
215 | 238 | } |
| 239 | + |
| 240 | + /** |
| 241 | + * Guess type from validation rules. |
| 242 | + */ |
| 243 | + protected function guessTypeFromValidationRules(): ?string |
| 244 | + { |
| 245 | + $allRules = array_merge($this->rules, $this->storingRules, $this->updatingRules); |
| 246 | + |
| 247 | + // Convert rule objects to strings for checking |
| 248 | + $ruleStrings = collect($allRules)->map(function ($rule) { |
| 249 | + if (is_string($rule)) { |
| 250 | + return $rule; |
| 251 | + } |
| 252 | + if (is_object($rule)) { |
| 253 | + return get_class($rule); |
| 254 | + } |
| 255 | + |
| 256 | + return (string) $rule; |
| 257 | + })->toArray(); |
| 258 | + |
| 259 | + // Check for specific types |
| 260 | + if ($this->hasAnyRule($ruleStrings, ['boolean', 'bool'])) { |
| 261 | + return 'boolean'; |
| 262 | + } |
| 263 | + |
| 264 | + if ($this->hasAnyRule($ruleStrings, ['array'])) { |
| 265 | + return 'array'; |
| 266 | + } |
| 267 | + |
| 268 | + if ($this->hasAnyRule($ruleStrings, ['email', 'url', 'ip', 'uuid', 'string', 'regex'])) { |
| 269 | + return 'string'; |
| 270 | + } |
| 271 | + |
| 272 | + if ($this->hasAnyRule($ruleStrings, ['date', 'date_format:', 'before:', 'after:', 'before_or_equal:', 'after_or_equal:'])) { |
| 273 | + return 'string'; // Dates are typically handled as strings in schemas |
| 274 | + } |
| 275 | + |
| 276 | + if ($this->hasAnyRule($ruleStrings, ['file', 'image', 'mimes:', 'mimetypes:'])) { |
| 277 | + return 'string'; // Files are typically handled as strings (paths/URLs) |
| 278 | + } |
| 279 | + |
| 280 | + if ($this->hasAnyRule($ruleStrings, ['integer', 'int', 'numeric', 'between:'])) { |
| 281 | + return 'number'; |
| 282 | + } |
| 283 | + |
| 284 | + return null; |
| 285 | + } |
| 286 | + |
| 287 | + /** |
| 288 | + * Check if any of the given rules exist in the rule strings. |
| 289 | + */ |
| 290 | + protected function hasAnyRule(array $ruleStrings, array $rulesToCheck): bool |
| 291 | + { |
| 292 | + foreach ($ruleStrings as $rule) { |
| 293 | + foreach ($rulesToCheck as $check) { |
| 294 | + if ($rule === $check || str_starts_with($rule, $check)) { |
| 295 | + return true; |
| 296 | + } |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + return false; |
| 301 | + } |
| 302 | + |
| 303 | + /** |
| 304 | + * Guess type from attribute name patterns. |
| 305 | + */ |
| 306 | + protected function guessTypeFromAttributeName(): ?string |
| 307 | + { |
| 308 | + $attribute = $this->attribute; |
| 309 | + |
| 310 | + if (! is_string($attribute)) { |
| 311 | + return null; |
| 312 | + } |
| 313 | + |
| 314 | + $attribute = strtolower($attribute); |
| 315 | + |
| 316 | + // Boolean patterns |
| 317 | + if (preg_match('/^(is_|has_|can_|should_|will_|was_|were_)/', $attribute) || |
| 318 | + in_array($attribute, ['active', 'enabled', 'disabled', 'verified', 'published', 'featured', 'public', 'private'])) { |
| 319 | + return 'boolean'; |
| 320 | + } |
| 321 | + |
| 322 | + // Number patterns |
| 323 | + if (preg_match('/_(id|count|number|amount|price|cost|total|sum|quantity|qty)$/', $attribute) || |
| 324 | + in_array($attribute, ['id', 'age', 'year', 'month', 'day', 'hour', 'minute', 'second', 'weight', 'height', 'size'])) { |
| 325 | + return 'number'; |
| 326 | + } |
| 327 | + |
| 328 | + // Date patterns |
| 329 | + if (preg_match('/_(at|date|time)$/', $attribute) || |
| 330 | + in_array($attribute, ['created_at', 'updated_at', 'deleted_at', 'published_at', 'birthday', 'date_of_birth'])) { |
| 331 | + return 'string'; |
| 332 | + } |
| 333 | + |
| 334 | + // Email pattern |
| 335 | + if (str_contains($attribute, 'email')) { |
| 336 | + return 'string'; |
| 337 | + } |
| 338 | + |
| 339 | + // Password pattern |
| 340 | + if (str_contains($attribute, 'password')) { |
| 341 | + return 'string'; |
| 342 | + } |
| 343 | + |
| 344 | + // Array patterns (JSON fields) |
| 345 | + if (preg_match('/_(json|data|metadata|config|settings|options)$/', $attribute) || |
| 346 | + str_contains($attribute, 'tags')) { |
| 347 | + return 'array'; |
| 348 | + } |
| 349 | + |
| 350 | + return null; |
| 351 | + } |
216 | 352 | } |
0 commit comments