/**
  * This method loads a provided array into the data structure.
  * It also creates dependencies, such as contact data objects
  * and populate the values into those objects.
  *
  * @param $data array of db-values.
  */
 public function loadData($data)
 {
     if ($data == null) {
         return;
     }
     if (!is_array($data)) {
         return;
     }
     foreach ($data as $k => $v) {
         // store data into this object (no ':" in the string)
         if (strpos($k, ':') === false) {
             $this->{$k} = Convert::xml2raw($v);
         } else {
             // A ':' is used as a namespace marker. It is used to
             // create the related data objects, such as MDContacts.
             $relations = explode(':', $k);
             $fieldName = array_pop($relations);
             $relObj = $this;
             // iterate through the relationships. At the moment, this
             // loading process just works for 1 level hierarchies.
             foreach ($relations as $relation) {
                 if ($relation == 'PointOfContacts') {
                     // load the sub-array into the MDContact object
                     $item = new MDContact();
                     $item->loadData($v);
                     // add the new MDContect to the collection class of this
                     // object.
                     $relObj->PointOfContacts()->add($item);
                 }
                 if ($relation == 'MDContacts') {
                     // load the sub-array into the MDContact object
                     $item = new MDContact();
                     $item->loadData($v);
                     // add the new MDContect to the collection class of this
                     // object.
                     $relObj->MDContacts()->add($item);
                 }
                 if ($relation == 'MDResourceConstraints') {
                     // load the sub-array into the MDResourceConstraints object
                     if (is_array($v)) {
                         foreach ($v as $vitem) {
                             $item = new MDResourceConstraint();
                             $item->loadData($vitem);
                             // add the new MDContect to the collection class of this
                             // object.
                             $relObj->MDResourceConstraints()->add($item);
                         }
                     }
                 }
                 if ($relation == 'MDResourceFormats') {
                     if (is_array($v)) {
                         foreach ($v as $vitem) {
                             // load the sub-array into the MDResourceFormats object
                             $item = new MDResourceFormat();
                             $item->loadData($vitem);
                             // add the new MDContect to the collection class of this
                             // object.
                             $relObj->MDResourceFormats()->add($item);
                         }
                     }
                 }
                 if ($relation == 'MDTopicCategory') {
                     if (is_array($v)) {
                         foreach ($v as $vitem) {
                             // load the sub-array into the MDResourceFormats object
                             $item = new MDTopicCategory();
                             $item->loadData($vitem);
                             // add the new MDTopicCategory to the collection class of this
                             // object.
                             $relObj->MDTopicCategory()->add($item);
                         }
                     }
                 }
                 if ($relation == 'MDCitationDates') {
                     if (is_array($v)) {
                         foreach ($v as $vitem) {
                             // load the sub-array into the MDResourceFormats object
                             $item = new MDCitationDate();
                             $item->loadData($vitem);
                             // add the new MDContect to the collection class of this
                             // object.
                             $relObj->MDCitationDates()->add($item);
                         }
                     }
                 }
                 if ($relation == 'MCPMDCreativeCommons') {
                     if (is_array($v)) {
                         foreach ($v as $vitem) {
                             // load the sub-array into the MDContact object
                             $item = new MCPMDCreativeCommons();
                             $item->loadData($vitem);
                             // add the new MCPMDCreativeCommons to the collection class of this
                             // object.
                             $relObj->MCPMDCreativeCommons()->add($item);
                         }
                     }
                 }
                 if ($relation == 'CIOnlineResources') {
                     if (is_array($v)) {
                         foreach ($v as $vitem) {
                             // load the sub-array into the MDContact object
                             $item = new CIOnlineResource();
                             $item->loadData($vitem);
                             // add the new MDContect to the collection class of this
                             // object.
                             $relObj->CIOnlineResources()->add($item);
                         }
                     }
                 }
                 if ($relation == 'MDHierarchyLevel') {
                     if (is_array($v)) {
                         foreach ($v as $vitem) {
                             $codes = MDCodeTypes::get_scope_codes();
                             if (isset($codes[$vitem['Value']])) {
                                 $item = new MDHierarchyLevel();
                                 $item->loadData($vitem);
                                 $relObj->MDHierarchyLevel()->add($item);
                             }
                         }
                     }
                 }
                 if ($relation == 'MDHierarchyLevelName') {
                     if (is_array($v)) {
                         foreach ($v as $vitem) {
                             $item = new MDHierarchyLevelName();
                             $item->loadData($vitem);
                             $relObj->MDHierarchyLevelName()->add($item);
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Test getCIOnlineLinkageNice of CIOnlineResource.
  */
 function testGetCIOnlineLinkageNice()
 {
     $ciOnlineResource = array();
     $ciOnlineResource['CIOnlineLinkage'] = trim('http://www.mysite.com');
     $ciOnlineResource['CIOnlineProtocol'] = trim('WWW:LINK-1.0-http--link');
     $ciOnlineResource['CIOnlineFunction'] = trim('download');
     $ciOnlineResource['CIOnlineDescription'] = trim('Valid Description');
     $ciOnlineResource['CIOnlineName'] = trim('Valid Name');
     // load the sub-array into the object
     $item = new CIOnlineResource();
     $item->loadData($ciOnlineResource);
     $this->assertEquals($item->getField('CIOnlineLinkage'), 'http://www.mysite.com', 'Problem creating the CIOnlineResource');
     //Checking standardprotocol
     $this->assertEquals($item->getCIOnlineLinkageNice(), 'http://www.mysite.com', 'initial CIOnlineFunction failed for getCIOnlineLinkageNice(). Value in MDCodeTypes might have changed');
     //without protocol in th field http:// is added
     $ciOnlineResource['CIOnlineLinkage'] = trim('www.mysite.com');
     $item = new CIOnlineResource();
     $item->loadData($ciOnlineResource);
     $this->assertEquals($item->getCIOnlineLinkageNice(), 'http://www.mysite.com', 'adding http:// to link failed  for getCIOnlineLinkageNice().');
     //other protocol in the field svn:// is ok
     $ciOnlineResource['CIOnlineLinkage'] = trim('svn://www.mysite.com');
     $item = new CIOnlineResource();
     $item->loadData($ciOnlineResource);
     $this->assertEquals($item->getCIOnlineLinkageNice(), 'svn://www.mysite.com', 'other protocol failed for getCIOnlineLinkageNice().');
     //empty function
     $ciOnlineResource['CIOnlineLinkage'] = '';
     $item = new CIOnlineResource();
     $item->loadData($ciOnlineResource);
     $this->assertEquals($item->getCIOnlineLinkageNice(), MDCodeTypes::$default_for_null_value, 'empty CIOnlineFunction failed for getCIOnlineLinkageNice()');
     // null function
     $ciOnlineResource['CIOnlineLinkage'] = null;
     $item = new CIOnlineResource();
     $item->loadData($ciOnlineResource);
     $this->assertEquals($item->getCIOnlineLinkageNice(), MDCodeTypes::$default_for_null_value, 'null CIOnlineFunction failed for getCIOnlineLinkageNice()');
 }