diff --git a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php index a92fd20535..0395aec730 100644 --- a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php +++ b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php @@ -73,6 +73,7 @@ private function addSchemaValidation(Parameter $parameter): Parameter { $schema = $parameter->getSchema(); $required = $parameter->getRequired(); + $openApi = $parameter->getOpenApi(); // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter, @@ -114,7 +115,7 @@ private function addSchemaValidation(Parameter $parameter): Parameter $maxLength = isset($schema['maxLength']); if ($minLength && $maxLength) { - $assertions[] = \sprintf('between:%s,%s', $schema['minLength'], $schema['maxLength']); + $assertions[] = sprintf('between:%s,%s', $schema['minLength'], $schema['maxLength']); } elseif ($minLength) { $assertions[] = 'min:'.$schema['minLength']; } elseif ($maxLength) { @@ -125,7 +126,7 @@ private function addSchemaValidation(Parameter $parameter): Parameter $maxItems = isset($schema['maxItems']); if ($minItems && $maxItems) { - $assertions[] = \sprintf('between:%s,%s', $schema['minItems'], $schema['maxItems']); + $assertions[] = sprintf('between:%s,%s', $schema['minItems'], $schema['maxItems']); } elseif ($minItems) { $assertions[] = 'min:'.$schema['minItems']; } elseif ($maxItems) { diff --git a/src/Laravel/workbench/app/Models/Book.php b/src/Laravel/workbench/app/Models/Book.php index c458cb68c4..5a57211368 100644 --- a/src/Laravel/workbench/app/Models/Book.php +++ b/src/Laravel/workbench/app/Models/Book.php @@ -26,6 +26,7 @@ use ApiPlatform\Metadata\Post; use ApiPlatform\Metadata\Put; use ApiPlatform\Metadata\QueryParameter; +use ApiPlatform\OpenApi\Model\Parameter; use ApiPlatform\Serializer\Filter\PropertyFilter; use Illuminate\Database\Eloquent\Concerns\HasUlids; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -46,9 +47,9 @@ new GetCollection(), ] )] -#[QueryParameter(key: 'isbn', filter: PartialSearchFilter::class, constraints: 'min:2')] -#[QueryParameter(key: 'name', filter: PartialSearchFilter::class)] -#[QueryParameter(key: 'author', filter: EqualsFilter::class)] +#[QueryParameter(key: 'isbn', schema: ['minimum' => '9783877138395', 'maximum' => '9793877138395'], filter: PartialSearchFilter::class)] +#[QueryParameter(key: 'name', schema: ['pattern' => '/^a/'], filter: PartialSearchFilter::class)] +#[QueryParameter(key: 'author', openApi: new Parameter(name: 'author', in: 'query', allowEmptyValue: false), filter: EqualsFilter::class, required: true)] #[QueryParameter(key: 'publicationDate', filter: DateFilter::class, property: 'publication_date')] #[QueryParameter(key: 'publicationDateWithNulls', filter: DateFilter::class, property: 'publication_date', filterContext: ['include_nulls' => true])] #[QueryParameter(key: 'isbn_range', filter: RangeFilter::class, property: 'isbn')] diff --git a/src/Laravel/workbench/app/Models/Journal.php b/src/Laravel/workbench/app/Models/Journal.php new file mode 100644 index 0000000000..ce3b9b04ce --- /dev/null +++ b/src/Laravel/workbench/app/Models/Journal.php @@ -0,0 +1,40 @@ + '/^a/'], filter: PartialSearchFilter::class, property: 'journal_name')] +#[QueryParameter(key: 'journalNameLengthValidation', schema: ['minLength' => '3'], filter: PartialSearchFilter::class, property: 'journal_name')] +#[QueryParameter(key: 'number', schema: ['exclusiveMinimum' => '0', 'maximum' => '30'], filter: RangeFilter::class)] +#[QueryParameter(key: 'author', openApi: new Parameter(name: 'author', in: 'query', allowEmptyValue: true), filter: EqualsFilter::class, required: true)] +#[QueryParameter( + key: 'name2', + filter: new OrFilter(new EqualsFilter()), + property: 'name', + schema: ['minItems' => 2] +)] +class Journal extends Model +{ + use HasFactory; + use HasUlids; + + protected $visible = ['journal_name', 'title', 'number', 'publication_date']; + protected $fillable = ['journal_name']; + + public function author(): BelongsTo + { + return $this->belongsTo(Journal::class); + } +} diff --git a/src/Laravel/workbench/database/factories/JournalFactory.php b/src/Laravel/workbench/database/factories/JournalFactory.php new file mode 100644 index 0000000000..bbe444f05b --- /dev/null +++ b/src/Laravel/workbench/database/factories/JournalFactory.php @@ -0,0 +1,39 @@ + + */ +class JournalFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var class-string + */ + protected $model = Journal::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'journal_name' => fake()->name(), + 'id' => (string) new Ulid(), + 'author_id' => AuthorFactory::new(), + 'title' => fake()->title(), + 'number' => fake()->numberBetween(1,30), + 'publication_date' => fake()->optional()->date(), + ]; + } +} diff --git a/src/Laravel/workbench/database/migrations/2024_09_16_142930_create_journals_table.php b/src/Laravel/workbench/database/migrations/2024_09_16_142930_create_journals_table.php new file mode 100644 index 0000000000..2079bf3761 --- /dev/null +++ b/src/Laravel/workbench/database/migrations/2024_09_16_142930_create_journals_table.php @@ -0,0 +1,34 @@ +ulid('id')->primary(); + $table->string('journal_name'); + $table->string('title'); + $table->integer('number')->nullable(); + $table->date('publication_date')->nullable(); + $table->integer('author_id')->unsigned(); + $table->foreign('author_id')->references('id')->on('authors'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('journals'); + } +}; diff --git a/src/Laravel/workbench/database/seeders/DatabaseSeeder.php b/src/Laravel/workbench/database/seeders/DatabaseSeeder.php index 2bd7066968..0591f7e5cf 100644 --- a/src/Laravel/workbench/database/seeders/DatabaseSeeder.php +++ b/src/Laravel/workbench/database/seeders/DatabaseSeeder.php @@ -17,6 +17,7 @@ use Workbench\Database\Factories\AuthorFactory; use Workbench\Database\Factories\BookFactory; use Workbench\Database\Factories\CommentFactory; +use Workbench\Database\Factories\JournalFactory; use Workbench\Database\Factories\PostFactory; use Workbench\Database\Factories\SluggableFactory; use Workbench\Database\Factories\UserFactory; @@ -29,6 +30,7 @@ class DatabaseSeeder extends Seeder public function run(): void { BookFactory::new()->has(AuthorFactory::new())->count(10)->create(); + JournalFactory::new()->has(AuthorFactory::new())->count(10)->create(); PostFactory::new()->has(CommentFactory::new()->count(10))->count(10)->create(); SluggableFactory::new()->count(10)->create(); UserFactory::new()->create();