Exemplo n.º 1
0
    /**
     * Populate a single simple xml node (leaf).
     * 
     * @param array            $data
     * @param string           $fullKey
     * @param CourseXMLElement $parent
     */
    private function populateLeaf(&$data, $fullKey, $parent) {
        $fullKeyNoLang = $fullKey;
        if ($this->getAttribute('lang')) {
            $fullKey .= '_' . $this->getAttribute('lang');
        }

        if (isset($data[$fullKey])) {
            if (!is_array($data[$fullKey])) {
                if (in_array($fullKeyNoLang, CourseXMLConfig::$integerFields)) {
                    $this->{0} = intval($data[$fullKey]);
                } else if (in_array($fullKeyNoLang, CourseXMLConfig::$floatFields)) {
                    $this->{0} = CourseXMLConfig::getFloat($data[$fullKey]);
                } else {
                    $this->{0} = $data[$fullKey];
                }

                // mime attribute for mime fields
                if (in_array($fullKeyNoLang, CourseXMLConfig::$binaryFields)) {
                    $this['mime'] = isset($data[$fullKey . '_mime']) ? $data[$fullKey . '_mime'] : '';
                }
            } else {
                // multiple entities (multiEnum, multiFields and units) use associative indexed arrays
                if (in_array($fullKeyNoLang, CourseXMLConfig::$multiEnumerationFields)) {
                    // multiEnums are just comma separated
                    $this->{0} = implode(',', $data[$fullKey]);
                } else if (in_array($fullKeyNoLang, CourseXMLConfig::$multipleFields)) {
                    // multiplicity fields
                    if ($parent !== null) {
                        $name = $this->getName();
                        // calc index to locate the proper child
                        $i = 0;
                        if (isset($data[$fullKey . '_walked'])) {
                            $i = intval($data[$fullKey . '_walked']) + 1;
                        }
                        // this part is walked n independent times, where n = count($data[$fullKey])
                        // for each walking, we have to remember which was the previous index
                        // and assign the next array value to the (next) proper parent element
                        if ($i < count($data[$fullKey])) {
                            if (in_array($fullKeyNoLang, CourseXMLConfig::$integerFields)) {
                                $parent->{$name}[$i] = intval($data[$fullKey][$i]);
                            } else if (in_array($fullKeyNoLang, CourseXMLConfig::$floatFields)) {
                                $parent->{$name}[$i] = CourseXMLConfig::getFloat($data[$fullKey][$i]);
                            } else {
                                $parent->{$name}[$i] = $data[$fullKey][$i];
                            }
                            // mime attribute for mime fields
                            if (in_array($fullKeyNoLang, CourseXMLConfig::$binaryFields)) {
                                $parent->{$name}[$i]['mime'] = isset($data[$fullKey . '_mime'][$i]) ? $data[$fullKey . '_mime'][$i] : '';
                            }
                            // store index for locating the proper child at the next iteration
                            $data[$fullKey . '_walked'] = $i;
                        }
                    }
                } else if (in_array($fullKeyNoLang, CourseXMLConfig::$arrayFields)) {
                    if ($parent !== null) {
                        $name = $this->getName();
                        // calc index to locate the proper child
                        $j = 0;
                        if (isset($data[$fullKey . '_walked'])) {
                            $j = intval($data[$fullKey . '_walked']) + 1;
                        }
                        // this part is walked n independent times, where n = count($data[$fullKey])
                        // for each walking, we have to remember which was the previous index
                        // and assign the next array value to the (next) proper parent element
                        if ($j < count($data[$fullKey])) {
                            if (in_array($fullKeyNoLang, CourseXMLConfig::$integerFields)) {
                                $this->{0} = intval($data[$fullKey][$j]);
                            } else if (in_array($fullKeyNoLang, CourseXMLConfig::$floatFields)) {
                                $this->{0} = CourseXMLConfig::getFloat($data[$fullKey][$j]);
                            } else {
                                $this->{0} = $data[$fullKey][$j];
                            }
                            // mime attribute for mime fields 
                            if (in_array($fullKeyNoLang, CourseXMLConfig::$binaryFields)) {
                                $this['mime'] = isset($data[$fullKey . '_mime']) ? $data[$fullKey . '_mime'][$j] : '';
                            }
                            // store index for locating the proper child at the next iteration
                            $data[$fullKey . '_walked'] = $j;
                        }
                    }
                } else { // units
                    $index = intval($this->getAttribute('index')) - 1;
                    if ($index >= 0 && isset($data[$fullKey][$index])) {
                        $this->{0} = $data[$fullKey][$index];
                        unset($this['index']); // remove attribute
                    }
                }
            }
        }
    }