Can't Instantiate UserProfilePhotos - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, August 7, 2023

Can't Instantiate UserProfilePhotos

I'm using the TelegramBot Api and the TelegramBot library, When I try to Instantiate the class UserProfilePhotos after the function getUserProfilePhotos() the code gives me an empty array, i tried to check if the call gets any data and it gets it. So i think it's either an error on the fromResponse() function or the map() function. Can someone help me?

Class UserProfilePhotos

<?php

namespace TelegramBot\Api\Types;

use TelegramBot\Api\BaseType;
use TelegramBot\Api\InvalidArgumentException;
use TelegramBot\Api\TypeInterface;

/**
 * Class UserProfilePhotos
 * This object represent a user's profile pictures.
 *
 * @package TelegramBot\Api\Types
 */
class UserProfilePhotos extends BaseType implements TypeInterface
{
    /**
     * {@inheritdoc}
     *
     * @var array
     */
    protected static $requiredParams = ['total_count', 'photos'];

    /**
     * {@inheritdoc}
     *
     * @var array
     */
    protected static $map = [
        'total_count' => true,
        'photos' => ArrayOfArrayOfPhotoSize::class,
    ];

    /**
     * Total number of profile pictures the target user has
     *
     * @var int
     */
    protected $totalCount;

    /**
     * Requested profile pictures (in up to 4 sizes each).
     * Array of Array of \TelegramBot\Api\Types\PhotoSize
     *
     * @var array
     */
    protected $photos;

    /**
     * @return array
     */
    public function getPhotos()
    {
        return $this->photos;
    }

    /**
     * @param array $photos
     *
     * @return void
     */
    public function setPhotos($photos)
    {
        $this->photos = $photos;
    }

    /**
     * @return int
     */
    public function getTotalCount()
    {
        return $this->totalCount;
    }

    /**
     * @param mixed $totalCount
     *
     * @throws InvalidArgumentException
     *
     * @return void
     */
    public function setTotalCount($totalCount)
    {
        if (is_integer($totalCount)) {
            $this->totalCount = $totalCount;
        } else {
            throw new InvalidArgumentException();
        }
    }
}

Function getUserProfilePhotos

    public function getUserProfilePhotos($userId, $offset = 0, $limit = 100)
    {
        return UserProfilePhotos::fromResponse($this->call('getUserProfilePhotos', [
            'user_id' => (int)$userId,
            'offset' => (int)$offset,
            'limit' => (int)$limit,
        ]));
    }

fromResponse and map functions

    public static function fromResponse($data)
    {
        self::validate($data);
        /** @psalm-suppress UnsafeInstantiation */
        $instance = new static();
        $instance->map($data);

        return $instance;
    }

    public function map($data)
    {
        foreach (static::$map as $key => $item) {
            if (isset($data[$key]) && (!is_array($data[$key]) || !empty($data[$key]))) {
                $method = 'set' . self::toCamelCase($key);
                if ($item === true) {
                    $this->$method($data[$key]);
                } else {
                    $this->$method($item::fromResponse($data[$key]));
                }
            }
        }
    }

the response to the request: { "ok": true, "result": { "total_count": 3, "photos": [ [ { "file_id": "AgACAgQAAxUAAWTPytagBeTeInkJ_1bUGok5M141AAKtpzEbLJjYH8iJ2qkdaVW5AQADAgADYQADLwQ", "file_unique_id": "AQADracxGyyY2B8AAQ", "file_size": 10458, "width": 160, "height": 160 }, { "file_id": "AgACAgQAAxUAAWTPytagBeTeInkJ_1bUGok5M141AAKtpzEbLJjYH8iJ2qkdaVW5AQADAgADYgADLwQ", "file_unique_id": "AQADracxGyyY2B9n", "file_size": 25941, "width": 320, "height": 320 }, { "file_id": "AgACAgQAAxUAAWTPytagBeTeInkJ_1bUGok5M141AAKtpzEbLJjYH8iJ2qkdaVW5AQADAgADYwADLwQ", "file_unique_id": "AQADracxGyyY2B8B", "file_size": 65056, "width": 640, "height": 640 } ], [ { "file_id": "AgACAgQAAxUAAWTPyta2TX7K1rJI1S5O2bKcHVY_AAKspzEbLJjYH_IMeYUl7rH7AQADAgADYQADLwQ", "file_unique_id": "AQADrKcxGyyY2B8AAQ", "file_size": 8298, "width": 160, "height": 160 }, { "file_id": "AgACAgQAAxUAAWTPyta2TX7K1rJI1S5O2bKcHVY_AAKspzEbLJjYH_IMeYUl7rH7AQADAgADYgADLwQ", "file_unique_id": "AQADrKcxGyyY2B9n", "file_size": 24820, "width": 320, "height": 320 }, { "file_id": "AgACAgQAAxUAAWTPyta2TX7K1rJI1S5O2bKcHVY_AAKspzEbLJjYH_IMeYUl7rH7AQADAgADYwADLwQ", "file_unique_id": "AQADrKcxGyyY2B8B", "file_size": 79652, "width": 640, "height": 640 } ], [ { "file_id": "AgACAgQAAxUAAWTPytbZj5OAUAty5GdJ-xjKXtlcAAKppzEbLJjYH6M8u-eWii_AAQADAgADYQADLwQ", "file_unique_id": "AQADqacxGyyY2B8AAQ", "file_size": 9812, "width": 160, "height": 160 }, { "file_id": "AgACAgQAAxUAAWTPytbZj5OAUAty5GdJ-xjKXtlcAAKppzEbLJjYH6M8u-eWii_AAQADAgADYgADLwQ", "file_unique_id": "AQADqacxGyyY2B9n", "file_size": 26520, "width": 320, "height": 320 }, { "file_id": "AgACAgQAAxUAAWTPytbZj5OAUAty5GdJ-xjKXtlcAAKppzEbLJjYH6M8u-eWii_AAQADAgADYwADLwQ", "file_unique_id": "AQADqacxGyyY2B8B", "file_size": 70032, "width": 640, "height": 640 } ] ] } }

I tried to debug the functions but i can't understand what is the reason it won't instantiate the class...



source https://stackoverflow.com/questions/76847189/cant-instantiate-userprofilephotos

No comments:

Post a Comment