tempest-framework icon indicating copy to clipboard operation
tempest-framework copied to clipboard

Map Upload files into request properties

Open brendt opened this issue 1 year ago • 6 comments

class MyRequest implements Request
{
    use IsRequest;
    
    public Upload $avatar;
}

brendt avatar Sep 09 '24 12:09 brendt

@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

yassiNebeL avatar Sep 13 '24 14:09 yassiNebeL

Are you sure this works? Because $from->getFiles() is an array without named keys?

brendt avatar Sep 16 '24 07:09 brendt

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.

yassiNebeL avatar Sep 16 '24 12:09 yassiNebeL

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?

yassiNebeL avatar Sep 16 '24 13:09 yassiNebeL

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.

yassiNebeL avatar Sep 16 '24 13:09 yassiNebeL

I think the easiest path forward is to submit a PR that I can review ;)

brendt avatar Sep 16 '24 16:09 brendt

@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?

yassiNebeL avatar Nov 12 '24 17:11 yassiNebeL

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 avatar Nov 14 '24 09:11 yassiNebeL

@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 avatar Nov 14 '24 09:11 innocenzi

@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.

yassiNebeL avatar Nov 14 '24 10:11 yassiNebeL