|
6 | 6 | use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; |
7 | 7 | use Binaryk\LaravelRestify\MCP\Actions\JsonSchemaFromRulesAction; |
8 | 8 | use Binaryk\LaravelRestify\Repositories\Repository; |
9 | | -use Illuminate\JsonSchema\JsonSchema; |
10 | | -use Illuminate\JsonSchema\JsonSchemaTypeFactory; |
11 | | -use Illuminate\JsonSchema\Types\ArrayType; |
12 | | -use Illuminate\JsonSchema\Types\BooleanType; |
13 | | -use Illuminate\JsonSchema\Types\NumberType; |
14 | | -use Illuminate\JsonSchema\Types\Type; |
15 | 9 |
|
16 | 10 | /** |
17 | 11 | * @mixin \Binaryk\LaravelRestify\Fields\Field |
18 | 12 | */ |
19 | 13 | trait FieldMcpSchemaDetection |
20 | 14 | { |
| 15 | + /** |
| 16 | + * Check if Laravel 12+ JsonSchema classes are available. |
| 17 | + */ |
| 18 | + public static function hasJsonSchemaSupport(): bool |
| 19 | + { |
| 20 | + return class_exists(\Illuminate\JsonSchema\JsonSchemaTypeFactory::class); |
| 21 | + } |
| 22 | + |
21 | 23 | /** |
22 | 24 | * Guess the field type based on validation rules, field class, and attribute patterns. |
| 25 | + * |
| 26 | + * @return \Illuminate\JsonSchema\Types\Type|null Returns Type on Laravel 12+, null on Laravel 11 |
23 | 27 | */ |
24 | | - public function guessFieldType(RestifyRequest $request): Type |
| 28 | + public function guessFieldType(RestifyRequest $request): mixed |
25 | 29 | { |
26 | | - $schema = new JsonSchemaTypeFactory; |
| 30 | + if (! static::hasJsonSchemaSupport()) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + $schema = new \Illuminate\JsonSchema\JsonSchemaTypeFactory; |
27 | 35 |
|
28 | 36 | $rules = $this->getRulesForRequest($request); |
29 | 37 |
|
@@ -84,8 +92,8 @@ public function getDescription(RestifyRequest $request, Repository $repository): |
84 | 92 | } |
85 | 93 | } |
86 | 94 |
|
87 | | - // Add examples based on field type and name |
88 | | - if ($this->jsonSchema instanceof Type) { |
| 95 | + // Add examples based on field type and name (Laravel 12+ only) |
| 96 | + if (static::hasJsonSchemaSupport() && $this->jsonSchema instanceof \Illuminate\JsonSchema\Types\Type) { |
89 | 97 | $examples = $this->generateFieldExamples($this->jsonSchema); |
90 | 98 |
|
91 | 99 | if (! empty($examples)) { |
@@ -140,20 +148,22 @@ protected function formatValidationRules(array $rules): array |
140 | 148 |
|
141 | 149 | /** |
142 | 150 | * Generate examples for the field. |
| 151 | + * |
| 152 | + * @param \Illuminate\JsonSchema\JsonSchema $fieldType |
143 | 153 | */ |
144 | | - protected function generateFieldExamples(JsonSchema $fieldType): array |
| 154 | + protected function generateFieldExamples(mixed $fieldType): array |
145 | 155 | { |
146 | 156 | $attribute = strtolower($this->attribute); |
147 | 157 |
|
148 | | - if ($fieldType instanceof BooleanType) { |
| 158 | + if ($fieldType instanceof \Illuminate\JsonSchema\Types\BooleanType) { |
149 | 159 | return ['true', 'false']; |
150 | 160 | } |
151 | 161 |
|
152 | | - if ($fieldType instanceof NumberType) { |
| 162 | + if ($fieldType instanceof \Illuminate\JsonSchema\Types\NumberType) { |
153 | 163 | return $this->getNumberExamples($attribute); |
154 | 164 | } |
155 | 165 |
|
156 | | - if ($fieldType instanceof ArrayType) { |
| 166 | + if ($fieldType instanceof \Illuminate\JsonSchema\Types\ArrayType) { |
157 | 167 | return ['["item1", "item2"]', '{"key": "value"}']; |
158 | 168 | } |
159 | 169 |
|
@@ -278,8 +288,11 @@ protected function hasAnyRule(array $ruleStrings, array $rulesToCheck): bool |
278 | 288 |
|
279 | 289 | /** |
280 | 290 | * Guess type from attribute name patterns. |
| 291 | + * |
| 292 | + * @param \Illuminate\JsonSchema\JsonSchema $schema |
| 293 | + * @return \Illuminate\JsonSchema\Types\Type|null |
281 | 294 | */ |
282 | | - protected function guessTypeFromAttributeName(JsonSchema $schema): ?Type |
| 295 | + protected function guessTypeFromAttributeName(mixed $schema): mixed |
283 | 296 | { |
284 | 297 | $attribute = $this->attribute; |
285 | 298 |
|
|
0 commit comments