<commit_before><?php

namespace Aensley\YahooStockQuotes;

/**
 * Yahoo Stock Quotes class to retrieve quote data from Yahoo's API.
 *
 * @package    Aensley/YahooStockQuotes
 * @author     <NAME>
 */
class YahooStockQuotes {

	/**
	 * The array of stock symbols to retrieve.
	 *
	 * @var array
	 */
	private $stockSymbols = array();

	/**
	 * Name of file where stock data is cached.
	 *
	 * @var string
	 */
	private $fileName = 'YahooStockQuotes.json';

	/**
	 * Associative array containing stock data.
	 *
	 * @var array
	 */
	private $data = array();

	/**
	 * The most recent 'updated' date among all stock information stored as a timestamp.
	 *
	 * @var int
	 */
	private $timestamp;

	/**
	 * The base of the URL to fetch stock data from.
	 *
	 * @var string
	 */
	private $urlBase = 'https://query.yahooapis.com/v1/public/yql?format=json&env=%ENV%&q=%QUERY%';

	/**
	 * The query to pass to Yahoo's API for stock data.
	 *
	 * @var string
	 */
	private $urlQuery = 'SELECT symbol, LastTradePriceOnly, ChangeinPercent, LastTradeDate, LastTradeTime FROM yahoo.finance.quotes WHERE symbol IN (%STOCKS%)';

	/**
	 * The environment for the Yahoo API request.
	 *
	 * @var string
	 */
	private $urlEnv = 'store://datatables.org/alltableswithkeys';


	/**
	 * Creates the StockQuote object.
	 *
	 * @param array $symbols An array of desired stock symbols to retrieve.
	 */
	public function __construct($symbols = array())
	{
		if (empty($symbols) || !is_array($symbols)) {
			return;
		}

		$this->stockSymbols = $symbols;
		$this->fileName = dirname(realpath(__FILE__)) . '/' . $this->fileName;
		$this->getCachedData();
		if (!$this->isCurrent()) {
			$this->fetchNew();
		}
	}


	/**
	 * Retrieves cached data to save a request to Yahoo's API.
	 */
	private function getCachedData()
	{
		if (is_readable($this->fileName)) {
			$this->data = json_decode(file_get_contents($this->fileName), true);
		}
	}


	/**
	 * Checks whether the cached data is current enough.
	 * This essentially triggers a new update on the first request after 12am every day.
	 *
	 * @return bool True if current. False if not.
	 */
	private function isCurrent() {
		return (!empty($this->data) && !empty($this->data['date']) && date('Y-m-d') === $this->data['date']);
	}


	/**
	 * Retrieves new quote data from Yahoo.
	 */
	private function fetchNew()
	{
		$this->data['stocks'] = array();
		// Replace %ENV% in $urlBase with encoded $urlEnv
		$url = str_replace('%ENV%', urlencode($this->urlEnv), $this->urlBase);
		// Replace %STOCKS% in $urlQuery with query-formatted stock symbol list.
		$query = str_replace('%STOCKS%', '"' . implode('","', $this->stockSymbols) . '"', $this->urlQuery);
		// Replace %QUERY% in $url ($urlBase with $urlEnv from above)
		// with encoded $query ($urlQuery with query-formatted stock symbols from above).
		$url = str_replace('%QUERY%', urlencode($query), $url);
		$data = @file_get_contents($url);
		if (!$data && function_exists('curl_init')) {
			$curlResource = curl_init();
			$timeout = 5; // set to zero for no timeout
			curl_setopt($curlResource, CURLOPT_URL, $url);
			curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curlResource, CURLOPT_CONNECTTIMEOUT, $timeout);
			$data = curl_exec($curlResource);
			curl_close($curlResource);
		}

