Simply-Stream / TwitchApiBundle

MIT License
1 stars 2 forks source link

Return data as an array #11

Closed enekochan closed 9 months ago

enekochan commented 10 months ago

When getting the data of a TwitchResponse you get an array of objects defined in the Dto folder. Then to get the individual values you have to call the methods in the specific Dto (and for example the TwitchUser::getEmail() method will fail if you don't have the user:read:email scope because the property won't exist).

Would be a good idea to add the possibility to get the data as an array, for example to symplify creating JSON values.

To use the same code for all the Dto a trait could be used:

<?php

namespace SimplyStream\TwitchApiBundle\Helix\Dto;

trait DtoTrait
{
    public function asArray(): array
    {
        $values = [];

        $reflection = new \ReflectionClass(get_class($this));
        $properties = $reflection->getProperties();
        foreach ($properties as $property) {
            if ($property->isInitialized($this)) {
                $values[$property->getName()] = $property->getValue($this);
            }
        }

        return $values;
    }
}

And then use it in all the Dto classes, for example in TwitchUser.php:

<?php

namespace SimplyStream\TwitchApiBundle\Helix\Dto;

use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;

class TwitchUser
{
    use DtoTrait;

    ...
}

Finally use it after calling the API:

class DefaultController extends AbstractController
{
    #[Route('/twitch_users', name: 'app_twitch')]
    public function twitch(TwitchApi $twitchApi): Response
    {
        $twitchResponse = $twitchApi->getUsersApi()->getUsers([], ['xxxxxx']);

        $users = $twitchResponse->getData();
        $data = array_map(static function($user) {
            return $user->asArray();
        }, $users);

        return new JsonResponse($data);
    }
}

And would have this as result:

[{"id":"83232866","broadcaster_type":"partner","description":"Si lees esto que sepas que te aprecio","display_name":"ibai","login":"ibai","offline_image_url":"https:\/\/static-cdn.jtvnw.net\/jtv_user_pictures\/b01927d9-1cc2-4ba0-b3e2-6e96959179d0-channel_offline_image-1920x1080.jpeg","profile_image_url":"https:\/\/static-cdn.jtvnw.net\/jtv_user_pictures\/574228be-01ef-4eab-bc0e-a4f6b68bedba-profile_image-300x300.png","type":"","view_count":0,"created_at":{"date":"2015-02-20 16:47:56.000000","timezone_type":2,"timezone":"Z"}}]
aaricdev commented 10 months ago

That's actually a nice idea that I didn't thought about :D I'll implement the toArray-method.

and for example the TwitchUser::getEmail() method will fail if you don't have the user:read:email scope because the property won't exist

This should be fixed with the latest commit from yesterday, I stumbled upon this error on accident, too a few days ago.

aaricdev commented 9 months ago

Implemented in https://github.com/Simply-Stream/TwitchApi/blob/main/src/Helix/Models/SerializesModels.php