Skip to content

Commit 0aab3e9

Browse files
committed
Working on DHS descriptors.
Created CSV file with descriptors: will have to refactor class.
1 parent 06fe3e1 commit 0aab3e9

File tree

3 files changed

+195
-47
lines changed

3 files changed

+195
-47
lines changed

.idea/workspace.xml

+32-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

batch/DHS.php

+151-7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ class DHS
6666
const kDHS_URL_INDICATORS =
6767
'http://api.dhsprogram.com/rest/dhs/indicators/fields?f=json';
6868

69+
/**
70+
* <h4>DHS data descriptors URL.</h4>
71+
*
72+
* This constant holds the <i>DHS data descriptors URL</i>.
73+
*
74+
* @var string
75+
*/
76+
const kDHS_URL_DATA_INDICATORS =
77+
'http://api.dhsprogram.com/rest/dhs/data/fields?f=json';
78+
79+
/**
80+
* <h4>DHS country descriptors URL.</h4>
81+
*
82+
* This constant holds the <i>DHS country descriptors URL</i>.
83+
*
84+
* @var string
85+
*/
86+
const kDHS_URL_COUNTRY_INDICATORS =
87+
'http://api.dhsprogram.com/rest/dhs/countries/fields?f=json';
88+
6989
/**
7090
* <h4>Wrapper object.</h4>
7191
*
@@ -215,8 +235,8 @@ public function InitTypes()
215235
$ns[ kTAG_LID ] = self::kDHS_NAMESPACE;
216236
$ns[ kTAG_NAME ] = [ 'en' => "Demographic and Health Surveys (DHS) Program" ];
217237
$ns[ kTAG_DESCRIPTION ] = [ 'en' => "This namespace groups all metadata " .
218-
"regarding the USAID Demographic and Health " .
219-
"Surveys" ];
238+
"regarding the USAID Demographic and Health " .
239+
"Surveys" ];
220240
$ns->Store();
221241

222242
//
@@ -291,15 +311,15 @@ public function InitTypes()
291311

292312

293313
/*===================================================================================
294-
* InitBaseDescriptors *
314+
* InitIndicators *
295315
*==================================================================================*/
296316

297317
/**
298318
* <h4>Initialise base descriptors.</h4>
299319
*
300320
* This method will load the base descriptors and load the descriptor match table.
301321
*/
302-
public function InitBaseDescriptors()
322+
public function InitIndicators()
303323
{
304324
//
305325
// Init local storage.
@@ -418,7 +438,7 @@ public function InitBaseDescriptors()
418438
$terms->FindByKey(
419439
$descriptor[ kTAG_GID ],
420440
[ kTOKEN_OPT_MANY => FALSE,
421-
kTOKEN_OPT_FORMAT => kTOKEN_OPT_FORMAT_HANDLE ] ),
441+
kTOKEN_OPT_FORMAT => kTOKEN_OPT_FORMAT_HANDLE ] ),
422442
$handle );
423443
$pred->Store();
424444
$this->mDatabase->SetDescriptor( $descriptor );
@@ -441,9 +461,133 @@ public function InitBaseDescriptors()
441461
} // Setting enumerated types.
442462

443463
} // Loading descriptors.
444-
print_r( $this->mDatabase->GetDescriptor( "@9" ) );
445464

