Exemple #1
0
/**
 *  @function all_tags_from_xml()
 *    returns an asociative array containing all selected tag-names as keys 
 *    and all tag content as the values. 
 *
 *  @param $xml (object): The an XML object containing the relevant children
 *    obtained from the XML. Could be obtained by XML->children('oml', true)
 *    or something similar, to get all childeren in the OML namespace. 
 *  @param $configuration (2d string array): an array configuring which fields to be
 *    included in the return value. $configuration can contain 4 sub arrays, 'array',
 *    'csv', 'plain' and 'string'. Putting a field in one of these sub arrays, deter-
 *    mines 
 *  @param $return_array (string array): The base array to add tags to. Usually, when
 *    some fields are already set, these can be joined with the fields to
 *    be set within this function.
 * 
 *  @return (MULTIPLE VALUES array): asociative array containing all selected tag-names 
 *    as keys and all tag content as the values. 
 */
function all_tags_from_xml($xml, $configuration = array(), $return_array = array())
{
    $csv_tags = array();
    $include = array_collapse($configuration);
    foreach ($xml as $key => $value) {
        if (in_array($key, $include)) {
            if (array_key_exists('array', $configuration) && in_array($key, $configuration['array'])) {
                // returned in plain array
                if (!array_key_exists($key, $return_array)) {
                    $return_array[$key] = array();
                }
                $return_array[$key][] = $value;
            } elseif (array_key_exists('csv', $configuration) && in_array($key, $configuration['csv'])) {
                // returned in CSV format
                if (!array_key_exists($key, $csv_tags)) {
                    $csv_tags[$key] = array();
                }
                $csv_tags[$key][] = trim($value);
            } elseif (array_key_exists('plain', $configuration) && in_array($key, $configuration['plain'])) {
                // returned plain (xml object)
                $return_array[$key] = $value;
            } elseif (array_key_exists('string', $configuration) && in_array($key, $configuration['string'])) {
                // returned as string
                $return_array[$key] = trim($value);
            } else {
                // an illegal or undefined category
            }
        }
    }
    foreach ($csv_tags as $key => $value) {
        $return_array[$key] = putcsv($value);
    }
    return $return_array;
}
Exemple #2
0
 public function compareToXML($xml, $implementation_id = false)
 {
     $relevant = array('name', 'external_version');
     $where = array();
     if ($implementation_id !== false) {
         $where['id'] = $implementation_id;
     }
     foreach ($relevant as $item) {
         if (property_exists($xml->children('oml', true), $item)) {
             if (in_array($item, array('creator', 'contributor'))) {
                 $where[$item] = putcsv(xml_array_to_plain_array($xml, $item));
             } else {
                 $where[$item] = trim($xml->children('oml', true)->{$item});
             }
         } else {
             $where[$item] = null;
         }
     }
     $candidates = $this->Implementation->getWhere($where);
     if (is_array($candidates)) {
         foreach ($candidates as $candidate) {
             $valid_duplicate = true;
             // TODO: Continue outerloop when false.
             // check parameters
             $params = $this->Input->getColumnFunctionWhere('count(*)', 'implementation_id = "' . $candidate->id . '"');
             if ($params[0] != xml_size($xml, 'parameter')) {
                 $valid_duplicate = false;
             }
             foreach ($xml->children('oml', true)->parameter as $p) {
                 if ($this->Input->compareToXML($p, $candidate->id) === false) {
                     $valid_duplicate = false;
                 }
             }
             // check bibrefs
             $bibrefs = $this->Bibliographical_reference->getColumnFunctionWhere('count(*)', 'implementation_id = "' . $candidate->id . '"');
             if ($bibrefs[0] != xml_size($xml, 'bibliographical_reference')) {
                 $valid_duplicate = false;
             }
             foreach ($xml->children('oml', true)->bibliographical_reference as $b) {
                 if ($this->Bibliographical_reference->compareToXML($b, $candidate->id) === false) {
                     $valid_duplicate = false;
                 }
             }
             // check components
             $components = $this->getComponents($candidate);
             if (count($components) != xml_size($xml, 'component')) {
                 $valid_duplicate = false;
             }
             foreach ($xml->children('oml', true)->component as $i) {
                 if ($this->Implementation_component->compareToXML($i, $candidate->id) === false) {
                     $valid_duplicate = false;
                 }
             }
             if ($valid_duplicate) {
                 // passed all checks :)
                 return $candidate->id;
             }
         }
     }
     // none of the implementations matched
     return false;
 }