<commit_before><?php namespace Arcanedev\Stripe;

use Arcanedev\Stripe\Contracts\CollectionInterface;
use Arcanedev\Stripe\Exceptions\ApiException;
use Arcanedev\Stripe\Utilities\Util;

/**
 * Class Collection
 * @package Arcanedev\Stripe
 *
 * @property string object
 * @property array  data
 * @property int    total_count
 * @property bool   has_more
 * @property string url
 */
class Collection extends StripeResource implements CollectionInterface
{
    /* ------------------------------------------------------------------------------------------------
     |  CRUD Functions
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * List Function
     *
     * @param  array             $params
     * @param  array|string|null $options
     *
     * @throws ApiException
     *
     * @return Collection|array
     */
    public function all($params = [], $options = null)
    {
        return $this->requestAndConvertToStripeObject('get', $params, $options);
        //list($url, $params)    = $this->extractPathAndUpdateParams($params);
        //list($response, $opts) = $this->request('get', $url, $params, $options);
        //
        //return Util::convertToStripeObject($response, $opts);
    }

    /**
     * Create Function
     *
     * @param  array             $params
     * @param  array|string|null $options
     *
     * @throws ApiException
     *
     * @return StripeObject|StripeResource|array
     */
    public function create($params = [], $options = null)
    {
        return $this->requestAndConvertToStripeObject('post', $params, $options);
        //list($url, $params)    = $this->extractPathAndUpdateParams($params);
        //list($response, $opts) = $this->request('post', $url, $params);
        //
        //return Util::convertToStripeObject($response, $opts);
    }

    /**
     * Retrieve Function
     *
     * @param  string            $id
     * @param  array             $params
     * @param  array|string|null $options
     *
     * @throws ApiException
     *
     * @return StripeObject|StripeResource|array
     */
    public function retrieve($id, $params = [], $options = null)
    {
        list($url, $params)    = $this->extractPathAndUpdateParams($params);
        $extn                  = urlencode(str_utf8($id));
        list($response, $opts) = $this->request('get', "$url/$extn", $params);

        return Util::convertToStripeObject($response, $opts);
    }

    /**
     * Make a request and convert to Stripe object
     *
     * @param  string            $method
     * @param  array             $params
     * @param  array|string|null $options
     *
     * @return Collection|StripeObject|StripeResource|array|
     */
    private function requestAndConvertToStripeObject($method, $params, $options)
    {
        list($url, $params)    = $this->extractPathAndUpdateParams($params);
        list($response, $opts) = $this->request($method, $url, $params, $options);

        return Util::convertToStripeObject($response, $opts);
    }

    /* ------------------------------------------------------------------------------------------------
     |  Check Functions
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * Check if Object is list
     *
     * @return bool
     */
    public function isList()
    {
        return $this->object === 'list';
    }

    /**
     * Check if URL has path
     *
     * @param  array $url
     *
     * @throws ApiException
     */
    private function checkPath(array $url)
    {
        if (! isset($url['path']) or empty($url['path'])) {
            throw new ApiException(
                'Could not parse list url into parts: ' . $this->url
            );
        }
    }

    /* ------------------------------------------------------------------------------------------------
     |  Other Functions
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * Get items Count
     *
     * @return int
     */
    public function count()
    {
        return ($this->isList() and isset($this->total_count))
            ? $this->total_count
            : 0;
    }

    /**
     * Extract Path And Update Parameters
     *
     * @param  $params
     *
     * @throws ApiException
     *
     * @return array
     */
    private function extractPathAndUpdateParams($params)
    {
        $url = parse_url($this->url);

        if ($url === false) {<commit_msg>Cleaning and refactoring the Collection Class<commit_after><?php namespace Arcanedev\Stripe;

use Arcanedev\Stripe\Contracts\CollectionInterface;
use Arcanedev\Stripe\Exceptions\ApiException;
use Arcanedev\Stripe\Utilities\Util;

/**
 * Class Collection
 * @package Arcanedev\Stripe
 *
 * @property string object
 * @property array  data
 * @property int    total_count
 * @property bool   has_more
 * @property string url
 */
class Collection extends StripeResource implements CollectionInterface
{
    /* ------------------------------------------------------------------------------------------------
     |  CRUD Functions
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * List Function
     *
     * @param  array             $params
     * @param  array|string|null $options
     *
     * @throws ApiException
     *
     * @return Collection|array
     */
    public function all($params = [], $options = null)
    {
        return $this->requestAndConvertToStripeObject('get', $params, $options);
    }

    /**
     * Create Function
     *
     * @param  array             $params
     * @param  array|string|null $options
     *
     * @throws ApiException
     *
     * @return StripeObject|StripeResource|array
     */
    public function create($params = [], $options = null)
    {
        return $this->requestAndConvertToStripeObject('post', $params, $options);
    }

    /**
     * Retrieve Function
     *
     * @param  string            $id
     * @param  array             $params
     * @param  array|string|null $options
     *
     * @throws ApiException
     *
     * @return StripeObject|StripeResource|array
     */
    public function retrieve($id, $params = [], $options = null)
    {
        list($url, $params)    = $this->extractPathAndUpdateParams($params);
        $extn                  = urlencode(str_utf8($id));
        list($response, $opts) = $this->request('get', "$url/$extn", $params);

        return Util::convertToStripeObject($response, $opts);
    }

    /**
     * Make a request and convert to Stripe object
     *
     * @param  string            $method
     * @param  array             $params
     * @param  array|string|null $options
     *
     * @return Collection|StripeObject|StripeResource|array|
     */
    private function requestAndConvertToStripeObject($method, $params, $options)
    {
        list($url, $params)    = $this->extractPathAndUpdateParams($params);
        list($response, $opts) = $this->request($method, $url, $params, $options);

        return Util::convertToStripeObject($response, $opts);
    }

    /* ------------------------------------------------------------------------------------------------
     |  Check Functions
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * Check if Object is list
     *
     * @return bool
     */
    public function isList()
    {
        return $this->object === 'list';
    }

    /**
     * Check if URL has path
     *
     * @param  array $url
     *
     * @throws ApiException
     */
    private function checkPath(array $url)
    {
        if ( ! isset($url['path']) || empty($url['path'])) {
            throw new ApiException(
                'Could not parse list url into parts: ' . $this->url
            );
        }
    }

    /* ------------------------------------------------------------------------------------------------
     |  Other Functions
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * Get items Count
     *
     * @return int
     */
    public function count()
    {
        return ($this->isList() && isset($this->total_count))
            ? $this->total_count
            : 0;
    }

    /**
     * Extract Path And Update Parameters
     *
     * @param  $params
     *
     * @throws ApiException
     *
     * @return array
     */
    private function extractPathAndUpdateParams($params)
    {
        $url = parse_url($this->url);

        if ($url === false) {
{'fraction_non_alphanumeric': 0.24556990968332, 'fraction_numerical': 0.00045729964559277465, 'mean_word_length': 1.3771739130434784, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '4075312', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2258, 'n_words': 480}