Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dev: Fetch global variables from Cint at plugin activation
- Loading branch information
1 parent
be9515e
commit aba1bd1
Showing
2 changed files
with
143 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
require_once(__DIR__ . "/CintLinkAPI.php"); | ||
|
||
/** | ||
* Helper class to parse links in the | ||
* Cint XML. | ||
* | ||
* @since 2016-08-09 | ||
* @author Olle Härstedt | ||
*/ | ||
final class CintXml | ||
{ | ||
/** | ||
* Raw XML | ||
* @var string | ||
*/ | ||
private $raw; | ||
|
||
/** | ||
* @var SimpleXmlElement | ||
*/ | ||
private $xml; | ||
|
||
/** | ||
* @param string $apiKey | ||
*/ | ||
public function __construct($apiKey) | ||
{ | ||
$url = 'https://' . $apiKey . '.cds.cintworks.net/'; | ||
$curl = new Curl(); | ||
$this->raw = $curl->get($url); | ||
$this->xml = new SimpleXmlElement($this->raw); | ||
} | ||
|
||
/** | ||
* Get global variables | ||
* @return SimpleXmlElement|null | ||
*/ | ||
public function getGlobalVariables() | ||
{ | ||
$url = $this->getHrefFromRel('global-variables'); | ||
if ($url !== false) | ||
{ | ||
$curl = new Curl(); | ||
$raw = $curl->get($url); | ||
return $raw; | ||
} | ||
else | ||
{ | ||
throw new Exception('Found no href from rel'); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Get link from rel name | ||
* @param string $relName Like 'global-variables' or 'quote' | ||
* @return string|false | ||
*/ | ||
private function getHrefFromRel($relName) | ||
{ | ||
$rel = 'http://cds.cint.com/rel/' . $relName; | ||
foreach ($this->xml->children() as $child) | ||
{ | ||
if ($child['rel'] == $rel) | ||
{ | ||
return $child['href']; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |