Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
natocTo committed Jun 11, 2016
0 parents commit b94f3d2
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
NatocTo/FootballData
======

Nette basic integration of football-data.org's API.

Installation
------------

The best way to install NatocTo/FootballData is using the [Composer](http://getcomposer.org/)

```sh
$ composer require natocto/football-data
```

and you can enable the extension using your neon config

```yml
extensions:
footballData: NatocTo\FootballData\DI\FootballDataExtension
```

if you want increase your rate limit from 50 requests per day to 50 requests per minute you can get your free token from [football-data.org](http://football-data.org/register)

```yml
footballData:
authToken: 'YOUR-TOKEN'
```


Documentation
------------

There is no documentation. Just look to source code if you are interested.
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "natocto/football-data",
"description": "Nette integration of football-data.org's API.",
"keywords": ["Football fixtures, Football data, Football results"],
"license": "MIT",
"authors": [
{
"name": "Jakub Kotek",
"homepage": "http://jakubkotek.cz",
"email": "info@jakubkotek.cz"
}
],
"require": {
"nette/di": "^2.3.0",
"guzzlehttp/guzzle": "^6.2"
},
"autoload": {
"psr-4": {
"NatocTo\\FootballData\\": "src/"
}
}
}
27 changes: 27 additions & 0 deletions src/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace NatocTo\FootballData;

use GuzzleHttp\Client;

class Content {

private $authToken;

private $client;

public function __construct($authToken) {

$this->authToken = $authToken;

$this->client = new Client(['base_uri' => 'http://api.football-data.org/v1/',]);
}


public function fetch($resource)
{
$response = $this->client->get($resource, ['headers' => ['X-Auth-Token' => $this->authToken]]);

return json_decode($response->getBody());
}
}
27 changes: 27 additions & 0 deletions src/DI/FootballDataExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace NatocTo\FootballData\DI;

use Nette;
use NatocTo;
use Exception;

class FootballDataExtension extends Nette\DI\CompilerExtension
{
public $defaults = [
'authToken' => ''
];

public function loadConfiguration()
{
$config = $this->getConfig($this->defaults);

$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('content'))
->setClass(NatocTo\FootballData\Content::class, [$config['authToken']]);

$builder->addDefinition($this->prefix('default'))
->setClass(NatocTo\FootballData\FootballData::class, [$this->prefix('@content')]);
}
}
104 changes: 104 additions & 0 deletions src/FootballData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace NatocTo\FootballData;

use NatocTo\FootballData\Model;

class FootballData {

private $content;

public function __construct(Content $content)
{
$this->content = $content;
}


/**
* Function returns a specific soccer season identified by an id.
*
* @param int $id
* @return NatocTo\FootballData\Model\SoccerSeason
*/
public function getSeason($id) {

$resource = 'soccerseasons/' . $id;

$response = $this->content->fetch($resource);

return new Model\Soccerseason($this->content, $response);
}


/**
* Function returns one unique team identified by a given id.
*
* @param int $id
* @return NatocTo\FootballData\Model\Team
*/
public function getTeam($id) {

$resource = 'teams/' . $id;

$response = $this->content->fetch($resource);

return new Model\Team($this->content, $response);
}


/**
* Function returns a specific soccer seasons identified by an year.
*
* @param int $id
* @return NatocTo\FootballData\Model\SoccerSeason
*/
public function getSeasonsByYear($year) {

$resource = 'soccerseasons/?season=' . $year;

return $this->content->fetch($resource);
}


/**
* Function returns all available fixtures for a given date range.
*
* @param DateString 'Y-m-d' $start
* @param DateString 'Y-m-d' $end
* @return array - list of fixtures
*/
public function getFixtures($start, $end) {

$resource = 'fixtures/?timeFrameStart=' . $start . '&timeFrameEnd=' . $end;

return $this->content->fetch($resource);
}


/**
* Function returns one unique fixture identified by a given id.
*
* @param int $id
* @return stdObject
*/
public function getFixture($id) {

$resource = 'fixtures/' . $id;

return $this->content->fetch($resource);
}


/**
* Function returns all teams matching a given keyword.
*
* @param string $keyword
* @return array - list of teams
*/
public function searchTeam($keyword) {

$resource = 'teams/?name=' . urlencode($keyword);

return $this->content->fetch($resource);
}
}
15 changes: 15 additions & 0 deletions src/model/BaseModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace NatocTo\FootballData\Model;

use NatocTo\FootballData\Content;

abstract class BaseModel {

protected $content;

public function __construct(Content $content) {

$this->content = $content;
}
}
74 changes: 74 additions & 0 deletions src/model/Soccerseason.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace NatocTo\FootballData\Model;

use NatocTo\FootballData\Content;

class Soccerseason extends BaseModel {

private $payload;

public function __construct(Content $content, $payload)
{
parent::__construct($content);

$this->payload = $payload;
}


/**
* Function returns all fixtures for the instantiated soccerseason.
*
* @return array of fixture objects
*/
public function getFixtures() {

$uri = $this->payload->_links->fixtures->href;

return $this->content->fetch($uri);
}


/**
* Function returns all fixtures for a given matchday.
*
* @param type $matchday
* @return array of fixture objects
*/
public function getFixturesByMatchday($matchday = 1) {

$uri = $this->payload->_links->fixtures->href . '/?matchday=' . $matchday;

$response = $this->content->fetch($uri);

return $response->fixtures;
}


/**
* Function returns all teams participating in the instantiated soccerseason.
*
* @return array of team objects
*/
public function getTeams() {

$uri = $this->payload->_links->teams->href;

$response = $this->content->fetch($uri);

return $response->teams;
}


/**
* Function returns the current league table for the instantiated soccerseason.
*
* @return object leagueTable
*/
public function getTable() {

$uri = $this->payload->_links->leagueTable->href;

return $this->content->fetch($uri);
}
}
47 changes: 47 additions & 0 deletions src/model/Team.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace NatocTo\FootballData\Model;

use NatocTo\FootballData\Content;

class Team extends BaseModel {

private $payload;

public function __construct(Content $content, $payload)
{
parent::__construct($content);

$this->payload = $payload;
}


/**
* Function returns all fixtures for the team for this season.
*
* @param string $venue
* @param string $timeFrame
* @return array of stdObjects representing fixtures
*/
public function getFixtures($venue = "", $timeFrame = "") {

$uri = $this->payload->_links->fixtures->href . '/?venue=' . $venue . '&timeFrame=' . $timeFrame;

return $this->content->fetch($uri);
}


/**
* Function returns all players of the team
*
* @return array of fixture objects
*/
public function getPlayers() {

$uri = $this->payload->_links->players->href;

$response = $this->content->fetch($uri);

return $response->players;
}
}

0 comments on commit b94f3d2

Please sign in to comment.