<commit_before><?php

namespace Kenjdavidson\OAuth2\YahooFantasySports\Provider\Service;

use Kenjdavidson\OAuth2\YahooFantasySports\Provider\YahooFantasyProvider;
use Kenjdavidson\OAuth2\YahooFantasySports\Provider\Resource\GameResource;
use Kenjdavidson\OAuth2\YahooFantasySports\Provider\Resource\LeagueResource;
use Kenjdavidson\OAuth2\YahooFantasySports\Provider\Resource\TeamResource;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;

/**
 * Standard method used within Yahoo Resource Services.  This service makes
 * calls to the API (which respond in XML) and convert the message to JSON.  
 * This is annoying, but the YQL service doesn't like the authorized request
 * and will not respond.
 * 
 * Regular API:
 * 
 * Games:
 * @link https://developer.yahoo.com/fantasysports/guide/#game-resource
 * @link https://developer.yahoo.com/fantasysports/guide/#games-collection
 * 
 * The games collection has the available sub-resource: leagues
 * 
 * Leagues:
 * @link https://developer.yahoo.com/fantasysports/guide/#leagues-collection
 * @link https://developer.yahoo.com/fantasysports/guide/#league-resource
 * 
 * The leagues collection has the available sub-resources: settings, standings,
 * scoreboard and teams
 *
 * Teams:
 * @link https://developer.yahoo.com/fantasysports/guide/#teams-collection
 * @link https://developer.yahoo.com/fantasysports/guide/#team-resource
 * 
 * The teams collection has the available sub-resources: matchups;weeks=, 
 * stats;type=[season|date], roster
 * 
 * Roster:
 * @link https://developer.yahoo.com/fantasysports/guide/#roster-collection
 * @link https://developer.yahoo.com/fantasysports/guide/#roster-resource
 * 
 * The roster collection has the available sub-resources: player
 * 
 * Player:
 * @link https://developer.yahoo.com/fantasysports/guide/#roster-collection
 * @link https://developer.yahoo.com/fantasysports/guide/#roster-resource
 * 
 * The player collection has the available sub-resourceS: stats
 * 
 *  
 * 
 * @author <NAME>
 */
class YahooFantasyService {
    
    // API base URI
    const API_BASE = 'https://fantasysports.yahooapis.com/fantasy/v2';
    
    // Collection URIs for specific request types
    const COLLECTION_URI = array(
        'games'      =>  '/users%s/games;game_keys=%s',
        'leagues'    =>  '/users%s/games/leagues;league_keys=%s%s',
        'teams'      =>  '/users%s/games/leagues/teams;team_keys=%s%s'        
    );
    
    // User Data URIs for specific request types
    const USERS_URI = array(
        'games'      =>  '/users;use_login=1/games%s',
        'leagues'    =>  '/users;use_login=1/games%s/leagues/standings',
        'teams'      =>  '/users;use_login=1/games%s/leagues/teams/standings'         
    );   
    
    /**
     * AbstractProvider used to make requests
     * @var YahooFantasyProvider
     */
    protected $provider;
    
    /**
     * AccessToken used to make requests
     * @var AccessToken
     */
    protected $token; 
    
    /**
     * On Refresh callback.
     * @var Callable
     */
    protected $onRefresh;
    
    /**
     * Creates a new GameService.  The GameService will automatically attempt
     * to refresh the token if it's expired. 
     * @param YahooFantasyProvider $provider
     * @param AccessToken $token
     * @param mixed $onRefresh
     */
    public function __construct($provider, $token, $onRefresh = null) {
        $this->provider = $provider;
        $this->token = $token; 
        $this->onRefresh = $onRefresh;
        
        if ($token->hasExpired()) {
            $this->refreshToken();
        }
    }
    
    /**
     * Convert an object into a JSON array
     * @param Object $obj
     * @return array
     */
    private static function jsonToArray($obj) {
        return json_decode(json_encode($obj), true);
    }
    
