Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improvements #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Sportlink Club.Dataservice
This package contains a PHP wrapper around the Sportlink Club.Dataservice API. It aims to make working with the API a breeze.

#### API
For a list of available atricles see: `https://dexels.github.io/navajofeeds-json-parser/`.

#### Installation

Install the package using composer:
Expand Down
14 changes: 14 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function __construct($api_key = '', HttpClientInterface $client = null)
{
$this->api_key = $api_key;
$this->client = $client ?: new HttpClient();

$this->setTimeZone();
}

/**
Expand All @@ -56,6 +58,18 @@ public function setApiKey($api_key)
return $this;
}

/**
* @param string $timezone
*
* @sse https://www.php.net/manual/en/timezones.php
*/
public function setTimeZone($timezone = 'Europe/Amsterdam')
{
date_default_timezone_set($timezone);

return $this;
}

/**
* Make a request to the API
*
Expand Down
71 changes: 71 additions & 0 deletions src/Birthday.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace PendoNL\ClubDataservice;

/**
* @property-read string $verjaardag The member birthday (format: 1 Jan).
* @property-read string $volledigenaam The member full name.
*
* @see https://dexels.github.io/navajofeeds-json-parser/article/?verjaardagen
*/
class Birthday extends AbstractItem
{
const ARTICLE = 'verjaardagen';

const SHIELDED = 'Afgeschermd';
const UNKNOWN = 'Onbekend';

/**
* The member firstname.
*
* @return string
*/
public function getFirstName()
{
return !$this->isShielded()
? explode(' ', $this->volledigenaam, 2)[0]
: static::UNKNOWN;
}

/**
* The member lastname.
*
* @return string
*/
public function getLastName()
{
return !$this->isShielded()
? explode(' ', $this->volledigenaam, 2)[1]
: static::UNKNOWN;
}

/**
* Get member full name.
*
* @return string
*/
public function getFullName()
{
return $this->volledigenaam;
}

/**
* Get a date time object for birthday value.
*
* @return \DateTimeInterface
*/
public function getDate()
{
return \DateTime::createFromFormat(
'j M H:i:s', $this->verjaardag . ' 00:00:00'
);
}

/**
* @return bool
*/
public function isShielded()
{
return $this->volledigenaam === static::SHIELDED;
}
}
165 changes: 148 additions & 17 deletions src/Club.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,37 @@

namespace PendoNL\ClubDataservice;

use PendoNL\ClubDataservice\Exception\InvalidResponseException;

