Esempio n. 1
0
 /**
  * create a tag
  *
  * This method will call Util::createTagFromArray(), which
  * is more flexible.
  *
  * <code>
  * require_once 'XML/Util.php';
  *
  * // create an XML tag:
  * $tag = Util::createTag('myNs:myTag', 
  *     array('foo' => 'bar'), 
  *     'This is inside the tag', 
  *     'http://www.w3c.org/myNs#');
  * </code>
  *
  * @param string $qname           qualified tagname (including namespace)
  * @param array  $attributes      array containg attributes
  * @param mixed  $content         the content
  * @param string $namespaceUri    URI of the namespace
  * @param int    $replaceEntities whether to replace XML special chars in 
  *                                content, embedd it in a CData section 
  *                                or none of both
  * @param bool   $multiline       whether to create a multiline tag where 
  *                                each attribute gets written to a single line
  * @param string $indent          string used to indent attributes 
  *                                (_auto indents attributes so they start 
  *                                at the same column)
  * @param string $linebreak       string used for linebreaks
  * @param bool   $sortAttributes  Whether to sort the attributes or not
  *
  * @return string XML tag
  * @access public
  * @static
  * @see createTagFromArray()
  * @uses createTagFromArray() to create the tag
  */
 public static function createTag($qname, $attributes = array(), $content = null, $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, $multiline = false, $indent = '_auto', $linebreak = "\n", $sortAttributes = true)
 {
     $tag = array('qname' => $qname, 'attributes' => $attributes);
     // add tag content
     if ($content !== null) {
         $tag['content'] = $content;
     }
     // add namespace Uri
     if ($namespaceUri !== null) {
         $tag['namespaceUri'] = $namespaceUri;
     }
     return Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $linebreak, $sortAttributes);
 }
Esempio n. 2
0
 /**
  * create a tag from an array
  * this method awaits an array in the following format
  * array(
  *       'qname'        => $tagName,
  *       'attributes'   => array(),
  *       'content'      => $content,      // optional
  *       'namespace'    => $namespace     // optional
  *       'namespaceUri' => $namespaceUri  // optional
  *   )
  *
  * @param array   $tag       tag definition
  * @param boolean $firstCall whether or not this is the first call
  *
  * @return string $string XML tag
  * @access private
  */
 function _createXMLTag($tag, $firstCall = true)
 {
     // build fully qualified tag name
     if ($this->options[XML_SERIALIZER_OPTION_NAMESPACE] !== null) {
         if (is_array($this->options[XML_SERIALIZER_OPTION_NAMESPACE])) {
             $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE][0] . ':' . $tag['qname'];
         } else {
             $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE] . ':' . $tag['qname'];
         }
     }
     // attribute indentation
     if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] !== false) {
         $multiline = true;
         $indent = str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
         if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] == '_auto') {
             $indent .= str_repeat(' ', strlen($tag['qname']) + 2);
         } else {
             $indent .= $this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES];
         }
     } else {
         $multiline = false;
         $indent = false;
     }
     if (is_array($tag['content'])) {
         if (empty($tag['content'])) {
             $tag['content'] = '';
         }
     } elseif (XML_SERIALIZER_OPTION_FALSE_AS_STRING && $tag['content'] === false) {
         $tag['content'] = '0';
     } elseif (is_scalar($tag['content']) && (string) $tag['content'] == '') {
         $tag['content'] = '';
     }
     // replace XML entities
     if ($firstCall === true) {
         if ($this->options[XML_SERIALIZER_OPTION_CDATA_SECTIONS] === true) {
             $replaceEntities = XML_UTIL_CDATA_SECTION;
         } else {
             $replaceEntities = $this->options[XML_SERIALIZER_OPTION_ENTITIES];
         }
     } else {
         // this is a nested call, so value is already encoded
         // and must not be encoded again
         $replaceEntities = XML_SERIALIZER_ENTITIES_NONE;
         // but attributes need to be encoded anyways
         // (done here because the rest of the code assumes the same encoding
         // can be used both for attributes and content)
         foreach ($tag['attributes'] as $k => $v) {
             $v = Util::replaceEntities($v, $this->options[XML_SERIALIZER_OPTION_ENTITIES]);
             $tag['attributes'][$k] = $v;
         }
     }
     if (is_scalar($tag['content']) || is_null($tag['content'])) {
         if ($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC]) {
             if ($firstCall === true) {
                 $tag['content'] = call_user_func($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['content']);
             }
             $tag['attributes'] = array_map($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['attributes']);
         }
         $tag = Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]);
     } elseif (is_array($tag['content'])) {
         $tag = $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']);
     } elseif (is_object($tag['content'])) {
         $tag = $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']);
     } elseif (is_resource($tag['content'])) {
         settype($tag['content'], 'string');
         if ($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC]) {
             if ($replaceEntities === true) {
                 $tag['content'] = call_user_func($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['content']);
             }
             $tag['attributes'] = array_map($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['attributes']);
         }
         $tag = Util::createTagFromArray($tag, $replaceEntities);
     }
     return $tag;
 }