		if ($data) {
			$data = json_decode($data, true);
			if (!empty($data) && !empty($data['query']['results']['quote'])) {
				foreach ($data['query']['results']['quote'] as $quote) {
					$this->setSingleStock($quote['symbol'], $quote['LastTradePriceOnly'], $quote['ChangeinPercent'], $quote['LastTradeDate'], $quote['LastTradeTime']);
				}

				$this->save();
			}
		}
	}


	/**
	 * Sets a single stock's properties.
	 *
	 * @param string $symbol The symbol for the stock.
	 * @param string $price  The stock's price.
	 * @param string $change The stock's change.
	 * @param string $date   The stock's last trade date.
	 * @param string $time   The stock's last trade time.
	 */
	private function setSingleStock($symbol = '', $price = '', $change = '', $date = '', $time = '')
	{<commit_msg>Fixed bug that caused queried for just one symbol to fail.<commit_after><?php

namespace Aensley\YahooStockQuotes;

/**
 * Yahoo Stock Quotes class to retrieve quote data from Yahoo's API.
 *
 * @package    Aensley/YahooStockQuotes
 * @author     <NAME>
 */
class YahooStockQuotes {

	/**
	 * The array of stock symbols to retrieve.
	 *
	 * @var array
	 */
	private $stockSymbols = array();

	/**
	 * Name of file where stock data is cached.
	 *
	 * @var string
	 */
	private $fileName = 'YahooStockQuotes.json';

	/**
	 * Associative array containing stock data.
	 *
	 * @var array
	 */
	private $data = array();

	/**
	 * The most recent 'updated' date among all stock information stored as a timestamp.
	 *
	 * @var int
	 */
	private $timestamp;

	/**
	 * The base of the URL to fetch stock data from.
	 *
	 * @var string
	 */
	private $urlBase = 'https://query.yahooapis.com/v1/public/yql?format=json&env=%ENV%&q=%QUERY%';

	/**
	 * The query to pass to Yahoo's API for stock data.
	 *
	 * @var string
	 */
	private $urlQuery = 'SELECT symbol, LastTradePriceOnly, ChangeinPercent, LastTradeDate, LastTradeTime FROM yahoo.finance.quotes WHERE symbol IN (%STOCKS%)';

	/**
	 * The environment for the Yahoo API request.
	 *
	 * @var string
	 */
	private $urlEnv = 'store://datatables.org/alltableswithkeys';


	/**
	 * Creates the StockQuote object.
	 *
	 * @param array $symbols An array of desired stock symbols to retrieve.
	 */
	public function __construct($symbols = array())
	{
		if (empty($symbols) || !is_array($symbols)) {
			return;
		}

		$this->stockSymbols = $symbols;
		$this->fileName = dirname(realpath(__FILE__)) . '/' . $this->fileName;
		$this->getCachedData();
		if (!$this->isCurrent()) {
			$this->fetchNew();
		}
	}


	/**
	 * Retrieves cached data to save a request to Yahoo's API.
	 */
	private function getCachedData()
	{
		if (is_readable($this->fileName)) {
			$this->data = json_decode(file_get_contents($this->fileName), true);
		}
	}


	/**
	 * Checks whether the cached data is current enough.
	 * This essentially triggers a new update on the first request after 12am every day.
	 *
	 * @return bool True if current. False if not.
	 */
	private function isCurrent() {
		return (!empty($this->data) && !empty($this->data['date']) && date('Y-m-d') === $this->data['date']);
	}


	/**
	 * Retrieves new quote data from Yahoo.
	 */
	private function fetchNew()
	{
		$this->data['stocks'] = array();
		// Replace %ENV% in $urlBase with encoded $urlEnv
		$url = str_replace('%ENV%', urlencode($this->urlEnv), $this->urlBase);
		// Replace %STOCKS% in $urlQuery with query-formatted stock symbol list.
		$query = str_replace('%STOCKS%', '"' . implode('","', $this->stockSymbols) . '"', $this->urlQuery);
		// Replace %QUERY% in $url ($urlBase with $urlEnv from above)
		// with encoded $query ($urlQuery with query-formatted stock symbols from above).
		$url = str_replace('%QUERY%', urlencode($query), $url);
		$data = @file_get_contents($url);
		if (!$data && function_exists('curl_init')) {
			$curlResource = curl_init();
			$timeout = 5; // set to zero for no timeout
			curl_setopt($curlResource, CURLOPT_URL, $url);
			curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curlResource, CURLOPT_CONNECTTIMEOUT, $timeout);
			$data = curl_exec($curlResource);
			curl_close($curlResource);
		}

		if ($data) {
			$data = json_decode($data, true);
			if (!empty($data) && !empty($data['query']['results']['quote'])) {
				$quotes = $data['query']['results']['quote'];
				// Is this an associative array?
				if (array_keys($quotes) !== range(0, count($quotes) - 1)) {
					// Yes, we were given a single quote not contained in a 0-indexed array. Make it one.
					$quotes = array($quotes);
				}

				foreach ($quotes as $quote) {
					$this->setSingleStock($quote['symbol'], $quote['LastTradePriceOnly'], $quote['ChangeinPercent'], $quote['LastTradeDate'], $quote['LastTradeTime']);
				}

				$this->save();
			}
		}
	}


	/**
	 * Sets a single stock's properties.
	 *
	 * @param string $symbol The symbol for the stock.
	 * @param string $price  The stock's price.
	 * @param string $change The stock's change.
	 * @param string $date   The stock's last trade date.
	 * @param string $time   The stock's last trade time.
	 */
	private function setSingleStock($symbol = '', $price = '', $change = '', $date = '', $time = '')
	{
{'fraction_non_alphanumeric': 0.1622547861025762, 'fraction_numerical': 0.0015362798392814937, 'mean_word_length': 3.6019575856443717, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 2, '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': '2757283', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2683, 'n_words': 770}