    /**
     * Makes an authenticated request and returns the result from the Yahoo!
     * API.  The XML returned has namespaces that need to be registered if 
     * XPath will be used on the XML.
     * @param string $method
     * @param string $url
     * @return SimpleXMLElement
     * 
     * @link http://php.net/manual/en/simplexmlelement.xpath.php
     */
    public function makeApiRequest($method, $url) {
        $request = $this->provider->getAuthenticatedRequest(
                $method, 
                $url,
                $this->token);
        $response = $this->provider->getParsedResponse($request);        
        $xml = new \SimpleXMLElement($response);               
        return YahooFantasyService::addXmlNamespace($xml);
    }<commit_msg>Updated Service to return JSON when requested.<commit_after><?php

namespace Kenjdavidson\OAuth2\YahooFantasySports\Provider\Service;

use Kenjdavidson\OAuth2\YahooFantasySports\Provider\YahooFantasyProvider;
use Kenjdavidson\OAuth2\YahooFantasySports\Provider\Resource\GameResource;
use Kenjdavidson\OAuth2\YahooFantasySports\Provider\Resource\LeagueResource;
use Kenjdavidson\OAuth2\YahooFantasySports\Provider\Resource\TeamResource;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;

/**
 * Standard method used within Yahoo Resource Services.  This service makes
 * calls to the API (which respond in XML) and convert the message to JSON.  
 * This is annoying, but the YQL service doesn't like the authorized request
 * and will not respond.
 * 
 * Regular API:
 * 
 * Games:
 * @link https://developer.yahoo.com/fantasysports/guide/#game-resource
 * @link https://developer.yahoo.com/fantasysports/guide/#games-collection
 * 
 * The games collection has the available sub-resource: leagues
 * 
 * Leagues:
 * @link https://developer.yahoo.com/fantasysports/guide/#leagues-collection
 * @link https://developer.yahoo.com/fantasysports/guide/#league-resource
 * 
 * The leagues collection has the available sub-resources: settings, standings,
 * scoreboard and teams
 *
 * Teams:
 * @link https://developer.yahoo.com/fantasysports/guide/#teams-collection
 * @link https://developer.yahoo.com/fantasysports/guide/#team-resource
 * 
 * The teams collection has the available sub-resources: matchups;weeks=, 
 * stats;type=[season|date], roster
 * 
 * Roster:
 * @link https://developer.yahoo.com/fantasysports/guide/#roster-collection
 * @link https://developer.yahoo.com/fantasysports/guide/#roster-resource
 * 
 * The roster collection has the available sub-resources: player
 * 
 * Player:
 * @link https://developer.yahoo.com/fantasysports/guide/#roster-collection
 * @link https://developer.yahoo.com/fantasysports/guide/#roster-resource
 * 
 * The player collection has the available sub-resourceS: stats
 * 
 *  
 * 
 * @author <NAME>
 */
class YahooFantasyService {
    
    // API base URI
    const API_BASE = 'https://fantasysports.yahooapis.com/fantasy/v2';
    
    // Collection URIs for specific request types
    const COLLECTION_URI = array(
        'games'      =>  '/users%s/games;game_keys=%s',
        'leagues'    =>  '/users%s/games/leagues;league_keys=%s%s',
        'teams'      =>  '/users%s/games/leagues/teams;team_keys=%s%s'        
    );
    
    // User Data URIs for specific request types
    const USERS_URI = array(
        'games'      =>  '/users;use_login=1/games%s',
        'leagues'    =>  '/users;use_login=1/games%s/leagues/standings',
        'teams'      =>  '/users;use_login=1/games%s/leagues/teams/standings'         
    );   
    
    /**
     * AbstractProvider used to make requests
     * @var YahooFantasyProvider
     */
    protected $provider;
    
    /**
     * AccessToken used to make requests
     * @var AccessToken
     */
    protected $token; 
    
    /**
     * On Refresh callback.
     * @var Callable
     */
    protected $onRefresh;
    
    /**
     * Creates a new GameService.  The GameService will automatically attempt
     * to refresh the token if it's expired. 
     * @param YahooFantasyProvider $provider
     * @param AccessToken $token
     * @param mixed $onRefresh
     */
    public function __construct($provider, $token, $onRefresh = null) {
        $this->provider = $provider;
        $this->token = $token; 
        $this->onRefresh = $onRefresh;
        
        if ($token->hasExpired()) {
            $this->refreshToken();
        }
    }
    
    /**
     * Convert an object into a JSON array
     * @param Object $obj
     * @return array
     */
    private static function jsonToArray($obj) {
        return json_decode(json_encode($obj), true);
    }
    
    /**
     * Makes an authenticated request and returns the result from the Yahoo!
     * API.  The XML returned has namespaces that need to be registered if 
     * XPath will be used on the XML.
     * @param string $method
     * @param string $url
     * @param boolean $json returns the XML converted to JSON
     * @return SimpleXMLElement
     * 
     * @link http://php.net/manual/en/simplexmlelement.xpath.php
     */
    public function makeApiRequest($method, $url, $json = false) {
        $request = $this->provider->getAuthenticatedRequest(
                $method, 
                $url,
                $this->token);
        $response = $this->provider->getParsedResponse($request);        
        $xml = new \SimpleXMLElement($response);  
        
        if ($json) {
            return json_encode($xml);
        }
        return YahooFantasyService::addXmlNamespace($xml);
    }
{'fraction_non_alphanumeric': 0.11622737376945062, 'fraction_numerical': 0.002328781623795914, 'mean_word_length': 2.7671451355661882, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 22, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, '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': '2263959', 'n_tokens_mistral': 2988, 'n_tokens_neox': 2793, 'n_words': 639}