Hi all,
Can someone tell me how to dynamically create class with properties (including type hint/docblock) but without using eval.
Say I have a model class like this:
/**
* @OA\Schema()
*/
class User {
/**
* User's ID
* @var int
* @OA\Property()
*/
public int $id;
/**
* User's first name
* @var string
* @OA\Property()
*/
public string $first_name;
/**
* User's last name
* @var string
* @OA\Property()
*/
public string $last_name;
}
When creating User object, I expect payload with mandatory first_name and last_name, but not containing id. When updating, id is mandatory, first_name and last_name are optional. So I would like somehow to create dynamically for example:
/**
* @OA\Schema(required={"first_name", "last_name"})
*/
class CreateUserSchema {
/**
* User's first name
* @var string
* @OA\Property()
*/
public string $first_name;
/**
* User's last name
* @var string
* @OA\Property()
*/
public string $last_name;
}
So that Swagger-PHP can display it nicely and I can use some validators. I can generate classes with PHP script based on found model classes, but I would like to avoid it if possible. There are anonymous classes, like:
$newClass = new class {
public string $first_name;
public string $last_name;
};
$newClassName = get_class($newClass);
class_alias($newClassName, 'CreateUserSchema');
But I dont know how to generate it dynamically - fields depends on the model and also list of required/optional fields. Same goes for annotations/docblocks - their content will vary depending on model class and required/optional fields.
Thank you in advance.
Best regards