Exemplo n.º 1
0
        debug(TypeConverter::$method($xml));
    }
}
// Convert a complicated XML file to an array
$xml = file_get_contents('test.xml');
foreach (array('none', 'merge', 'group', 'overwrite') as $format) {
    debug('TypeConverter::xmlToArray(' . $format . ')');
    switch ($format) {
        case 'none':
            debug(TypeConverter::xmlToArray($xml, TypeConverter::XML_NONE));
            break;
        case 'merge':
            debug(TypeConverter::xmlToArray($xml, TypeConverter::XML_MERGE));
            break;
        case 'group':
            debug(TypeConverter::xmlToArray($xml, TypeConverter::XML_GROUP));
            break;
        case 'overwrite':
            debug(TypeConverter::xmlToArray($xml, TypeConverter::XML_OVERWRITE));
            break;
    }
}
// Convert UTF-8
$json = array('j\'étais', 'joué', '中文', 'éáíúűóüöäÍÓ');
debug($json);
debug(TypeConverter::utf8Encode($json));
debug(TypeConverter::utf8Decode(TypeConverter::utf8Encode($json)));
?>

</body>
</html>
Exemplo n.º 2
0
 /**
  * Processes the feed and rebuilds an array based on the feeds type (RSS, RDF, Atom).
  *
  * @param HttpSocketResponse $response
  * @param array $query
  * @param string $source
  * @return bool
  */
 protected function _process(HttpSocketResponse $response, $query, $source)
 {
     if (!$response->isOk()) {
         return array();
     }
     $feed = TypeConverter::toArray($response->body());
     $clean = array();
     if (!empty($query['root']) && !empty($feed[$query['feed']['root']])) {
         $items = $feed[$query['feed']['root']];
     } else {
         // RSS
         if (isset($feed['channel']) && isset($feed['channel']['item'])) {
             $items = $feed['channel']['item'];
             // RDF
         } else {
             if (isset($feed['item'])) {
                 $items = $feed['item'];
                 // Atom
             } else {
                 if (isset($feed['entry'])) {
                     $items = $feed['entry'];
                     // XML
                 } else {
                     $items = $feed;
                 }
             }
         }
     }
     if (empty($items) || !is_array($items)) {
         return $clean;
     }
     // Gather elements
     $elements = array('title' => array('title'), 'guid' => array('guid', 'id'), 'date' => array('date', 'pubDate', 'published', 'updated'), 'link' => array('link', 'origLink'), 'image' => array('image', 'thumbnail', 'enclosure'), 'author' => array('author', 'writer', 'editor', 'user'), 'source' => array('source'), 'description' => array('description', 'desc', 'summary', 'content', 'text'));
     if (is_array($query['fields'])) {
         $elements = array_merge_recursive($elements, $query['fields']);
     }
     // Loop the feed
     foreach ($items as $item) {
         $data = array();
         foreach ($elements as $element => $keys) {
             if (isset($keys['attributes'])) {
                 $attributes = $keys['attributes'];
                 unset($keys['attributes']);
             } else {
                 $attributes = array('value', 'href', 'src', 'name', 'label', 'url');
             }
             if (isset($keys['keys'])) {
                 $keys = $keys['keys'];
             }
             foreach ($keys as $key) {
                 if (isset($item[$key]) && empty($data[$element])) {
                     if ($value = $this->_extract($item[$key], $attributes)) {
                         $data[$element] = $value;
                         break;
                     }
                 }
             }
         }
         if (empty($data['link'])) {
             trigger_error(sprintf('Feed %s does not have a valid link element', $source), E_USER_NOTICE);
             continue;
         }
         if (empty($data['source']) && $source) {
             $data['source'] = (string) $source;
         }
         // Determine how to sort
         $sortBy = $query['feed']['sort'];
         if (isset($data[$sortBy])) {
             $sort = $data[$sortBy];
         } else {
             if (isset($data['date'])) {
                 $sort = $data['date'];
             } else {
                 $sort = null;
             }
         }
         if ($sortBy === 'date' && $sort) {
             $sort = strtotime($sort);
         } else {
             if (!$sort) {
                 $sort = microtime();
             }
         }
         if ($data) {
             $clean[$sort] = $data;
         }
     }
     return $clean;
 }
Exemplo n.º 3
0
 /**
  * Parse an xml string into a usable php structure
  */
 private function normalizeFromXml($name, $config, $input = '')
 {
     $doc = new SimpleXMLElement($this->filedata[$name]);
     $items = TypeConverter::xmlToArray($doc, TypeConverter::XML_MERGE);
     // get down into the root element
     $root = $config['target']['mapping']['root'];
     $elements = split('/', str_replace('//', '', $root));
     if ($elements[0] == 'response' && !$items['response']) {
         array_shift($elements);
     }
     if (array_key_exists('nodata', $items['response']) && is_array($items['response']['nodata'])) {
         $this->resourcedata[$name] = 'nodata';
         return $doc;
     }
     foreach ($elements as $elm) {
         $items = $items[$elm];
     }
     $items = $this->flattenZOHO($items);
     $this->resourcedata[$name] = $items;
     return $doc;
 }
Exemplo n.º 4
0
 /**
  * Format the response into the right content type.
  *
  * @param string $type
  * @return string
  */
 protected function _format($type)
 {
     $response = array('success' => $this->_success, 'data' => $this->_data);
     if ($this->_code) {
         $response['code'] = $this->_code;
     }
     switch (strtolower($type)) {
         case 'json':
             $format = TypeConverter::toJson($response);
             break;
         case 'xml':
             $format = TypeConverter::toXml($response);
             break;
         case 'html':
         case 'text':
         default:
             $format = (string) $this->_data;
             break;
     }
     return $format;
 }
Exemplo n.º 5
0
 public function convert(array $data)
 {
     return str_replace(PHP_EOL, '', TypeConverter::toXml($data));
 }