Skip to content

Commit

Permalink
Migrate code to use class constants; fix unit tests
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/XML_Util2/trunk@313963 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
CloCkWeRX committed Jul 30, 2011
1 parent 4e714ce commit 60fa4bd
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 169 deletions.
198 changes: 99 additions & 99 deletions XML/Util2.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,82 +50,82 @@
require_once 'XML/Util2/Exception.php';

/**
* error code for invalid chars in XML name
*/
define('XML_UTIL2_ERROR_INVALID_CHARS', 51);
* utility class for working with XML documents
*
/**
* error code for invalid chars in XML name
* @category XML
* @package XML_Util2
* @author Stephan Schmidt <schst@php.net>
* @copyright 2003-2008 Stephan Schmidt <schst@php.net>
* @license http://opensource.org/licenses/bsd-license New BSD License
* @version Release: @version@
* @link http://pear.php.net/package/XML_Util2
*/
define('XML_UTIL2_ERROR_INVALID_START', 52);
class XML_Util2
{
/**
* error code for invalid chars in XML name
*/
const ERROR_INVALID_CHARS = 51;

/**
* error code for non-scalar tag content
*/
define('XML_UTIL2_ERROR_NON_SCALAR_CONTENT', 60);
/**
* error code for invalid chars in XML name
*/
const ERROR_INVALID_START = 52;

/**
* error code for missing tag name
*/
define('XML_UTIL2_ERROR_NO_TAG_NAME', 61);
/**
* error code for non-scalar tag content
*/
const ERROR_NON_SCALAR_CONTENT = 60;

/**
* replace XML entities
*/
define('XML_UTIL2_REPLACE_ENTITIES', 1);
/**
* error code for missing tag name
*/
const ERROR_NO_TAG_NAME = 61;

/**
* embedd content in a CData Section
*/
define('XML_UTIL2_CDATA_SECTION', 5);
/**
* replace XML entities
*/
const REPLACE_ENTITIES = 1;

/**
* do not replace entitites
*/
define('XML_UTIL2_ENTITIES_NONE', 0);
/**
* embedd content in a CData Section
*/
const CDATA_SECTION = 5;

/**
* replace all XML entitites
* This setting will replace <, >, ", ' and &
*/
define('XML_UTIL2_ENTITIES_XML', 1);
/**
* do not replace entitites
*/
const ENTITIES_NONE = 0;

/**
* replace only required XML entitites
* This setting will replace <, " and &
*/
define('XML_UTIL2_ENTITIES_XML_REQUIRED', 2);
/**
* replace all XML entitites
* This setting will replace <, >, ", ' and &
*/
const ENTITIES_XML = 1;

/**
* replace HTML entitites
* @link http://www.php.net/htmlentities
*/
define('XML_UTIL2_ENTITIES_HTML', 3);
/**
* replace only required XML entitites
* This setting will replace <, " and &
*/
const ENTITIES_XML_REQUIRED = 2;

/**
* Collapse all empty tags.
*/
define('XML_UTIL2_COLLAPSE_ALL', 1);
/**
* replace HTML entitites
* @link http://www.php.net/htmlentities
*/
const ENTITIES_HTML = 3;

/**
* Collapse only empty XHTML tags that have no end tag.
*/
define('XML_UTIL2_COLLAPSE_XHTML_ONLY', 2);
/**
* Collapse all empty tags.
*/
const COLLAPSE_ALL = 1;

/**
* utility class for working with XML documents
*
/**
* Collapse only empty XHTML tags that have no end tag.
*/
const COLLAPSE_XHTML_ONLY = 2;

* @category XML
* @package XML_Util2
* @author Stephan Schmidt <schst@php.net>
* @copyright 2003-2008 Stephan Schmidt <schst@php.net>
* @license http://opensource.org/licenses/bsd-license New BSD License
* @version Release: @version@
* @link http://pear.php.net/package/XML_Util2
*/
class XML_Util2
{
/**
* return API version
*
Expand Down Expand Up @@ -161,17 +161,17 @@ public function apiVersion()
* // replace XML entites in UTF-8:
* $string = $util->replaceEntities(
* 'This string contains < & > as well as ä, ö, ß, à and ê',
* XML_UTIL2_ENTITIES_HTML,
* XML_Util2::ENTITIES_HTML,
* 'UTF-8'
* );
* </code>
*
* @param string $string string where XML special chars
* should be replaced
* @param int $replaceEntities setting for entities in attribute values
* (one of XML_UTIL2_ENTITIES_XML,
* XML_UTIL2_ENTITIES_XML_REQUIRED,
* XML_UTIL2_ENTITIES_HTML)
* (one of XML_Util2::ENTITIES_XML,
* XML_Util2::ENTITIES_XML_REQUIRED,
* XML_Util2::ENTITIES_HTML)
* @param string $encoding encoding value (if any)...
* must be a valid encoding as determined
* by the htmlentities() function
Expand All @@ -180,25 +180,25 @@ public function apiVersion()
* @access public
* @see reverseEntities()
*/
public function replaceEntities($string, $replaceEntities = XML_UTIL2_ENTITIES_XML,
public function replaceEntities($string, $replaceEntities = XML_Util2::ENTITIES_XML,
$encoding = 'ISO-8859-1')
{
switch ($replaceEntities) {
case XML_UTIL2_ENTITIES_XML:
case XML_Util2::ENTITIES_XML:
return strtr($string, array(
'&' => '&amp;',
'>' => '&gt;',
'<' => '&lt;',
'"' => '&quot;',
'\'' => '&apos;' ));
break;
case XML_UTIL2_ENTITIES_XML_REQUIRED:
case XML_Util2::ENTITIES_XML_REQUIRED:
return strtr($string, array(
'&' => '&amp;',
'<' => '&lt;',
'"' => '&quot;' ));
break;
case XML_UTIL2_ENTITIES_HTML:
case XML_Util2::ENTITIES_HTML:
return htmlentities($string, ENT_COMPAT, $encoding);
break;
}
Expand Down Expand Up @@ -230,17 +230,17 @@ public function replaceEntities($string, $replaceEntities = XML_UTIL2_ENTITIES_X
* $string = $util->reverseEntities(
* 'This string contains &lt; &amp; &gt; as well as'
* . ' &auml;, &ouml;, &szlig;, &agrave; and &ecirc;',
* XML_UTIL2_ENTITIES_HTML,
* XML_Util2::ENTITIES_HTML,
* 'UTF-8'
* );
* </code>
*
* @param string $string string where XML special chars
* should be replaced
* @param int $replaceEntities setting for entities in attribute values
* (one of XML_UTIL2_ENTITIES_XML,
* XML_UTIL2_ENTITIES_XML_REQUIRED,
* XML_UTIL2_ENTITIES_HTML)
* (one of XML_Util2::ENTITIES_XML,
* XML_Util2::ENTITIES_XML_REQUIRED,
* XML_Util2::ENTITIES_HTML)
* @param string $encoding encoding value (if any)...
* must be a valid encoding as determined
* by the html_entity_decode() function
Expand All @@ -249,25 +249,25 @@ public function replaceEntities($string, $replaceEntities = XML_UTIL2_ENTITIES_X
* @access public
* @see replaceEntities()
*/
public function reverseEntities($string, $replaceEntities = XML_UTIL2_ENTITIES_XML,
public function reverseEntities($string, $replaceEntities = XML_Util2::ENTITIES_XML,
$encoding = 'ISO-8859-1')
{
switch ($replaceEntities) {
case XML_UTIL2_ENTITIES_XML:
case XML_Util2::ENTITIES_XML:
return strtr($string, array(
'&amp;' => '&',
'&gt;' => '>',
'&lt;' => '<',
'&quot;' => '"',
'&apos;' => '\'' ));
break;
case XML_UTIL2_ENTITIES_XML_REQUIRED:
case XML_Util2::ENTITIES_XML_REQUIRED:
return strtr($string, array(
'&amp;' => '&',
'&lt;' => '<',
'&quot;' => '"' ));
break;
case XML_UTIL2_ENTITIES_HTML:
case XML_Util2::ENTITIES_HTML:
return html_entity_decode($string, ENT_COMPAT, $encoding);
break;
}
Expand Down Expand Up @@ -382,18 +382,18 @@ public function getDocTypeDeclaration($root, $uri = null, $internalDtd = null)
* @param string $linebreak string used for linebreaks of
* multiline attributes
* @param int $entities setting for entities in attribute values
* (one of XML_UTIL2_ENTITIES_NONE,
* XML_UTIL2_ENTITIES_XML,
* XML_UTIL2_ENTITIES_XML_REQUIRED,
* XML_UTIL2_ENTITIES_HTML)
* (one of XML_Util2::ENTITIES_NONE,
* XML_Util2::ENTITIES_XML,
* XML_Util2::ENTITIES_XML_REQUIRED,
* XML_Util2::ENTITIES_HTML)
*
* @return string string representation of the attributes
* @access public
* @uses replaceEntities() to replace XML entities in attribute values
* @todo allow sort also to be an options array
*/
public function attributesToString($attributes, $sort = true, $multiline = false,
$indent = ' ', $linebreak = "\n", $entities = XML_UTIL2_ENTITIES_XML)
$indent = ' ', $linebreak = "\n", $entities = XML_Util2::ENTITIES_XML)
{
/*
* second parameter may be an array
Expand Down Expand Up @@ -424,9 +424,9 @@ public function attributesToString($attributes, $sort = true, $multiline = false
}
if ( !$multiline || count($attributes) == 1) {
foreach ($attributes as $key => $value) {
if ($entities != XML_UTIL2_ENTITIES_NONE) {
if ($entities === XML_UTIL2_CDATA_SECTION) {
$entities = XML_UTIL2_ENTITIES_XML;
if ($entities != XML_Util2::ENTITIES_NONE) {
if ($entities === XML_Util2::CDATA_SECTION) {
$entities = XML_Util2::ENTITIES_XML;
}
$value = $this->replaceEntities($value, $entities);
}
Expand All @@ -435,7 +435,7 @@ public function attributesToString($attributes, $sort = true, $multiline = false
} else {
$first = true;
foreach ($attributes as $key => $value) {
if ($entities != XML_UTIL2_ENTITIES_NONE) {
if ($entities != XML_Util2::ENTITIES_NONE) {
$value = $this->replaceEntities($value, $entities);
}
if ($first) {
Expand All @@ -454,17 +454,17 @@ public function attributesToString($attributes, $sort = true, $multiline = false
* Collapses empty tags.
*
* @param string $xml XML
* @param int $mode Whether to collapse all empty tags (XML_UTIL2_COLLAPSE_ALL)
* or only XHTML (XML_UTIL2_COLLAPSE_XHTML_ONLY) ones.
* @param int $mode Whether to collapse all empty tags (XML_Util2::COLLAPSE_ALL)
* or only XHTML (XML_Util2::COLLAPSE_XHTML_ONLY) ones.
*
* @return string XML
* @access public
* @todo PEAR CS - unable to avoid "space after open parens" error
* in the IF branch
*/
public function collapseEmptyTags($xml, $mode = XML_UTIL2_COLLAPSE_ALL)
public function collapseEmptyTags($xml, $mode = XML_Util2::COLLAPSE_ALL)
{
if ($mode == XML_UTIL2_COLLAPSE_XHTML_ONLY) {
if ($mode == XML_Util2::COLLAPSE_XHTML_ONLY) {
return preg_replace(
'/<(area|base(?:font)?|br|col|frame|hr|img|input|isindex|link|meta|'
. 'param)([^>]*)><\/\\1>/s',
Expand Down Expand Up @@ -514,7 +514,7 @@ public function collapseEmptyTags($xml, $mode = XML_UTIL2_COLLAPSE_ALL)
* @uses createTagFromArray() to create the tag
*/
public function createTag($qname, $attributes = array(), $content = null,
$namespaceUri = null, $replaceEntities = XML_UTIL2_REPLACE_ENTITIES,
$namespaceUri = null, $replaceEntities = XML_Util2::REPLACE_ENTITIES,
$multiline = false, $indent = '_auto', $linebreak = "\n",
$sortAttributes = true)
{
Expand Down Expand Up @@ -597,19 +597,19 @@ public function createTag($qname, $attributes = array(), $content = null,
* @uses createCDataSection()
* @uses raiseError()
*/
public function createTagFromArray($tag, $replaceEntities = XML_UTIL2_REPLACE_ENTITIES,
public function createTagFromArray($tag, $replaceEntities = XML_Util2::REPLACE_ENTITIES,
$multiline = false, $indent = '_auto', $linebreak = "\n",
$sortAttributes = true)
{
if (isset($tag['content']) && !is_scalar($tag['content'])) {
return $this->raiseError('Supplied non-scalar value as tag content',
XML_UTIL2_ERROR_NON_SCALAR_CONTENT);
XML_Util2::ERROR_NON_SCALAR_CONTENT);
}

if (!isset($tag['qname']) && !isset($tag['localPart'])) {
return $this->raiseError('You must either supply a qualified name '
. '(qname) or local tag name (localPart).',
XML_UTIL2_ERROR_NO_TAG_NAME);
XML_Util2::ERROR_NO_TAG_NAME);
}

// if no attributes hav been set, use empty attributes
Expand Down Expand Up @@ -668,9 +668,9 @@ public function createTagFromArray($tag, $replaceEntities = XML_UTIL2_REPLACE_EN
$tag = sprintf('<%s%s />', $tag['qname'], $attList);
} else {
switch ($replaceEntities) {
case XML_UTIL2_ENTITIES_NONE:
case XML_Util2::ENTITIES_NONE:
break;
case XML_UTIL2_CDATA_SECTION:
case XML_Util2::CDATA_SECTION:
$tag['content'] = $this->createCDataSection($tag['content']);
break;
default:
Expand Down Expand Up @@ -892,7 +892,7 @@ function isValidName($string)
// check for invalid chars
if (!preg_match('/^[[:alpha:]_]\\z/', $string{0})) {
return $this->raiseError('XML names may only start with letter '
. 'or underscore', XML_UTIL2_ERROR_INVALID_START);
. 'or underscore', XML_Util2::ERROR_INVALID_START);
}

// check for invalid chars
Expand All @@ -901,7 +901,7 @@ function isValidName($string)
) {
return $this->raiseError('XML names may only contain alphanumeric '
. 'chars, period, hyphen, colon and underscores',
XML_UTIL2_ERROR_INVALID_CHARS);
XML_Util2::ERROR_INVALID_CHARS);
}
// XML name is valid
return true;
Expand Down
4 changes: 2 additions & 2 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
);

print 'creating a tag with CData section:<br>';
print htmlentities($util->createTagFromArray($tag, XML_UTIL_CDATA_SECTION));
print htmlentities($util->createTagFromArray($tag, XML_Util2::CDATA_SECTION));
print "\n<br><br>\n";

/**
Expand All @@ -260,7 +260,7 @@
);

print 'creating a tag with HTML entities:<br>';
print htmlentities($util->createTagFromArray($tag, XML_UTIL_ENTITIES_HTML));
print htmlentities($util->createTagFromArray($tag, XML_Util2::ENTITIES_HTML));
print "\n<br><br>\n";

/**
Expand Down
Loading

0 comments on commit 60fa4bd

Please sign in to comment.