示例#1
0
function fn_yml_array_to_yml($data, $level = 0)
{
    if (!is_array($data)) {
        return $data;
    }
    $return = '';
    foreach ($data as $key => $value) {
        $attr = '';
        if (is_array($value) && is_numeric(key($value))) {
            foreach ($value as $k => $v) {
                $arr = array($key => $v);
                $return .= fn_array_to_xml($arr);
                unset($value[$k]);
            }
            unset($data[$key]);
            continue;
        }
        if (strpos($key, '@') !== false) {
            $data = explode('@', $key);
            $key = $data[0];
            unset($data[0]);
            if (count($data) > 0) {
                foreach ($data as $prop) {
                    if (strpos($prop, '=') !== false) {
                        $prop = explode('=', $prop);
                        $attr .= ' ' . $prop[0] . '="' . $prop[1] . '"';
                    } else {
                        $attr .= ' ' . $prop . '=""';
                    }
                }
            }
        }
        if (strpos($key, '+') !== false) {
            list($key) = explode('+', $key, 2);
        }
        $tab = str_repeat('    ', $level);
        if (empty($value)) {
            $return .= $tab . "<" . $key . $attr . "/>\n";
        } elseif (is_array($value)) {
            $return .= $tab . "<" . $key . $attr . ">\n" . fn_yml_array_to_yml($value, $level + 1) . '</' . $key . ">\n";
        } else {
            $return .= $tab . "<" . $key . $attr . '>' . fn_yml_array_to_yml($value, $level + 1) . '</' . $key . ">\n";
        }
    }
    return $return;
}
示例#2
0
 protected function head($file)
 {
     $yml2_header = array('<?xml version="1.0" encoding="' . $this->options['export_encoding'] . '"?>', '<!DOCTYPE yml2_catalog SYSTEM "shops.dtd">', '<yml_catalog date="' . date('Y-m-d G:i') . '">', '<shop>');
     $yml2_data = array('name' => $this->getShopName(), 'company' => str_replace('&', '&amp;', Registry::get('settings.Company.company_name')), 'url' => Registry::get('config.http_location'), 'platform' => PRODUCT_NAME, 'version' => PRODUCT_VERSION, 'agency' => 'Agency', 'email' => Registry::get('settings.Company.company_orders_department'));
     $this->buildCurrencies($yml2_data);
     $this->buildCategories($yml2_data);
     if (!fn_is_empty($this->options['delivery_options'])) {
         foreach ($this->options['delivery_options'] as $option) {
             $option_attr = 'option@cost=' . $option['cost'] . '@days=' . $option['days'];
             if (!empty($option['order_before'])) {
                 $option_attr .= '@order-before=' . $option['order_before'];
             }
             $yml2_data['delivery-options'][$option_attr] = '';
         }
     }
     fwrite($file, implode(PHP_EOL, $yml2_header) . PHP_EOL);
     fwrite($file, fn_yml_array_to_yml($yml2_data));
     fwrite($file, '<offers>' . PHP_EOL);
 }