<commit_before><?php

declare(strict_types=1);

/*
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Core23\LastFm\Connection;

use Core23\LastFm\Exception\ApiException;

abstract class AbstractConnection implements ConnectionInterface
{
    /**
     * Default Endpoint.
     */
    public const DEFAULT_WS_ENDPOINT = 'http://ws.audioscrobbler.com/2.0/';

    /**
     * @var string
     */
    protected $apiKey;

    /**
     * @var string
     */
    protected $sharedSecret;

    /**
     * @var string
     */
    protected $uri;

    /**
     * AbstractConnection constructor.
     *
     * @param string $apikey
     * @param string $sharedSecret
     * @param string $uri
     */
    public function __construct(string $apikey, string $sharedSecret, string $uri = null)
    {
        if (null === $uri) {
            $uri = static::DEFAULT_WS_ENDPOINT;
        }

        $this->apiKey       = $apikey;
        $this->sharedSecret = $sharedSecret;
        $this->uri          = $uri;
    }

    /**
     * {@inheritdoc}
     */
    public function signedCall(string $method, array $params = [], SessionInterface $session = null, $requestMethod = 'GET'): array
    {
        // Call parameter
        $callParams = [
            'method'  => $method,
            'api_key' => $this->apiKey,
        ];

        // Add session key
        if (null !== $session) {
            $callParams['sk'] = $session->getKey();
        }

        $params = array_merge($callParams, $params);
        $params = $this->filterNull($params);
        $params = $this->encodeUTF8($params);

        // Sign parameter
        $params['api_sig'] = $this->signParams($params);

        return $this->call($params, $requestMethod);
    }

    /**
     * {@inheritdoc}
     */
    public function unsignedCall(string $method, array $params = [], string $requestMethod = 'GET'): array
    {
        // Call parameter
        $callParameter = [
            'method'  => $method,
            'api_key' => $this->apiKey,
        ];

        $params = array_merge($callParameter, $params);
        $params = $this->filterNull($params);
        $params = $this->encodeUTF8($params);

        return $this->call($params, $requestMethod);
    }

    /**
     * {@inheritdoc}
     */
    public function getApiKey(): string
    {
        return $this->apiKey;
    }

    /**
     * {@inheritdoc}
     */
    public function getSharedSecret(): string
    {
        return $this->sharedSecret;
    }

    /**
     * Performs the webservice call.
     *
     * @param array  $params
     * @param string $requestMethod
     *
     * @throws ApiException
     *
     * @return array
     */
    abstract protected function call(array $params, string $requestMethod = 'GET'): array;

    /**
     * Filter null values.
     *
     * @param array $object
     *
     * @return array
     */
    private function filterNull(array $object): array
    {
        return array_filter($object, function ($val) {
            return null !== $val;
        });
    }

    /**
     * Converts any string or array of strings to UTF8.
     *
     * @param string|string[] $object a String or an array
     *
     * @return string|string[] UTF8-string or array
     */
    private function encodeUTF8($object)
    {
        if (is_array($object)) {
            return array_map([$this, 'encodeUTF8'], $object);
        }

        return mb_convert_encoding($object, 'UTF-8', 'auto');
    }

    /**
     * @param array $params
     *
     * @return string
     */
    private function signParams(array $params): string
    {
        ksort($params);<commit_msg>Added missing typecast<commit_after><?php

declare(strict_types=1);

/*
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Core23\LastFm\Connection;

use Core23\LastFm\Exception\ApiException;

abstract class AbstractConnection implements ConnectionInterface
{
    /**
     * Default Endpoint.
     */
    public const DEFAULT_WS_ENDPOINT = 'http://ws.audioscrobbler.com/2.0/';

    /**
     * @var string
     */
    protected $apiKey;

    /**
     * @var string
     */
    protected $sharedSecret;

    /**
     * @var string
     */
    protected $uri;

    /**
     * AbstractConnection constructor.
     *
     * @param string $apikey
     * @param string $sharedSecret
     * @param string $uri
     */
    public function __construct(string $apikey, string $sharedSecret, string $uri = null)
    {
        if (null === $uri) {
            $uri = static::DEFAULT_WS_ENDPOINT;
        }

        $this->apiKey       = $apikey;
        $this->sharedSecret = $sharedSecret;
        $this->uri          = $uri;
    }

    /**
     * {@inheritdoc}
     */
    public function signedCall(string $method, array $params = [], SessionInterface $session = null, $requestMethod = 'GET'): array
    {
        // Call parameter
        $callParams = [
            'method'  => $method,
            'api_key' => $this->apiKey,
        ];

        // Add session key
        if (null !== $session) {
            $callParams['sk'] = $session->getKey();
        }

        $params = array_merge($callParams, $params);
        $params = $this->filterNull($params);
        $params = $this->encodeUTF8($params);

        // Sign parameter
        $params['api_sig'] = $this->signParams($params);

        return $this->call($params, $requestMethod);
    }

    /**
     * {@inheritdoc}
     */
    public function unsignedCall(string $method, array $params = [], string $requestMethod = 'GET'): array
    {
        // Call parameter
        $callParameter = [
            'method'  => $method,
            'api_key' => $this->apiKey,
        ];

        $params = array_merge($callParameter, $params);
        $params = $this->filterNull($params);
        $params = $this->encodeUTF8($params);

        return $this->call($params, $requestMethod);
    }

    /**
     * {@inheritdoc}
     */
    public function getApiKey(): string
    {
        return $this->apiKey;
    }

    /**
     * {@inheritdoc}
     */
    public function getSharedSecret(): string
    {
        return $this->sharedSecret;
    }

    /**
     * Performs the webservice call.
     *
     * @param array  $params
     * @param string $requestMethod
     *
     * @throws ApiException
     *
     * @return array
     */
    abstract protected function call(array $params, string $requestMethod = 'GET'): array;

    /**
     * Filter null values.
     *
     * @param array $object
     *
     * @return array
     */
    private function filterNull(array $object): array
    {
        return array_filter($object, function ($val) {
            return null !== $val;
        });
    }

    /**
     * Converts any string or array of strings to UTF8.
     *
     * @param string|string[] $object a String or an array
     *
     * @return string|string[] UTF8-string or array
     */
    private function encodeUTF8($object)
    {
        if (is_array($object)) {
            return array_map([$this, 'encodeUTF8'], $object);
        }

        return mb_convert_encoding((string) $object, 'UTF-8', 'auto');
    }

    /**
     * @param array $params
     *
     * @return string
     */
    private function signParams(array $params): string
    {
        ksort($params);
{'fraction_non_alphanumeric': 0.13839166444028853, 'fraction_numerical': 0.0037403152551429334, 'mean_word_length': 1.9828685258964143, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5270515', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2152, 'n_words': 512}