Skip to content

Commit b54f233

Browse files
committed
Renamed fixture classes. Updated v21 data parser to parser compact format from v20 apis.
1 parent dde0a79 commit b54f233

11 files changed

+279
-22
lines changed

src/Sdmx/api/parser/v21/V21DataParser.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ class V21DataParser implements DataParser
2323
*/
2424
public function parse($data, DataflowStructure $dsd, $dataflow, $containsData)
2525
{
26-
$xml = new SimpleXMLElement($data);
26+
$dataWithoutNs = $this->removeNamespaces($data);
27+
$xml = new SimpleXMLElement($dataWithoutNs);
2728
$result = [];
2829

29-
$dataSet = $xml->xpath('/message:StructureSpecificData/message:DataSet')[0];
30+
$dataSet = $xml->xpath('//DataSet')[0];
3031
$action = (string)$dataSet[self::ACTION];
3132
$validFrom = (string)$dataSet[self::VALID_FROM];
3233
$validTo = (string)$dataSet[self::VALID_TO];
3334

34-
$series = $dataSet->xpath('./ns1:Series');
35+
$series = $dataSet->xpath('./Series');
3536
foreach ($series as $seriesLine) {
3637
$result[] = $this->parseSeriesLine($seriesLine, $dsd, $dataflow, $containsData, $action, $validFrom, $validTo);
3738
}
@@ -58,7 +59,7 @@ private function parseSeriesLine(SimpleXMLElement $seriesLine, DataflowStructure
5859
$this->setMetadata($seriesLine, $dsd, $action, $validFrom, $validTo, $result);
5960

6061
if ($containsData) {
61-
$observations = $seriesLine->xpath('./ns1:Obs');
62+
$observations = $seriesLine->xpath('./Obs');
6263
foreach ($observations as $observation) {
6364
$this->processObservation($dsd, $observation, $result);
6465
}
@@ -142,4 +143,17 @@ private function processObservation(DataflowStructure $dsd, SimpleXMLElement $ob
142143

143144
$result->addObservation($obsVal, $time, $obsAttr);
144145
}
146+
147+
/**
148+
* @param string $data
149+
* @return string
150+
*/
151+
protected function removeNamespaces($data)
152+
{
153+
$dataWoNs = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $data);
154+
$dataWoNs = preg_replace('/[a-zA-Z]+:([a-zA-Z]+[=>])/', '$1', $dataWoNs);
155+
$dataWoNs = preg_replace('/(<\/*)[^>:]+:/', '$1', $dataWoNs);
156+
157+
return $dataWoNs;
158+
}
145159
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function setUp()
2323

2424
public function testParseCodelistNode()
2525
{
26-
$xml = new SimpleXMLElement(ParserFixtures::getDataStructure());
26+
$xml = new SimpleXMLElement(V20ParserFixtures::getDataStructure());
2727
$nodes = $xml->xpath('//*[name()="CodeList"]');
2828
$codelist = $this->parser->parseCodesFromNode($nodes[0]);
2929

@@ -36,7 +36,7 @@ public function testParseCodelistNode()
3636

3737
public function testParseCodelist()
3838
{
39-
$codelist = $this->parser->parseCodes(ParserFixtures::getDataStructure());
39+
$codelist = $this->parser->parseCodes(V20ParserFixtures::getDataStructure());
4040

4141
$this->assertNotNull($codelist);
4242
$this->assertEquals(58, count($codelist));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function setUp()
2626

2727
public function testParseDataStructures()
2828
{
29-
$structures = $this->parser->parse(ParserFixtures::getDataStructures());
29+
$structures = $this->parser->parse(V20ParserFixtures::getDataStructures());
3030

3131
$this->assertNotNull($structures);
3232
$this->assertEquals(2, count($structures));
@@ -37,7 +37,7 @@ public function testParseDataStructures()
3737

3838
public function testParseDataStructureWithChildren()
3939
{
40-
$dataStructures = $this->parser->parse(ParserFixtures::getDatastructure());
40+
$dataStructures = $this->parser->parse(V20ParserFixtures::getDatastructure());
4141

4242
$this->assertNotNull($dataStructures);
4343
$this->assertEquals(1, count($dataStructures));

tests/Sdmx/Tests/api/parser/v20/ParserFixtures.php renamed to tests/Sdmx/Tests/api/parser/v20/V20ParserFixtures.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Sdmx\Tests\api\parser\v20;
44

5-
class ParserFixtures
5+
class V20ParserFixtures
66
{
77
const DATA_STRUCTURES_FILE = 'datastructures';
88
const DATA_STRUCTURE_FILE = 'datastructure';
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<message:MessageGroup xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic"
2+
xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common"
3+
xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic http://www.sdmx.org/docs/2_0/SDMXGenericData.xsd http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message http://www.sdmx.org/docs/2_0/SDMXMessage.xsd"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:message="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message">
6+
<Header xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message">
7+
<ID>none</ID>
8+
<Test>false</Test>
9+
<Truncated>false</Truncated>
10+
<Prepared>2017-03-18T18:14:19</Prepared>
11+
<Sender id="OECD">
12+
<Name xml:lang="en">Organisation for Economic Co-operation and Development</Name>
13+
<Name xml:lang="fr">Organisation de coopération et de développement économiques</Name>
14+
</Sender>
15+
</Header>
16+
<DataSet keyFamilyURI="http://stats.oecd.org/RestSDMX/sdmx.ashx/GetKeyFamily/QNA">
17+
<KeyFamilyRef>QNA</KeyFamilyRef>
18+
<Series>
19+
<SeriesKey>
20+
<Value concept="LOCATION" value="AUS"/>
21+
<Value concept="SUBJECT" value="B1_GE"/>
22+
<Value concept="MEASURE" value="VOBARSA"/>
23+
<Value concept="FREQUENCY" value="Q"/>
24+
</SeriesKey>
25+
<Attributes>
26+
<Value concept="TIME_FORMAT" value="P3M"/>
27+
<Value concept="UNIT" value="AUD"/>
28+
<Value concept="POWERCODE" value="6"/>
29+
<Value concept="REFERENCEPERIOD" value="2010"/>
30+
</Attributes>
31+
<Obs>
32+
<Time>2009-Q2</Time>
33+
<ObsValue value="1325930.53235"/>
34+
</Obs>
35+
<Obs>
36+
<Time>2009-Q3</Time>
37+
<ObsValue value="1329921.736824"/>
38+
</Obs>
39+
<Obs>
40+
<Time>2009-Q4</Time>
41+
<ObsValue value="1339385.182638"/>
42+
</Obs>
43+
<Obs>
44+
<Time>2010-Q1</Time>
45+
<ObsValue value="1346122.001608"/>
46+
</Obs>
47+
<Obs>
48+
<Time>2010-Q2</Time>
49+
<ObsValue value="1354005.674766"/>
50+
</Obs>
51+
<Obs>
52+
<Time>2010-Q3</Time>
53+
<ObsValue value="1361893.145455"/>
54+
</Obs>
55+
<Obs>
56+
<Time>2010-Q4</Time>
57+
<ObsValue value="1375169.311813"/>
58+
</Obs>
59+
<Obs>
60+
<Time>2011-Q1</Time>
61+
<ObsValue value="1372218.630675"/>
62+
</Obs>
63+
<Obs>
64+
<Time>2011-Q2</Time>
65+
<ObsValue value="1387564.451114"/>
66+
</Obs>
67+
<Obs>
68+
<Time>2011-Q3</Time>
69+
<ObsValue value="1404000.162784"/>
70+
</Obs>
71+
<Obs>
72+
<Time>2011-Q4</Time>
73+
<ObsValue value="1417956.087089"/>
74+
</Obs>
75+
</Series>
76+
<Series>
77+
<SeriesKey>
78+
<Value concept="LOCATION" value="AUT"/>
79+
<Value concept="SUBJECT" value="B1_GE"/>
80+
<Value concept="MEASURE" value="VOBARSA"/>
81+
<Value concept="FREQUENCY" value="Q"/>
82+
</SeriesKey>
83+
<Attributes>
84+
<Value concept="TIME_FORMAT" value="P3M"/>
85+
<Value concept="UNIT" value="EUR"/>
86+
<Value concept="POWERCODE" value="6"/>
87+
<Value concept="REFERENCEPERIOD" value="2010"/>
88+
</Attributes>
89+
<Obs>
90+
<Time>2009-Q2</Time>
91+
<ObsValue value="286920.620974"/>
92+
</Obs>
93+
<Obs>
94+
<Time>2009-Q3</Time>
95+
<ObsValue value="288010.744978"/>
96+
</Obs>
97+
<Obs>
98+
<Time>2009-Q4</Time>
99+
<ObsValue value="291794.30499"/>
100+
</Obs>
101+
<Obs>
102+
<Time>2010-Q1</Time>
103+
<ObsValue value="288933.060981"/>
104+
</Obs>
105+
<Obs>
106+
<Time>2010-Q2</Time>
107+
<ObsValue value="294001.200998"/>
108+
</Obs>
109+
<Obs>
110+
<Time>2010-Q3</Time>
111+
<ObsValue value="296313.537006"/>
112+
</Obs>
113+
<Obs>
114+
<Time>2010-Q4</Time>
115+
<ObsValue value="298183.345012"/>
116+
</Obs>
117+
<Obs>
118+
<Time>2011-Q1</Time>
119+
<ObsValue value="303069.257029"/>
120+
</Obs>
121+
<Obs>
122+
<Time>2011-Q2</Time>
123+
<ObsValue value="303077.701029"/>
124+
</Obs>
125+
<Obs>
126+
<Time>2011-Q3</Time>
127+
<ObsValue value="303778.465031"/>
128+
</Obs>
129+
<Obs>
130+
<Time>2011-Q4</Time>
131+
<ObsValue value="302443.261027"/>
132+
</Obs>
133+
</Series>
134+
<Annotations>
135+
<common:Annotation xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common">
136+
<common:AnnotationTitle>Copyright OECD - All rights reserved</common:AnnotationTitle>
137+
</common:Annotation>
138+
<common:Annotation xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common">
139+
<common:AnnotationTitle>Terms and Conditions</common:AnnotationTitle>
140+
<common:AnnotationURL>http://www.oecd.org/termsandconditions/</common:AnnotationURL>
141+
</common:Annotation>
142+
<common:Annotation xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common">
143+
<common:AnnotationTitle>Privacy Policy</common:AnnotationTitle>
144+
<common:AnnotationURL>http://www.oecd.org/privacy/</common:AnnotationURL>
145+
</common:Annotation>
146+
<common:Annotation xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common">
147+
<common:AnnotationTitle>MyOECD</common:AnnotationTitle>
148+
<common:AnnotationURL>https://www.oecd.org/login</common:AnnotationURL>
149+
</common:Annotation>
150+
<common:Annotation xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common">
151+
<common:AnnotationTitle>Contact Us</common:AnnotationTitle>
152+
<common:AnnotationURL>http://www.oecd.org/contact/</common:AnnotationURL>
153+
</common:Annotation>
154+
</Annotations>
155+
</DataSet>
156+
</message:MessageGroup>

tests/Sdmx/Tests/api/parser/v21/V21CodelistParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class V21CodelistParserTest extends TestCase
1717

1818
public function testParseCodelistNode()
1919
{
20-
$data = new \SimpleXMLElement(ParserFixtures::getCodelist());
20+
$data = new \SimpleXMLElement(V21ParserFixtures::getCodelist());
2121
$node = $data->xpath('//mes:Structure/mes:Structures/str:Codelists/str:Codelist')[0];
2222
$codelist = $this->codelistParser->parseCodesFromNode($node);
2323

@@ -27,7 +27,7 @@ public function testParseCodelistNode()
2727

2828
public function testParseCodelistResponse()
2929
{
30-
$codelist = $this->codelistParser->parseCodes(ParserFixtures::getCodelist());
30+
$codelist = $this->codelistParser->parseCodes(V21ParserFixtures::getCodelist());
3131

3232
$this->assertSame('Zero', $codelist['0']);
3333
$this->assertSame('Fifteen', $codelist['15']);

tests/Sdmx/Tests/api/parser/v21/V21DataParserTest.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
use Sdmx\api\entities\DataflowStructure;
88
use Sdmx\api\entities\Dimension;
99
use Sdmx\api\parser\DataParser;
10+
use Sdmx\api\parser\v20\V20CodelistParser;
11+
use Sdmx\api\parser\v20\V20DataStructureParser;
1012
use Sdmx\api\parser\v21\V21CodelistParser;
1113
use Sdmx\api\parser\v21\V21DataParser;
1214
use Sdmx\api\parser\v21\V21DataStructureParser;
15+
use Sdmx\Tests\api\parser\v20\V20ParserFixtures;
1316

1417

1518
class V21DataParserTest extends TestCase
@@ -22,8 +25,8 @@ class V21DataParserTest extends TestCase
2225
public function testParseDataWithKeySeriesOnly()
2326
{
2427
$structureParser = new V21DataStructureParser(new V21CodelistParser());
25-
$dsd = $structureParser->parse(ParserFixtures::getEduNonFinance('structure'))[0];
26-
$result = $this->parser->parse(ParserFixtures::getEduNonFinance('data_onlykeyseries'), $dsd, 'UNESCO,EDU_NON_FINANCE,1.0', false);
28+
$dsd = $structureParser->parse(V21ParserFixtures::getEduNonFinance('structure'))[0];
29+
$result = $this->parser->parse(V21ParserFixtures::getEduNonFinance('data_onlykeyseries'), $dsd, 'UNESCO,EDU_NON_FINANCE,1.0', false);
2730

2831
$line = $result[0];
2932
$lineData = 'UNIT_MEASURE=PT,STAT_UNIT=GER,SEX=F,REF_AREA=GQ,AGE=OVER_AGE,REGION_DEST=W00,COUNTRY_ORIGIN=W00,SECTOR_EDU=INST_T,GRADE=_T,EDU_FIELD=_T,EDU_ATTAIN=_Z,EDU_CAT=_T,EDU_LEVEL=L1,EDU_TYPE=_T,WEALTH_QUINTILE=_Z,LOCATION=_T';
@@ -77,7 +80,7 @@ public function testParseDataWithKeySeriesOnlyFrequencyDim()
7780
$this->assertEquals('YEAR', $line->getFrequency());
7881
}
7982

80-
public function testParseDataDataSetMeta()
83+
public function testParseDataSetMeta()
8184
{
8285
$dsd = new DataflowStructure();
8386
$dimension = new Dimension();
@@ -102,8 +105,8 @@ public function testParseDataDataSetMeta()
102105
public function testParseData()
103106
{
104107
$structureParser = new V21DataStructureParser(new V21CodelistParser());
105-
$dsd = $structureParser->parse(ParserFixtures::getEduNonFinance('structure'))[0];
106-
$result = $this->parser->parse(ParserFixtures::getEduNonFinance('data'), $dsd, 'UNESCO,EDU_NON_FINANCE,1.0', true);
108+
$dsd = $structureParser->parse(V21ParserFixtures::getEduNonFinance('structure'))[0];
109+
$result = $this->parser->parse(V21ParserFixtures::getEduNonFinance('data'), $dsd, 'UNESCO,EDU_NON_FINANCE,1.0', true);
107110

108111
$line = $result[0];
109112
$lineData = 'UNIT_MEASURE=PT,STAT_UNIT=GER,SEX=F,REF_AREA=GQ,AGE=OVER_AGE,REGION_DEST=W00,COUNTRY_ORIGIN=W00,SECTOR_EDU=INST_T,GRADE=_T,EDU_FIELD=_T,EDU_ATTAIN=_Z,EDU_CAT=_T,EDU_LEVEL=L1,EDU_TYPE=_T,WEALTH_QUINTILE=_Z,LOCATION=_T';
@@ -120,6 +123,30 @@ public function testParseData()
120123
$this->assertEquals(['5'], $line->getObsLevelAttributes('DECIMALS'));
121124
}
122125

126+
public function testParseQnaData(){
127+
$structureParser = new V20DataStructureParser(new V20CodelistParser());
128+
$dsd = $structureParser->parse(V20ParserFixtures::getDataStructure())[0];
129+
$result = $this->parser->parse(V21ParserFixtures::getQnaData(), $dsd, 'QNA', true);
130+
131+
$line = $result[0];
132+
$lineDimData = 'LOCATION=AUS,SUBJECT=B1_GE,MEASURE=VOBARSA,FREQUENCY=Q';
133+
$lineDimData = explode(',', $lineDimData);
134+
foreach ($lineDimData as $datum) {
135+
$tokens = explode('=', $datum);
136+
$this->assertEquals($tokens[1], $line->getDimensionValue($tokens[0]));
137+
}
138+
139+
$lineAttrData= 'TIME_FORMAT=P3M,UNIT=AUD,POWERCODE=6,REFERENCEPERIOD=2010';
140+
$lineAttrData = explode(',', $lineAttrData);
141+
foreach ($lineAttrData as $datum) {
142+
$tokens = explode('=', $datum);
143+
$this->assertEquals($tokens[1], $line->getAttributeValue($tokens[0]));
144+
}
145+
146+
$this->assertEquals([1372218.6306749999, 1387564.4511140001, 1404000.1627839999, 1417956.087089], $line->getObservations());
147+
$this->assertEquals(['2011-Q1', '2011-Q2', '2011-Q3', '2011-Q4'], $line->getTimeSlots());
148+
}
149+
123150
protected function setUp()
124151
{
125152
$this->parser = new V21DataParser();

tests/Sdmx/Tests/api/parser/v21/V21DataStructureParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class V21DataStructureParserTest extends TestCase
2121

2222
public function testParseDataStructureWithoutChildren()
2323
{
24-
$dataStructures = $this->parser->parse(ParserFixtures::getDatastructure());
24+
$dataStructures = $this->parser->parse(V21ParserFixtures::getDatastructure());
2525

2626
$this->assertNotNull($dataStructures);
2727
$this->assertEquals(1, count($dataStructures));
@@ -42,7 +42,7 @@ public function testParseDataStructureWithoutChildren()
4242

4343
public function testParseDataStructureWithChildren()
4444
{
45-
$dataStructures = $this->parser->parse(ParserFixtures::getDatastructure('1'));
45+
$dataStructures = $this->parser->parse(V21ParserFixtures::getDatastructure('1'));
4646

4747
$this->assertNotNull($dataStructures);
4848
$this->assertEquals(1, count($dataStructures));

tests/Sdmx/Tests/api/parser/v21/V21DataflowParserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class V21DataflowParserTest extends TestCase
1616

1717
public function testParseDataflowList()
1818
{
19-
$result = $this->parser->parse(ParserFixtures::getDataflow());
19+
$result = $this->parser->parse(V21ParserFixtures::getDataflow());
2020

2121
$this->assertNotNull($result);
2222
$this->assertEquals(2, count($result));
@@ -27,7 +27,7 @@ public function testParseDataflowList()
2727

2828
public function testParseDataflowWithoutName()
2929
{
30-
$result = $this->parser->parse(ParserFixtures::getDataflow('1'));
30+
$result = $this->parser->parse(V21ParserFixtures::getDataflow('1'));
3131

3232
$this->assertNotNull($result);
3333
$this->assertEquals(1, count($result));
@@ -37,7 +37,7 @@ public function testParseDataflowWithoutName()
3737

3838
public function testParseDataflowWithoutDsd()
3939
{
40-
$result = $this->parser->parse(ParserFixtures::getDataflow('2'));
40+
$result = $this->parser->parse(V21ParserFixtures::getDataflow('2'));
4141

4242
$this->assertNotNull($result);
4343
$this->assertEquals(1, count($result));

0 commit comments

Comments
 (0)