Skip to content

Commit 3b58f30

Browse files
committed
Update V20 parser to parse WB data properly.
1 parent 5d45f90 commit 3b58f30

File tree

7 files changed

+6568
-4
lines changed

7 files changed

+6568
-4
lines changed

src/Sdmx/api/entities/PortableTimeSeries.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ public function addAttribute($key, $value)
272272
*/
273273
private function addKeyValueToCollection($key, $value, array &$collection)
274274
{
275-
$collection[] = trim($key) . '=' . trim($value);
275+
$value = trim($key) . '=' . trim($value);
276+
if (!in_array($value, $collection)) {
277+
$collection[] = $value;
278+
}
279+
276280
}
277281

278282
/**

src/Sdmx/api/parser/v20/V20DataParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function parseDimensions($series, $portableSeries, $dsd)
8383
*/
8484
private function parseAttributes($series, $portableSeries)
8585
{
86-
$attributes = $series->xpath('./Attributes/Value');
86+
$attributes = $series->xpath('.//Attributes/Value');
8787
foreach ($attributes as $attr) {
8888
$portableSeries->addAttribute((string)$attr['concept'], (string)$attr['value']);
8989
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Sdmx\Tests\api\client\rest\query;
4+
5+
use Sdmx\api\client\rest\query\SdmxQueryBuilder;
6+
use Sdmx\api\client\rest\query\WorldBankQueryBuilder;
7+
use Sdmx\api\entities\Dataflow;
8+
9+
class WorldBankQueryBuilderTest extends \PHPUnit_Framework_TestCase
10+
{
11+
const BASE_URL = 'http://some.base.url';
12+
13+
/**
14+
* @var SdmxQueryBuilder $builder
15+
*/
16+
private $builder;
17+
18+
public function testGetDataQuery()
19+
{
20+
$flow = new Dataflow();
21+
$flow->setId('SomeFlow');
22+
$flow->setAgency('WB');
23+
$url = $this->builder->getDataQuery($flow, 'A.B.C');
24+
25+
$this->assertSame(self::BASE_URL . '/v2/data/SomeFlow/C.B', $url);
26+
}
27+
28+
protected function setUp()
29+
{
30+
$this->builder = new WorldBankQueryBuilder(self::BASE_URL);
31+
}
32+
33+
34+
}

tests/Sdmx/Tests/api/parser/v20/V20DataParserTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use PHPUnit\Framework\TestCase;
77
use Sdmx\api\parser\DataParser;
8+
use Sdmx\api\parser\DataStructureParser;
89
use Sdmx\api\parser\v20\V20CodelistParser;
910
use Sdmx\api\parser\v20\V20DataParser;
1011
use Sdmx\api\parser\v20\V20DataStructureParser;
@@ -17,10 +18,14 @@ class V20DataParserTest extends TestCase
1718
*/
1819
private $parser;
1920

21+
/**
22+
* @var DataStructureParser $structureParser
23+
*/
24+
private $structureParser;
25+
2026
public function testParseQnaData()
2127
{
22-
$structureParser = new V20DataStructureParser(new V20CodelistParser());
23-
$dsd = $structureParser->parse(V20ParserFixtures::getDataStructure())[0];
28+
$dsd = $this->structureParser->parse(V20ParserFixtures::getDataStructure())[0];
2429
$result = $this->parser->parse(V20ParserFixtures::getQnaData(), $dsd, 'QNA', true);
2530

2631
$line = $result[0];
@@ -43,8 +48,33 @@ public function testParseQnaData()
4348
$this->assertEquals(['1', '2', '3', '4'], $line->getObsLevelAttributes('TEST'));
4449
}
4550

51+
public function testParseWbData()
52+
{
53+
$dsd = $this->structureParser->parse(V20ParserFixtures::getWbDataStructure())[0];
54+
$result = $this->parser->parse(V20ParserFixtures::getWbData(), $dsd, 'WDI', true);
55+
56+
$line = $result[0];
57+
$lineDimData = 'FREQ=A,SERIES=PA_NUS_PPP_05,REF_AREA=ES';
58+
$lineDimData = explode(',', $lineDimData);
59+
foreach ($lineDimData as $datum) {
60+
$tokens = explode('=', $datum);
61+
$this->assertEquals($tokens[1], $line->getDimensionValue($tokens[0]));
62+
}
63+
64+
$lineAttrData = 'UNIT_MULT=0';
65+
$lineAttrData = explode(',', $lineAttrData);
66+
foreach ($lineAttrData as $datum) {
67+
$tokens = explode('=', $datum);
68+
$this->assertEquals($tokens[1], $line->getAttributeValue($tokens[0]));
69+
}
70+
71+
$this->assertEquals([1.5, 2, 3], $line->getObservations());
72+
$this->assertEquals(['2014', '2015', '2016'], $line->getTimeSlots());
73+
}
74+
4675
protected function setUp()
4776
{
4877
$this->parser = new V20DataParser();
78+
$this->structureParser = new V20DataStructureParser(new V20CodelistParser());
4979
}
5080
}

tests/Sdmx/Tests/api/parser/v20/V20ParserFixtures.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public static function getDataStructure()
2626
return file_get_contents(self::getFileUrl(self::DATA_STRUCTURE_FILE));
2727
}
2828

29+
public static function getWbDataStructure(){
30+
return file_get_contents(self::getFileUrl('wb_structure'));
31+
}
32+
33+
public static function getWbData(){
34+
return file_get_contents(self::getFileUrl('wb_data'));
35+
}
36+
2937
/**
3038
* @return string
3139
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<GenericData xmlns:generic="http://www.SDMX.org/resources/SDMXML/schemas/v2_1/generic"
3+
xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_1/message">
4+
<Header>
5+
<ID>WB</ID>
6+
<Test>true</Test>
7+
<Truncated>false</Truncated>
8+
<Name xml:lang="en">The World Bank</Name>
9+
<Prepared>2017-03-27T17:31:44.4818054-04:00</Prepared>
10+
<Sender id="WB">
11+
<Name xml:lang="en">The Worldbank Group</Name>
12+
</Sender>
13+
</Header>
14+
<DataSet>
15+
<generic:KeyFamilyRef>WB_WDI</generic:KeyFamilyRef>
16+
<generic:Series>
17+
<generic:SeriesKey>
18+
<generic:Value concept="FREQ" value="A"/>
19+
<generic:Value concept="SERIES" value="PA_NUS_PPP_05"/>
20+
<generic:Value concept="REF_AREA" value="ES"/>
21+
</generic:SeriesKey>
22+
<generic:Obs>
23+
<generic:Time>2014</generic:Time>
24+
<generic:ObsValue value="1.5"/>
25+
<generic:Attributes>
26+
<generic:Value concept="UNIT_MULT" value="0"/>
27+
</generic:Attributes>
28+
</generic:Obs>
29+
<generic:Obs>
30+
<generic:Time>2015</generic:Time>
31+
<generic:ObsValue value="2"/>
32+
<generic:Attributes>
33+
<generic:Value concept="UNIT_MULT" value="0"/>
34+
</generic:Attributes>
35+
</generic:Obs>
36+
<generic:Obs>
37+
<generic:Time>2016</generic:Time>
38+
<generic:ObsValue value="3"/>
39+
<generic:Attributes>
40+
<generic:Value concept="UNIT_MULT" value="0"/>
41+
</generic:Attributes>
42+
</generic:Obs>
43+
</generic:Series>
44+
</DataSet>
45+
</GenericData>

0 commit comments

Comments
 (0)