Skip to content

Commit

Permalink
Handle HTI teams
Browse files Browse the repository at this point in the history
  • Loading branch information
jetwitaussi committed Jan 14, 2017
1 parent 2298a61 commit 83432e5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
10 changes: 8 additions & 2 deletions PHT/Config/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ class Team
* Set to true to get the user's primary team
* @var boolean
*/
public $primary = null;
public $primary = false;

/**
* Set to true to get the user's secondary team
* @var boolean
*/
public $secondary = null;
public $secondary = false;

/**
* Set to true to get the user's HTI team, not used for youth team
* @var boolean
*/
public $international = false;

/**
* Set to id to get user's team, combine with primary or secondary
Expand Down
40 changes: 30 additions & 10 deletions PHT/PHT.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ public function getSeniorTeam(Config\Team $team = null)
throw new Exception\InvalidArgumentException('Parameter $team should be instanceof \PHT\Config\Team');
}

if ($team->primary !== null) {
if ($team->primary === true) {
return $this->getPrimarySeniorTeam($team->userId);
} elseif ($team->secondary !== null) {
} elseif ($team->secondary === true) {
return $this->getSecondarySeniorTeam($team->userId);
} elseif ($team->international === true) {
return $this->getInternationalSeniorTeam($team->userId);
} else {
return $this->findSeniorTeam($team->id, $team->userId);
}
Expand Down Expand Up @@ -112,7 +114,7 @@ protected function findSeniorTeam($id = null, $userId = null)
*/
protected function findYouthTeam($id = null, $userId = null)
{
if (!is_int($id)) {
if ($id === null) {
$id = $this->findSeniorTeam(null, $userId)->getYouthTeamId();
}
return Wrapper\Team\Youth::team($id);
Expand All @@ -124,7 +126,7 @@ protected function findYouthTeam($id = null, $userId = null)
*/
protected function getPrimarySeniorTeam($userId = null)
{
return $this->getSpecificSeniorTeam('false', $userId);
return $this->getSpecificSeniorTeam(Xml\Team\Senior::PRIMARY, $userId);
}

/**
Expand All @@ -133,28 +135,46 @@ protected function getPrimarySeniorTeam($userId = null)
*/
protected function getSecondarySeniorTeam($userId = null)
{
return $this->getSpecificSeniorTeam('true', $userId);
return $this->getSpecificSeniorTeam(Xml\Team\Senior::SECONDARY, $userId);
}

/**
* @param string $boolString set 'false' for primary team or 'true' for secondary team
* @param integer $userId
* @return \PHT\Xml\Team\Senior
*/
protected function getSpecificSeniorTeam($boolString, $userId = null)
protected function getInternationalSeniorTeam($userId = null)
{
return $this->getSpecificSeniorTeam(Xml\Team\Senior::INTERNATIONAL, $userId);
}

/**
* @param integer $type
* @param integer $userId
* @return \PHT\Xml\Team\Senior
*/
protected function getSpecificSeniorTeam($type, $userId = null)
{
$xml = Wrapper\Team\Senior::team(null, $userId);

$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->loadXml($xml);
$doc->loadXml($xml->getXml(false));

$teams = $doc->getElementsByTagName('Team');
for ($t = 0; $t < $teams->length; $t++) {
$txml = new \DOMDocument('1.0', 'UTF-8');
$txml->appendChild($txml->importNode($teams->item($t), true));
if (strtolower($txml->getElementsByTagName('IsPrimaryClub')->item(0)->nodeValue) == $boolString) {
$doc->getElementsByTagName('Teams')->item(0)->removeChild($teams->item($t));
$isHti = $txml->getElementsByTagName('LeagueID')->item(0)->nodeValue == 1000;
$isPrimary = strtolower($txml->getElementsByTagName('IsPrimaryClub')->item(0)->nodeValue) == 'true';
if ($type == Xml\Team\Senior::PRIMARY && $isPrimary) {
continue;
}
if ($type == Xml\Team\Senior::SECONDARY && !$isPrimary && !$isHti) {
continue;
}
if ($type == Xml\Team\Senior::INTERNATIONAL && $isHti) {
continue;
}
$doc->getElementsByTagName('Teams')->item(0)->removeChild($teams->item($t));
}
if ($doc->getElementsByTagName('Team')->length) {
return new Xml\Team\Senior($doc->saveXML());
Expand Down
4 changes: 4 additions & 0 deletions PHT/Xml/Team/Senior.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

class Senior extends Xml\HTSupporter
{
const PRIMARY = 1;
const SECONDARY = 2;
const INTERNATIONAL = 3;

/**
*
* @param string $xml
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ try {
\PHT\Cache\Driver::getInstance()->clear('club');
// request xml again:
echo $HT->getClub()->getTeamName();
// get HTI team
$teamConf = new \PHT\Config\Team();
$teamConf->international = true;
echo $HT->getSeniorTeam($teamConf)->getName();
} catch(\PHT\Exception\ChppException $e) {
// chpp request returns xml content
Expand Down

0 comments on commit 83432e5

Please sign in to comment.