tempest-framework
tempest-framework copied to clipboard
Map Upload files into request properties
class MyRequest implements Request
{
use IsRequest;
public Upload $avatar;
}
@brendt I figure it out, could I send a PR or you will fix it? this #381 was blocking the server-side request ServerRequestInterface.
this is what I did :
final readonly class RequestToObjectMapper implements Mapper
...
public function map(mixed $from, mixed $to): array|object
{
$data = array_merge($from->getBody(), $from->getFiles());
/** @var Request $from */
return map($data)->to($to);
}
I look forward to your response
Are you sure this works? Because $from->getFiles() is an array without named keys?
Yes, we need a named keys, any idea how we can resolve that? when I tried to map the request to a model object with ArrayToObjectMapper because we couldn't find the property name with the array keys.
We need to add the uploadedFiles as well, now getFiles returns an array with named keys.
final readonly class RequestToPsrRequestMapper implements Mapper
{
...
public function map(mixed $from, mixed $to): PsrRequest
{
/** @var Request $from */
return new ServerRequest(
uri: $from->getUri(),
method: $from->getMethod()->value,
headers: $from->getHeaders(),
cookieParams: $from->getCookies(),
queryParams: $from->getQuery(),
parsedBody: $from->getBody(),
uploadedFiles: $from->getUploadedFiles()
);
}
}
Continuing with the Book example after adding coverPicture property the request along with the Book model this is what I got:
INSERT INTO `Book` (title, coverPicture) VALUES (:title, :coverPicture);
bindings: {
"title": "titre 1",
"coverPicture": null
}
Could you tell me which value should I store in the DB?
I'm pretty sure that the mapping is properly working, I'm waiting for your response Brendt in order to know which value we should save the path or the name, that's why I would implement a new caster.
I think the easiest path forward is to submit a PR that I can review ;)
@brendt, the mapping between PsrRequest and Request works properly. I believe the mapping between Request and Model is also correct, but I encountered an error when trying to save the Book model using #702.
The error message is: "Object of class Tempest\Http\Upload could not be converted to string."
We can easily implement the __toString method in the Upload class to return the path. let me know if I need to create a new issue regarding the issue encountered.
What do you think about it?
I feel like my contributions are unacknowledged. I joined GitHub in 2022 with the main goal of contributing to FOSS, not to gain recognition or push leadership.
Kindly, Yassine
@yassiNebeL I'm sorry you feel that way, but Brent doesn't work full-time on Tempest. There are gaps in availabilities, it's normal that issues and pull requests take time to be resolved.
@innocenzi Thank you for you reply, I understand that @brendt doesn't work full-time on Tempest.
My intention was not to rush or an immediate action but to express that Ive been disconnected from the project despite my efforts. I just want to clarify my position and the frustration that comes with contributing without feedback.
I appreciate the work done, and I'm still open to contributing if things change.