/**
* Club class.
*
* @see https://dexels.github.io/navajofeeds-json-parser/
*/
class Club extends AbstractItem
{
/** @var Team[]|array */
public $teams = [];

/** @var array{id: int, info: TeamInfo} */
public $teamInfo = [];

public $afgelastingen = [];

/** @var Committee[]|array */
public $committees = [];

/** @var Birthday[]|array */
public $birthdays = [];

/**
* Return all teams (and map competitions).
*
* @param array $arguments
* @return array
*
* @return Team[]|array
*
* @throws InvalidResponseException
*/
public function getTeams($arguments = [])
{
Expand Down Expand Up @@ -40,43 +61,82 @@ public function getTeams($arguments = [])
}

/**
* Return all competitions.
* Return a single team.
*
* @return array
* @param string $code
*
* @return Team|null
*
* @throws InvalidResponseException
*/
public function getCompetitions()
public function getTeam($code)
{
if(count($this->teams) == 0) {
$this->getTeams();
}

return $this->competitions;
if (isset($this->teams[$code])) {
return $this->teams[$code];
}

return null;
}

/**
* Return a single team.
* Get team info.
*
* @param string $code
* @return Team|null
* @param int $teamCode The team code.
* @param int $localTeamCode The local team code.
*
* @return TeamInfo|null
*/
public function getTeam($code)
public function getTeamInfo($teamCode, $localTeamCode = -1)
{
if(count($this->teams) == 0) {
$this->getTeams();
// Set cache key.
$key = $teamCode . '-' . $localTeamCode;

if (isset($this->teamInfo[$key])) {
return $this->teamInfo[$key];
}

if (isset($this->teams[$code])) {
return $this->teams[$code];
try {
$response = $this->api->request(TeamInfo::ARTICLE, [
'teamcode' => $teamCode,
'lokaleteamcode' => $localTeamCode,
]);

$this->teamInfo[$key] = new TeamInfo($this->api, $response['team']);

return $this->teamInfo[$key];
} catch (InvalidResponseException $ex) {
return null;
}
}

return null;
/**
* Return all competitions.
*
* @return Competition[]|array
*
* @throws InvalidResponseException
*/
public function getCompetitions()
{
if(count($this->teams) == 0) {
$this->getTeams();
}

return $this->competitions;
}

/**
* Return a single competition.
*
* @param string $code
* @param string $code
*
* @return Team|null
*
* @throws InvalidResponseException
*/
public function getCompetition($code)
{
Expand All @@ -93,7 +153,10 @@ public function getCompetition($code)
* Afgelastingen.
*
* @param array $arguments
*
* @return array
*
* @throws InvalidResponseException
*/
public function afgelastingen($arguments = [])
{
Expand All @@ -104,11 +167,79 @@ public function afgelastingen($arguments = [])
$response = $this->api->request('afgelastingen', $arguments);

foreach($response as $item){
$afgelasting = new Match($this->api, $item);
$this->afgelastingen[] = $afgelasting;
$this->afgelastingen[] = new Game($this->api, $item);
}

return $this->afgelastingen;
}

/**
* Return all committees.
*
* @param array $arguments
*
* @return Committee[]|array
*
* @throws InvalidResponseException
*/
public function getCommittees($arguments = [])
{
if ($this->committees) {
return $this->committees;
}

$response = $this->api->request(Committee::ARTICLE, $arguments);

// Loop committees.
foreach ($response as $item) {
$committee = new Committee($this->api, $item);

if(!array_key_exists($committee->commissiecode, $this->committees)) {
$this->committees[$committee->commissiecode] = $committee;
}
}

return $this->committees;
}

/**
* Get Committee.
*
* @param int $commissiecode The unique commission code.
*
* @return Committee|null
*
* @throws InvalidResponseException
*/
public function getCommittee($commissiecode)
{
$committees = $this->getCommittees();

if (isset($committees[$commissiecode])) {
return $committees[$commissiecode];
}

return null;
}

/**
* Get birthdays.
*
* @return Birthday[]|array
*/
public function getBirthdays($arguments = [])
{
if ($this->birthdays) {
return $this->birthdays;
}

$response = $this->api->request(Birthday::ARTICLE, $arguments);

// Loop birthdays.
foreach ($response as $item) {
$this->birthdays[] = new Birthday($this->api, $item);
}

return $this->birthdays;
}
}
45 changes: 45 additions & 0 deletions src/Committee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace PendoNL\ClubDataservice;

use PendoNL\ClubDataservice\Exception\InvalidResponseException;

/**
* @property-read int|string $commissiecode The commission code id.
*
* @see https://dexels.github.io/navajofeeds-json-parser/article/?commissies
*/
class Committee extends AbstractItem
{
const ARTICLE = 'commissies';

/** @var CommitteeMember[]|array */
private $members = [];

/**
* Get committee members.
*
* @param bool $showPicture Retrieve a picture of this member.
*
* @return CommitteeMember[]|array
*
* @throws InvalidResponseException
*/
public function getMembers($showPicture = false)
{
if (count($this->members) != 0) {
return $this->members;
}

$membersData = $this->api->request(CommitteeMember::ARTICLE, [
'commissiecode' => (int)$this->commissiecode,
'toonlidfoto' => $showPicture,
]);

foreach ($membersData as $memberData) {
$this->members[] = new CommitteeMember($this->api, $memberData);
}

return $this->members;
}
}
Loading