Skip to content

Commit

Permalink
Add team stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Abrey committed Nov 23, 2019
1 parent 3927fe8 commit 2d83fd5
Show file tree
Hide file tree
Showing 8 changed files with 473 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ $playerStats = $stats->getPlayer(4241463);

The return from `getPlayer()` will be a `Player` object, which includes their ID and a `seasons` property which is an array of `PlayerSeason` objects, containing the stats for the player that season.

### Team Stats

Fetching stats for teams is similar.

The return from `getTeam()` will be a `Team` object, which includes their ID and a `seasons` property which is an array of `TeamSeason` objects.

```php
$stats = new DanAbrey\ESPNCollegeFootballStats\ESPNCollegeFootballStats();
$teamStats = $stats->getTeam(333);
```

By default, it will return the last four seasons' worth of data. You can change this by passing an array of seasons to `$stats->setSeasons()`.

```php
$stats = new DanAbrey\ESPNCollegeFootballStats\ESPNCollegeFootballStats();
$stats->setTeamSeasons([2015, 2016]);
$teamStats = $stats->getTeam(333);
```

## Disclaimer

This package is intended as a proof of concept experiment, I highly advise against using the ESPN API (which isn't documented) for anything other than hobby personal use.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "danabrey/espn-college-football-stats",
"description": "PHP package for reading data from ESPN's college football JSON API",
"type": "library",
"version": "1.0.0",
"version": "1.1.0",
"license": "MIT",
"authors": [
{
Expand Down
36 changes: 36 additions & 0 deletions src/ESPNCollegeFootballStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
namespace DanAbrey\ESPNCollegeFootballStats;

use DanAbrey\ESPNCollegeFootballStats\Extractor\PlayerStatsExtractor;
use DanAbrey\ESPNCollegeFootballStats\Extractor\TeamStatsExtractor;
use DanAbrey\ESPNNCAAStatsScraper\Exception\PlayerNotFoundException;

class ESPNCollegeFootballStats
{
private $seasons = [];

/**
* @param string $id
* @return Player
Expand All @@ -31,4 +34,37 @@ public function getPlayer(string $id): Player

return $player;
}

public function getTeam(string $id): Team
{
if (count($this->seasons) < 1) {
throw new \Exception('Must set seasons parameter');
}

$team = new Team();
$team->setId($id);

$seasons = [];

foreach ($this->seasons as $season) {
$json = file_get_contents(
sprintf(
'https://site.web.api.espn.com/apis/common/v3/sports/football/college-football/athletes/%s/stats?region=gb&lang=en&contentorigin=espn&season=%s',
$id,
$season
)
);

$seasons[] = (new TeamStatsExtractor($json))->extractSeasonData();
}

$team->setSeasons($seasons);

return $team;
}

public function setSeasons(array $seasons)
{
$this->seasons = $seasons;
}
}
71 changes: 71 additions & 0 deletions src/Extractor/TeamStatsExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php


namespace DanAbrey\ESPNCollegeFootballStats\Extractor;

use DanAbrey\ESPNCollegeFootballStats\PlayerSeason;
use DanAbrey\ESPNCollegeFootballStats\TeamSeason;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class TeamStatsExtractor
{
/**
* @var array
*/
private $data;

private const MAP = [
'Rushing' => [
'rushingAttempts',
'rushingYards',
'rushingTouchdowns',
],
'Receiving' => [
'receptions',
'receivingYards',
'receivingTouchdowns',
],
'Passing' => [
'passingTouchdowns',
'passingYards',
'passingAttempts',
'completions',
'interceptions',
'sacks',
]
];

public function __construct(string $json)
{
$this->data = json_decode($json, true);
}

public function extractSeasonData()
{
$teamSeason = [];
$teamSeason['year'] = $this->data['requestedSeason']['year'];

foreach ($this->data['teamTotals'] as $category) {
$categoryName = $category['displayName'];

if (!array_key_exists($categoryName, self::MAP)) {
continue;
}

foreach ($category['stats'] as $stat) {
if (!in_array($stat['name'], self::MAP[$categoryName])) {
continue;
}

$teamSeason[$stat['name']] = str_replace(',', '', $stat['value']);
}
}

$normalizers = [new ArrayDenormalizer(), new ObjectNormalizer()];
$serializer = new Serializer($normalizers);

return $serializer->denormalize($teamSeason, TeamSeason::class);
}
}
47 changes: 47 additions & 0 deletions src/Team.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace DanAbrey\ESPNCollegeFootballStats;

class Team
{
/**
* @var string
*/
private $id;

/**
* @var array
*/
private $seasons = [];

/**
* @return string
*/
public function getId(): string
{
return $this->id;
}

/**
* @param string $id
*/
public function setId(string $id): void
{
$this->id = $id;
}

/**
* @return array
*/
public function getSeasons(): array
{
return $this->seasons;
}

/**
* @param array $seasons
*/
public function setSeasons(array $seasons): void
{
$this->seasons = $seasons;
}
}
Loading

0 comments on commit 2d83fd5

Please sign in to comment.