<commit_before><?php

namespace allejo\Socrata;

use allejo\Socrata\Converters\Converter;
use allejo\Socrata\Utilities\StringUtilities;
use allejo\Socrata\Utilities\UrlQuery;

class SodaDataset
{
    private $sodaClient;
    private $urlQuery;
    private $resourceId;
    private $apiVersion;

    public function __construct ($sodaClient, $resourceID)
    {
        StringUtilities::validateResourceID($resourceID);

        if (!($sodaClient instanceof SodaClient))
        {
            throw new \InvalidArgumentException("The first variable is expected to be a SodaClient object");
        }

        $this->apiVersion = 0;
        $this->sodaClient = $sodaClient;
        $this->resourceId = $resourceID;
        $this->urlQuery   = new UrlQuery($this->buildResourceUrl(), $this->sodaClient->getToken(), $this->sodaClient->getEmail(), $this->sodaClient->getPassword());

        $this->urlQuery->setOAuth2Token($this->sodaClient->getOAuth2Token());
    }

    /**
     * Get the API version this dataset is using
     *
     * @since  0.1.0
     *
     * @return int The API version number
     */
    public function getApiVersion ()
    {
        // If we don't have the API version set, send a dummy query with limit 0 since we only care about the headers
        if ($this->apiVersion == 0)
        {
            $soql = new SoqlQuery();
            $soql->limit(0);

            // When we fetch a dataset, the API version is stored
            $this->getDataset($soql);
        }

        return $this->apiVersion;
    }

    /**
     * Get the metadata of a dataset
     *
     * @see    SodaClient::enableAssociativeArrays()
     * @see    SodaClient::disableAssociativeArrays()
     *
     * @since  0.1.0
     *
     * @return array The metadata as a PHP array. The array will contain associative arrays or stdClass objects from
     *               the decoded JSON received from the data set.
     */
    public function getMetadata ()
    {
        $metadataUrlQuery = new UrlQuery($this->buildViewUrl(), $this->sodaClient->getToken(), $this->sodaClient->getEmail(), $this->sodaClient->getPassword());

        return $metadataUrlQuery->sendGet("", $this->sodaClient->associativeArrayEnabled());
    }

    /**
     * Fetch a dataset based on a resource ID.
     *<commit_msg>Update phpDoc and use the OAuth2.0 token in getMetadata<commit_after><?php

/**
 * This file contains the SodaDataset class
 *
 * @copyright 2015 <NAME>
 * @license   https://www.gnu.org/licenses/lgpl-2.1.html LGPL-2.1
 */

namespace allejo\Socrata;

use allejo\Socrata\Converters\Converter;
use allejo\Socrata\Utilities\StringUtilities;
use allejo\Socrata\Utilities\UrlQuery;
use allejo\Socrata\Exceptions\InvalidResourceException;

/**
 * An object provided to interact with a Socrata dataset directly. Provides functionality for fetching the dataset, an
 * individual row, or updating/replacing a dataset.
 *
 * @package allejo\Socrata
 * @since   0.1.0
 */
class SodaDataset
{
    /**
     * The client with all the authentication and configuration set
     *
     * @var SodaClient
     */
    private $sodaClient;

    /**
     * The object used to make URL jobs for common requests
     *
     * @var UrlQuery
     */
    private $urlQuery;

    /**
     * The 4x4 resource ID of a dataset
     *
     * @var string
     */
    private $resourceId;

    /**
     * The API version of the dataset being worked with
     *
     * @var int
     */
    private $apiVersion;

    /**
     * Create an object for interacting with a Socrata dataset
     *
     * @param  SodaClient $sodaClient   The SodaClient with all of the authentication information and settings for access
     * @param  string     $resourceID   The 4x4 resource ID of the dataset that will be referenced
     *
     * @throws InvalidResourceException If the given resource ID does not match the pattern of a resource ID
     *
     * @since 0.1.0
     */
    public function __construct ($sodaClient, $resourceID)
    {
        StringUtilities::validateResourceID($resourceID);

        if (!($sodaClient instanceof SodaClient))
        {
            throw new \InvalidArgumentException("The first variable is expected to be a SodaClient object");
        }

        $this->apiVersion = 0;
        $this->sodaClient = $sodaClient;
        $this->resourceId = $resourceID;
        $this->urlQuery   = new UrlQuery($this->buildResourceUrl(), $this->sodaClient->getToken(), $this->sodaClient->getEmail(), $this->sodaClient->getPassword());

        $this->urlQuery->setOAuth2Token($this->sodaClient->getOAuth2Token());
    }

    /**
     * Get the API version this dataset is using
     *
     * @since  0.1.0
     *
     * @return int The API version number
     */
    public function getApiVersion ()
    {
        // If we don't have the API version set, send a dummy query with limit 0 since we only care about the headers
        if ($this->apiVersion == 0)
        {
            $soql = new SoqlQuery();
            $soql->limit(0);

            // When we fetch a dataset, the API version is stored
            $this->getDataset($soql);
        }

        return $this->apiVersion;
    }

    /**
     * Get the metadata of a dataset
     *
     * @see    SodaClient::enableAssociativeArrays()
     * @see    SodaClient::disableAssociativeArrays()
     *
     * @since  0.1.0
     *
     * @return array The metadata as a PHP array. The array will contain associative arrays or stdClass objects from
     *               the decoded JSON received from the data set.
     */
    public function getMetadata ()
    {
        $metadataUrlQuery = new UrlQuery($this->buildViewUrl(), $this->sodaClient->getToken(), $this->sodaClient->getEmail(), $this->sodaClient->getPassword());
        $metadataUrlQuery->setOAuth2Token($this->sodaClient->getOAuth2Token());

        return $metadataUrlQuery->sendGet("", $this->sodaClient->associativeArrayEnabled());
    }

    /**
     * Fetch a dataset based on a resource ID.
     *
{'fraction_non_alphanumeric': 0.10979476055397965, 'fraction_numerical': 0.0076756215584848995, 'mean_word_length': 2.4950437317784258, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 65, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '1734977', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1715, 'n_words': 495}