Skip to content

Commit 5f15f54

Browse files
committed
Added dot stat client. Added get dataflows method.
1 parent 3dcbdd3 commit 5f15f54

File tree

7 files changed

+526
-0
lines changed

7 files changed

+526
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace Sdmx\api\client\rest;
4+
5+
6+
use Sdmx\api\client\http\HttpClient;
7+
use Sdmx\api\client\rest\query\SdmxQueryBuilder;
8+
use Sdmx\api\client\SdmxClient;
9+
use Sdmx\api\entities\Dataflow;
10+
use Sdmx\api\entities\DataflowStructure;
11+
use Sdmx\api\entities\DsdIdentifier;
12+
use Sdmx\api\entities\PortableTimeSeries;
13+
use Sdmx\api\parser\DataStructureParser;
14+
15+
class DotStatClient implements SdmxClient
16+
{
17+
18+
/**
19+
* @var SdmxQueryBuilder $queryBuilder
20+
*/
21+
private $queryBuilder;
22+
23+
/**
24+
* @var HttpClient $httpClient
25+
*/
26+
private $httpClient;
27+
28+
/**
29+
* @var DataStructureParser $dataStructureParser
30+
*/
31+
private $dataStructureParser;
32+
33+
/**
34+
* RestSdmxV20Client constructor.
35+
* @param SdmxQueryBuilder $queryBuilder
36+
* @param HttpClient $httpClient
37+
*/
38+
public function __construct(SdmxQueryBuilder $queryBuilder, HttpClient $httpClient, DataStructureParser $dataStructureParser)
39+
{
40+
$this->queryBuilder = $queryBuilder;
41+
$this->httpClient = $httpClient;
42+
$this->dataStructureParser = $dataStructureParser;
43+
}
44+
45+
46+
/**
47+
* Gets all dataflows.
48+
* @return Dataflow[]
49+
*/
50+
public function getDataflows()
51+
{
52+
$url = $this->queryBuilder->getDsdQuery('all', 'all', 'latest', false);
53+
$data = $this->httpClient->get($url);
54+
$result = [];
55+
56+
$structures = $this->dataStructureParser->parse($data);
57+
foreach ($structures as $structure){
58+
$result[] = $this->mapStructureToDataflow($structure);
59+
}
60+
61+
return $result;
62+
}
63+
64+
/**
65+
* Gets the dataflow information for a given dataflow.
66+
* @param string $dataflow
67+
* @param string $agency
68+
* @param string $version
69+
* @return Dataflow
70+
*/
71+
public function getDataflow($dataflow, $agency, $version)
72+
{
73+
// TODO: Implement getDataflow() method.
74+
}
75+
76+
/**
77+
* Gets the structure for a given dataflow.
78+
* @param DsdIdentifier $dsd
79+
* @param bool $full if true, for 2.1 providers it retrieves the full dsd, with all the codelists.
80+
* @return DataflowStructure
81+
*/
82+
public function getDataflowStructure(DsdIdentifier $dsd, $full = false)
83+
{
84+
// TODO: Implement getDataflowStructure() method.
85+
}
86+
87+
/**
88+
* Gets all the codes from this provider for a given codelist.
89+
* @param string $codelist
90+
* @param string $agency
91+
* @param string $version
92+
* @return string[]
93+
*/
94+
public function getCodes($codelist, $agency, $version)
95+
{
96+
// TODO: Implement getCodes() method.
97+
}
98+
99+
/**
100+
* @param Dataflow $dataflow The dataflow of the time series to be gathered
101+
* @param DataflowStructure $dsd The structure of the dataflow of the time series to be gathered
102+
* @param string $resource The id of the time series
103+
* @param array $options
104+
* ```php
105+
* $options = array(
106+
* 'startPeriod' => 'string', //Start time of the observations to be gathered
107+
* 'endPeriod' => 'string', //End time of the observations to be gathered
108+
* 'seriesKeysOnly' => 'boolean', //Flag for disabling data and attributes processing (usually for getting the only dataflow contents)
109+
* 'lastNObservations' => 'integer' //The last 'n' observations to return for each matched series.
110+
* )
111+
* ```
112+
* @return PortableTimeSeries[]
113+
*/
114+
public function getTimeSeries(Dataflow $dataflow, DataflowStructure $dsd, $resource, array $options = array())
115+
{
116+
// TODO: Implement getTimeSeries() method.
117+
}
118+
119+
/**
120+
* @param string $dataflow The dataflow of the time series to be gathered
121+
* @param string $agency The agency of the dataflow of the time series to be gathered
122+
* @param string $version The version of the dataflow of the time series to be gathered
123+
* @param string $resource The id of the time series
124+
* @param array $options
125+
* ```php
126+
* $options = array(
127+
* 'startPeriod' => 'string', //Start time of the observations to be gathered
128+
* 'startPeriod' => 'string', //End time of the observations to be gathered
129+
* 'seriesKeysOnly' => 'boolean', //Flag for disabling data and attributes processing (usually for getting the only dataflow contents)
130+
* 'lastNObservations' => 'integer' //The last 'n' observations to return for each matched series.
131+
* )
132+
* ```
133+
* @return PortableTimeSeries[]
134+
*/
135+
public function getTimeSeries2($dataflow, $agency, $version, $resource, array $options = array())
136+
{
137+
// TODO: Implement getTimeSeries2() method.
138+
}
139+
140+
/**
141+
* @param $structure
142+
* @return Dataflow
143+
*/
144+
protected function mapStructureToDataflow($structure)
145+
{
146+
$dataflow = new Dataflow();
147+
$dataflow->setAgency($structure->getAgency());
148+
$dataflow->setId($structure->getId());
149+
$dataflow->setVersion($structure->getVersion());
150+
$dataflow->setName($structure->getName());
151+
$dsd = new DsdIdentifier($structure->getId(), $structure->getAgency(), $structure->getVersion());
152+
$dataflow->setDsdIdentifier($dsd);
153+
return $dataflow;
154+
}
155+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Sdmx\api\client\rest\query;
4+
5+
6+
use Sdmx\api\entities\Dataflow;
7+
use Sdmx\util\StringUtils;
8+
9+
class DotStatQueryBuilder implements SdmxQueryBuilder
10+
{
11+
12+
/**
13+
* @var string $baseUrl
14+
*/
15+
private $baseUrl;
16+
17+
/**
18+
* DotStatQueryBuilder constructor.
19+
* @param string $baseUrl
20+
*/
21+
public function __construct($baseUrl)
22+
{
23+
$this->baseUrl = $baseUrl;
24+
}
25+
26+
27+
/**
28+
* @param string $agency
29+
* @param string $dataflow
30+
* @param string $version
31+
* @return string
32+
*/
33+
public function getDataflowQuery($agency, $dataflow, $version)
34+
{
35+
return $this->getDsdQuery($dataflow, $agency, $version, false);
36+
}
37+
38+
/**
39+
* @param string $id
40+
* @param string $agency
41+
* @param string $version
42+
* @param boolean $full
43+
* @return string
44+
*/
45+
public function getDsdQuery($id, $agency, $version, $full)
46+
{
47+
return StringUtils::joinArrayElements([$this->baseUrl, 'GetDataStructure', $id], '/');
48+
}
49+
50+
/**
51+
* @param string $codelist
52+
* @param string $agency
53+
* @param string $version
54+
* @return string
55+
*/
56+
public function getCodelistQuery($codelist, $agency, $version)
57+
{
58+
// TODO: Implement getCodelistQuery() method.
59+
}
60+
61+
/**
62+
* @param Dataflow $dataflow
63+
* @param string $resource
64+
* @param array $options
65+
* ```php
66+
* $options = array(
67+
* 'startPeriod' => 'string', //Start time of the observations to be gathered
68+
* 'endPeriod' => 'string', //End time of the observations to be gathered
69+
* 'seriesKeysOnly' => 'boolean', //Flag for disabling data and attributes processing (usually for getting the only dataflow contents)
70+
* 'lastNObservations' => 'integer' //The last 'n' observations to return for each matched series.
71+
* )
72+
* ```
73+
* @return string
74+
*/
75+
public function getDataQuery(Dataflow $dataflow, $resource, array $options = array())
76+
{
77+
// TODO: Implement getDataQuery() method.
78+
}
79+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Sdmx\api\parser\v20;
4+
5+
6+
use Sdmx\api\entities\DataflowStructure;
7+
use Sdmx\api\parser\DataStructureParser;
8+
use SimpleXMLElement;
9+
10+
class V20DataStructureParser implements DataStructureParser
11+
{
12+
13+
/**
14+
* @param string $data
15+
* @return DataflowStructure[]
16+
*/
17+
public function parse($data)
18+
{
19+
$xml = new SimpleXMLElement($data);
20+
$ns = $xml->getNamespaces(true);
21+
foreach($ns as $key => $value){
22+
$xml->registerXPathNamespace($key, $value);
23+
}
24+
$result = [];
25+
$families = $xml->xpath('/message:Structure/message:KeyFamilies/*[name()="KeyFamily"]');
26+
27+
foreach ($families as $family){
28+
$result[] = $this->parseStructure($family);
29+
}
30+
31+
return $result;
32+
}
33+
34+
/**
35+
* @param SimpleXMLElement $family
36+
* @return DataflowStructure
37+
*/
38+
private function parseStructure(SimpleXMLElement $family)
39+
{
40+
$result = new DataflowStructure();
41+
42+
$result->setId((string) $family['id']);
43+
$result->setAgency((string) $family['agencyID']);
44+
45+
$name = $family->xpath('./*[name()="Name"][@xml:lang="en"]');
46+
if(count($name) > 0){
47+
$result->setName((string)$name[0]);
48+
}
49+
50+
return $result;
51+
}
52+
}

0 commit comments

Comments
 (0)