446-
} // InitBaseDescriptors.
465+
} // InitIndicators.
466+
467+
468+
/*===================================================================================
469+
* InitDataIndicators *
470+
*==================================================================================*/
471+
472+
/**
473+
* <h4>Initialise data descriptors.</h4>
474+
*
475+
* This method will load the data descriptors and load the descriptor match table.
476+
*/
477+
public function InitDataIndicators()
478+
{
479+
//
480+
// Init local storage.
481+
//
482+
$types = $this->mDatabase->NewTypesCollection();
483+
$terms = $this->mDatabase->NewTermsCollection();
484+
$descriptors = $this->mDatabase->NewDescriptorsCollection();
485+
486+
//
487+
// Load base descriptors.
488+
//
489+
$indicators =
490+
json_decode( file_get_contents( self::kDHS_URL_DATA_INDICATORS ), TRUE )
491+
[ 'Data' ];
492+
493+
//
494+
// Load match table.
495+
//
496+
foreach( $indicators as $indicator )
497+
{
498+
//
499+
// Init descriptor.
500+
//
501+
$descriptor = new \Milko\PHPLib\Descriptor( $descriptors );
502+
503+
//
504+
// Set identifiers.
505+
//
506+
$descriptor[ kTAG_NS ] = self::kDHS_NAMESPACE;
507+
$descriptor[ kTAG_LID ] = $indicator[ 'fieldname' ];
508+
$descriptor[ kTAG_SYMBOL ] = $indicator[ 'fieldname' ];
509+
510+
//
511+
// Set names.
512+
//
513+
$descriptor[ kTAG_NAME ] = [ 'en' => $indicator[ 'fieldname' ] ];
514+
$descriptor[ kTAG_DESCRIPTION ] =
515+
[ 'en' => str_replace( "\t", '', $indicator[ 'fieldDescription' ] ) ];
516+
517+
//
518+
// Set data types.
519+
//
520+
switch( $indicator[ 'fieldname' ] )
521+
{
522+
case 'Indicator':
523+
case 'CountryName':
524+
case 'SurveyId':
525+
case 'IndicatorId':
526+
case 'CharacteristicLabel':
527+
case 'ByVariableLabel':
528+
case 'SurveyYearLabel':
529+
$descriptor[ kTAG_DATA_TYPE ] = kTYPE_STRING;
530+
$descriptor[ kTAG_DATA_KIND ] = [ kKIND_DISCRETE ];
531+
break;
532+
533+
case 'CharacteristicCategory':
534+
case 'CharacteristicId':
535+
case 'RegionId':
536+
case 'SurveyType':
537+
$descriptor[ kTAG_DATA_TYPE ] = kTYPE_STRING;
538+
$descriptor[ kTAG_DATA_KIND ] = [ kKIND_CATEGORICAL, kKIND_SUMMARY ];
539+
break;
540+
541+
case 'DataId':
542+
case 'Precision':
543+
case 'SurveyYear':
544+
case 'CharacteristicOrder':
545+
case 'ByVariableId':
546+
$descriptor[ kTAG_DATA_TYPE ] = kTYPE_INT;
547+
$descriptor[ kTAG_DATA_KIND ] = [ kKIND_DISCRETE ];
548+
break;
549+
550+
case 'DenominatorWeighted':
551+
case 'DenominatorUnweighted':
552+
$descriptor[ kTAG_DATA_TYPE ] = kTYPE_INT;
553+
$descriptor[ kTAG_DATA_KIND ] = [ kKIND_QUANTITATIVE ];
554+
break;
555+
556+
case 'Value':
557+
case 'CILow':
558+
case 'CIHigh':
559+
$descriptor[ kTAG_DATA_TYPE ] = kTYPE_FLOAT;
560+
$descriptor[ kTAG_DATA_KIND ] = [ kKIND_QUANTITATIVE ];
561+
break;
562+
563+
case 'DHS_CountryCode':
564+
$descriptor[ kTAG_DATA_TYPE ] = kTYPE_ENUM;
565+
$descriptor[ kTAG_DATA_KIND ] = [ kKIND_CATEGORICAL ];
566+
break;
567+
568+
case 'IsTotal':
569+
case 'IsPreferred':
570+
$descriptor[ kTAG_DATA_TYPE ] = kTYPE_BOOLEAN;
571+
$descriptor[ kTAG_DATA_KIND ] = [ kKIND_DISCRETE ];
572+
break;
573+
574+
} // Setting data types.
575+
576+
//
577+
// Store descriptor.
578+
//
579+
$handle = $descriptor->Store();
580+
581+
//
582+
// Set match table entry.
583+
//
584+
if( ! array_key_exists( $indicator[ 'fieldname' ], $this->mMatchTable ) )
585+
$this->mMatchTable[ $indicator[ 'fieldname' ] ]
586+
= $descriptor[ $descriptors->KeyOffset() ];
587+
588+
} // Loading descriptors.
589+
590+
} // InitDataIndicators.
447591

448592

449593

0 commit comments

Comments
 (0)