diff --git a/XML/Util.php b/XML/Util.php index 6895e10..e3dbdea 100644 --- a/XML/Util.php +++ b/XML/Util.php @@ -1,7 +1,4 @@ , ", ' and & */ define('XML_UTIL_ENTITIES_XML', 1); /** - * replace only required XML entitites + * Replace only required XML entitites * This setting will replace <, " and & */ define('XML_UTIL_ENTITIES_XML_REQUIRED', 2); /** - * replace HTML entitites + * Replace HTML entitites * @link http://www.php.net/htmlentities */ define('XML_UTIL_ENTITIES_HTML', 3); @@ -111,33 +108,30 @@ define('XML_UTIL_COLLAPSE_ALL', 1); define('XML_UTIL_COLLAPSE_XHTML_ONLY', 2); /** - * utility class for working with XML documents + * Utility class for working with XML documents * - * @category XML * @package XML_Util * @author Stephan Schmidt * @copyright 2003-2008 Stephan Schmidt * @license http://opensource.org/licenses/bsd-license New BSD License - * @version Release: 1.2.3 + * @version Release: 1.3.0 * @link http://pear.php.net/package/XML_Util */ class XML_Util { /** - * return API version + * Return API version * * @return string $version API version - * @access public - * @static */ - function apiVersion() + public static function apiVersion() { return '1.1'; } /** - * replace XML entities + * Replace XML entities * * With the optional second parameter, you may select, which * entities should be replaced. @@ -172,27 +166,33 @@ class XML_Util * by the htmlentities() function * * @return string string with replaced chars - * @access public - * @static - * @see reverseEntities() + * @see reverseEntities() */ - function replaceEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML, - $encoding = 'ISO-8859-1') - { + public static function replaceEntities( + $string, $replaceEntities = XML_UTIL_ENTITIES_XML, $encoding = 'ISO-8859-1' + ) { switch ($replaceEntities) { case XML_UTIL_ENTITIES_XML: - return strtr($string, array( - '&' => '&', - '>' => '>', - '<' => '<', - '"' => '"', - '\'' => ''' )); + return strtr( + $string, + array( + '&' => '&', + '>' => '>', + '<' => '<', + '"' => '"', + '\'' => ''' + ) + ); break; case XML_UTIL_ENTITIES_XML_REQUIRED: - return strtr($string, array( - '&' => '&', - '<' => '<', - '"' => '"' )); + return strtr( + $string, + array( + '&' => '&', + '<' => '<', + '"' => '"' + ) + ); break; case XML_UTIL_ENTITIES_HTML: return htmlentities($string, ENT_COMPAT, $encoding); @@ -202,7 +202,7 @@ class XML_Util } /** - * reverse XML entities + * Reverse XML entities * * With the optional second parameter, you may select, which * entities should be reversed. @@ -238,27 +238,33 @@ class XML_Util * by the html_entity_decode() function * * @return string string with replaced chars - * @access public - * @static - * @see replaceEntities() + * @see replaceEntities() */ - function reverseEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML, - $encoding = 'ISO-8859-1') - { + public static function reverseEntities( + $string, $replaceEntities = XML_UTIL_ENTITIES_XML, $encoding = 'ISO-8859-1' + ) { switch ($replaceEntities) { case XML_UTIL_ENTITIES_XML: - return strtr($string, array( - '&' => '&', - '>' => '>', - '<' => '<', - '"' => '"', - ''' => '\'' )); + return strtr( + $string, + array( + '&' => '&', + '>' => '>', + '<' => '<', + '"' => '"', + ''' => '\'' + ) + ); break; case XML_UTIL_ENTITIES_XML_REQUIRED: - return strtr($string, array( - '&' => '&', - '<' => '<', - '"' => '"' )); + return strtr( + $string, + array( + '&' => '&', + '<' => '<', + '"' => '"' + ) + ); break; case XML_UTIL_ENTITIES_HTML: return html_entity_decode($string, ENT_COMPAT, $encoding); @@ -268,7 +274,7 @@ class XML_Util } /** - * build an xml declaration + * Build an xml declaration * * * require_once 'XML/Util.php'; @@ -282,13 +288,12 @@ class XML_Util * @param bool $standalone document is standalone (or not) * * @return string xml declaration - * @access public - * @static - * @uses attributesToString() to serialize the attributes of the XML declaration + * @uses attributesToString() to serialize the attributes of the + * XML declaration */ - function getXMLDeclaration($version = '1.0', $encoding = null, - $standalone = null) - { + public static function getXMLDeclaration( + $version = '1.0', $encoding = null, $standalone = null + ) { $attributes = array( 'version' => $version, ); @@ -301,12 +306,14 @@ class XML_Util $attributes['standalone'] = $standalone ? 'yes' : 'no'; } - return sprintf('', - XML_Util::attributesToString($attributes, false)); + return sprintf( + '', + XML_Util::attributesToString($attributes, false) + ); } /** - * build a document type declaration + * Build a document type declaration * * * require_once 'XML/Util.php'; @@ -321,12 +328,11 @@ class XML_Util * @param string $internalDtd internal dtd entries * * @return string doctype declaration - * @access public - * @static - * @since 0.2 + * @since 0.2 */ - function getDocTypeDeclaration($root, $uri = null, $internalDtd = null) - { + public static function getDocTypeDeclaration( + $root, $uri = null, $internalDtd = null + ) { if (is_array($uri)) { $ref = sprintf(' PUBLIC "%s" "%s"', $uri['id'], $uri['uri']); } elseif (!empty($uri)) { @@ -343,7 +349,7 @@ class XML_Util } /** - * create string representation of an attribute list + * Create string representation of an attribute list * * * require_once 'XML/Util.php'; @@ -375,14 +381,13 @@ class XML_Util * XML_UTIL_ENTITIES_HTML) * * @return string string representation of the attributes - * @access public - * @static - * @uses replaceEntities() to replace XML entities in attribute values - * @todo allow sort also to be an options array + * @uses replaceEntities() to replace XML entities in attribute values + * @todo allow sort also to be an options array */ - function attributesToString($attributes, $sort = true, $multiline = false, - $indent = ' ', $linebreak = "\n", $entities = XML_UTIL_ENTITIES_XML) - { + public static function attributesToString( + $attributes, $sort = true, $multiline = false, + $indent = ' ', $linebreak = "\n", $entities = XML_UTIL_ENTITIES_XML + ) { /* * second parameter may be an array */ @@ -410,7 +415,7 @@ class XML_Util if ($sort) { ksort($attributes); } - if ( !$multiline || count($attributes) == 1) { + if (!$multiline || count($attributes) == 1) { foreach ($attributes as $key => $value) { if ($entities != XML_UTIL_ENTITIES_NONE) { if ($entities === XML_UTIL_CDATA_SECTION) { @@ -446,26 +451,23 @@ class XML_Util * or only XHTML (XML_UTIL_COLLAPSE_XHTML_ONLY) ones. * * @return string XML - * @access public - * @static - * @todo PEAR CS - unable to avoid "space after open parens" error - * in the IF branch */ - function collapseEmptyTags($xml, $mode = XML_UTIL_COLLAPSE_ALL) + public static function collapseEmptyTags($xml, $mode = XML_UTIL_COLLAPSE_ALL) { if ($mode == XML_UTIL_COLLAPSE_XHTML_ONLY) { return preg_replace( '/<(area|base(?:font)?|br|col|frame|hr|img|input|isindex|link|meta|' . 'param)([^>]*)><\/\\1>/s', '<\\1\\2 />', - $xml); + $xml + ); } else { return preg_replace('/<(\w+)([^>]*)><\/\\1>/s', '<\\1\\2 />', $xml); } } /** - * create a tag + * Create a tag * * This method will call XML_Util::createTagFromArray(), which * is more flexible. @@ -496,16 +498,15 @@ class XML_Util * @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 + * @see createTagFromArray() + * @uses createTagFromArray() to create the tag */ - function createTag($qname, $attributes = array(), $content = null, + public static function createTag( + $qname, $attributes = array(), $content = null, $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, $multiline = false, $indent = '_auto', $linebreak = "\n", - $sortAttributes = true) - { + $sortAttributes = true + ) { $tag = array( 'qname' => $qname, 'attributes' => $attributes @@ -521,13 +522,15 @@ class XML_Util $tag['namespaceUri'] = $namespaceUri; } - return XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, - $indent, $linebreak, $sortAttributes); + return XML_Util::createTagFromArray( + $tag, $replaceEntities, $multiline, + $indent, $linebreak, $sortAttributes + ); } /** - * create a tag from an array - * this method awaits an array in the following format + * Create a tag from an array. + * This method awaits an array in the following format *
      * array(
      *     // qualified name of the tag
@@ -576,27 +579,31 @@ class XML_Util
      * @param bool   $sortAttributes  Whether to sort the attributes or not
      *
      * @return string XML tag
-     * @access public
-     * @static
-     * @see createTag()
+     *
+     * @see  createTag()
      * @uses attributesToString() to serialize the attributes of the tag
      * @uses splitQualifiedName() to get local part and namespace of a qualified name
      * @uses createCDataSection()
      * @uses raiseError()
      */
-    function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES,
+    public static function createTagFromArray(
+        $tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES,
         $multiline = false, $indent = '_auto', $linebreak = "\n",
-        $sortAttributes = true)
-    {
+        $sortAttributes = true
+    ) {
         if (isset($tag['content']) && !is_scalar($tag['content'])) {
-            return XML_Util::raiseError('Supplied non-scalar value as tag content',
-            XML_UTIL_ERROR_NON_SCALAR_CONTENT);
+            return XML_Util::raiseError(
+                'Supplied non-scalar value as tag content',
+                XML_UTIL_ERROR_NON_SCALAR_CONTENT
+            );
         }
 
         if (!isset($tag['qname']) && !isset($tag['localPart'])) {
-            return XML_Util::raiseError('You must either supply a qualified name '
+            return XML_Util::raiseError(
+                'You must either supply a qualified name '
                 . '(qname) or local tag name (localPart).',
-                XML_UTIL_ERROR_NO_TAG_NAME);
+                XML_UTIL_ERROR_NO_TAG_NAME
+            );
         }
 
         // if no attributes hav been set, use empty attributes
@@ -633,8 +640,8 @@ class XML_Util
         if (isset($tag['namespaceUri']) && !empty($tag['namespaceUri'])) {
             // is a namespace given
             if (isset($tag['namespace']) && !empty($tag['namespace'])) {
-                $tag['attributes']['xmlns:' . $tag['namespace']] =
-                    $tag['namespaceUri'];
+                $tag['attributes']['xmlns:' . $tag['namespace']]
+                    = $tag['namespaceUri'];
             } else {
                 // define this Uri as the default namespace
                 $tag['attributes']['xmlns'] = $tag['namespaceUri'];
@@ -649,8 +656,10 @@ class XML_Util
         }
 
         // create attribute list
-        $attList = XML_Util::attributesToString($tag['attributes'],
-            $sortAttributes, $multiline, $indent, $linebreak);
+        $attList = XML_Util::attributesToString(
+            $tag['attributes'],
+            $sortAttributes, $multiline, $indent, $linebreak
+        );
         if (!isset($tag['content']) || (string)$tag['content'] == '') {
             $tag = sprintf('<%s%s />', $tag['qname'], $attList);
         } else {
@@ -661,18 +670,21 @@ class XML_Util
                 $tag['content'] = XML_Util::createCDataSection($tag['content']);
                 break;
             default:
-                $tag['content'] = XML_Util::replaceEntities($tag['content'],
-                    $replaceEntities);
+                $tag['content'] = XML_Util::replaceEntities(
+                    $tag['content'], $replaceEntities
+                );
                 break;
             }
-            $tag = sprintf('<%s%s>%s', $tag['qname'], $attList, $tag['content'],
-                $tag['qname']);
+            $tag = sprintf(
+                '<%s%s>%s', $tag['qname'], $attList, $tag['content'],
+                $tag['qname']
+            );
         }
         return $tag;
     }
 
     /**
-     * create a start element
+     * Create a start element
      *
      * 
      * require_once 'XML/Util.php';
@@ -693,14 +705,13 @@ class XML_Util
      * @param bool   $sortAttributes Whether to sort the attributes or not
      *
      * @return string XML start element
-     * @access public
-     * @static
-     * @see createEndElement(), createTag()
+     * @see    createEndElement(), createTag()
      */
-    function createStartElement($qname, $attributes = array(), $namespaceUri = null,
+    public static function createStartElement(
+        $qname, $attributes = array(), $namespaceUri = null,
         $multiline = false, $indent = '_auto', $linebreak = "\n",
-        $sortAttributes = true)
-    {
+        $sortAttributes = true
+    ) {
         // if no attributes hav been set, use empty attributes
         if (!isset($attributes) || !is_array($attributes)) {
             $attributes = array();
@@ -728,14 +739,16 @@ class XML_Util
         }
 
         // create attribute list
-        $attList = XML_Util::attributesToString($attributes, $sortAttributes,
-            $multiline, $indent, $linebreak);
+        $attList = XML_Util::attributesToString(
+            $attributes, $sortAttributes,
+            $multiline, $indent, $linebreak
+        );
         $element = sprintf('<%s%s>', $qname, $attList);
         return  $element;
     }
 
     /**
-     * create an end element
+     * Create an end element
      *
      * 
      * require_once 'XML/Util.php';
@@ -747,18 +760,16 @@ class XML_Util
      * @param string $qname qualified tagname (including namespace)
      *
      * @return string XML end element
-     * @access public
-     * @static
-     * @see createStartElement(), createTag()
+     * @see    createStartElement(), createTag()
      */
-    function createEndElement($qname)
+    public static function createEndElement($qname)
     {
         $element = sprintf('', $qname);
         return $element;
     }
 
     /**
-     * create an XML comment
+     * Create an XML comment
      *
      * 
      * require_once 'XML/Util.php';
@@ -770,17 +781,15 @@ class XML_Util
      * @param string $content content of the comment
      *
      * @return string XML comment
-     * @access public
-     * @static
      */
-    function createComment($content)
+    public static function createComment($content)
     {
         $comment = sprintf('', $content);
         return $comment;
     }
 
     /**
-     * create a CData section
+     * Create a CData section
      *
      * 
      * require_once 'XML/Util.php';
@@ -792,18 +801,17 @@ class XML_Util
      * @param string $data data of the CData section
      *
      * @return string CData section with content
-     * @access public
-     * @static
      */
-    function createCDataSection($data)
+    public static function createCDataSection($data)
     {
-        return sprintf('',
-            preg_replace('/\]\]>/', ']]]]>', strval($data)));
-
+        return sprintf(
+            '',
+            preg_replace('/\]\]>/', ']]]]>', strval($data))
+        );
     }
 
     /**
-     * split qualified name and return namespace and local part
+     * Split qualified name and return namespace and local part
      *
      * 
      * require_once 'XML/Util.php';
@@ -823,10 +831,8 @@ class XML_Util
      * @param string $defaultNs default namespace (optional)
      *
      * @return array array containing namespace and local part
-     * @access public
-     * @static
      */
-    function splitQualifiedName($qname, $defaultNs = null)
+    public static function splitQualifiedName($qname, $defaultNs = null)
     {
         if (strstr($qname, ':')) {
             $tmp = explode(':', $qname);
@@ -842,7 +848,7 @@ class XML_Util
     }
 
     /**
-     * check, whether string is valid XML name
+     * Check, whether string is valid XML name
      *
      * 

XML names are used for tagname, attribute names and various * other, lesser known entities.

@@ -863,33 +869,39 @@ class XML_Util * @param string $string string that should be checked * * @return mixed true, if string is a valid XML name, PEAR error otherwise - * @access public - * @static + * * @todo support for other charsets * @todo PEAR CS - unable to avoid 85-char limit on second preg_match */ - function isValidName($string) + public static function isValidName($string) { // check for invalid chars if (!preg_match('/^[[:alpha:]_]\\z/', $string{0})) { - return XML_Util::raiseError('XML names may only start with letter ' - . 'or underscore', XML_UTIL_ERROR_INVALID_START); + return XML_Util::raiseError( + 'XML names may only start with letter or underscore', + XML_UTIL_ERROR_INVALID_START + ); } // check for invalid chars - if (!preg_match('/^([[:alpha:]_]([[:alnum:]\-\.]*)?:)?[[:alpha:]_]([[:alnum:]\_\-\.]+)?\\z/', - $string) - ) { - return XML_Util::raiseError('XML names may only contain alphanumeric ' + $match = preg_match( + '/^([[:alpha:]_]([[:alnum:]\-\.]*)?:)?' + . '[[:alpha:]_]([[:alnum:]\_\-\.]+)?\\z/', + $string + ); + if (!$match) { + return XML_Util::raiseError( + 'XML names may only contain alphanumeric ' . 'chars, period, hyphen, colon and underscores', - XML_UTIL_ERROR_INVALID_CHARS); + XML_UTIL_ERROR_INVALID_CHARS + ); } // XML name is valid return true; } /** - * replacement for XML_Util::raiseError + * Replacement for XML_Util::raiseError * * Avoids the necessity to always require * PEAR.php @@ -898,13 +910,11 @@ class XML_Util * @param int $code error code * * @return PEAR_Error - * @access public - * @static - * @todo PEAR CS - should this use include_once instead? + * @todo PEAR CS - should this use include_once instead? */ - function raiseError($msg, $code) + public static function raiseError($msg, $code) { - require_once 'PEAR.php'; + include_once 'PEAR.php'; return PEAR::raiseError($msg, $code); } }