Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replaced _xml_entities() with functions by Dom Hastings, added lots o…

…f tests to verify. Closes #10
  • Loading branch information...
commit ec52c2bca7d8efcc4ffdbb34c91c225a1a151414 1 parent 25555c4
@jbroadway authored
Showing with 228 additions and 11 deletions.
  1. +141 −10 ActiveResource.php
  2. +87 −1 tests/ActiveResource.php
View
151 ActiveResource.php
@@ -327,17 +327,148 @@ function _build_xml ($k, $v) {
}
/**
- * Converts entities to unicode entities (ie. < becomes &#60;).
- * From php.net/htmlentities comments, user "webwurst at web dot de"
- */
- function _xml_entities ($string) {
- $trans = get_html_translation_table (HTML_ENTITIES);
-
- foreach ($trans as $key => $value) {
- $trans[$key] = '&#' . ord ($key) . ';';
+ * Returns the unicode value of the string
+ *
+ * @param string $c The source string
+ * @param integer $i The index to get the char from (passed by reference for use in a loop)
+ * @return integer The value of the char at $c[$i]
+ * @author kerry at shetline dot com
+ * @author Dom Hastings - modified to suit my needs
+ * @see http://www.php.net/manual/en/function.ord.php#78032
+ */
+ function _unicode_ord(&$c, &$i = 0) {
+ // get the character length
+ $l = strlen($c);
+ // copy the offset
+ $index = $i;
+
+ // check it's a valid offset
+ if ($index >= $l) {
+ return false;
}
-
- return strtr ($string, $trans);
+
+ // check the value
+ $o = ord($c[$index]);
+
+ // if it's ascii
+ if ($o <= 0x7F) {
+ return $o;
+
+ // not sure what it is...
+ } elseif ($o < 0xC2) {
+ return false;
+
+ // if it's a two-byte character
+ } elseif ($o <= 0xDF && $index < $l - 1) {
+ $i += 1;
+ return ($o & 0x1F) << 6 | (ord($c[$index + 1]) & 0x3F);
+
+ // three-byte
+ } elseif ($o <= 0xEF && $index < $l - 2) {
+ $i += 2;
+ return ($o & 0x0F) << 12 | (ord($c[$index + 1]) & 0x3F) << 6 | (ord($c[$index + 2]) & 0x3F);
+
+ // four-byte
+ } elseif ($o <= 0xF4 && $index < $l - 3) {
+ $i += 3;
+ return ($o & 0x0F) << 18 | (ord($c[$index + 1]) & 0x3F) << 12 | (ord($c[$index + 2]) & 0x3F) << 6 | (ord($c[$index + 3]) & 0x3F);
+
+ // not sure what it is...
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Makes the specified string XML-safe
+ *
+ * @param string $s
+ * @param boolean $hex Whether or not to make hexadecimal entities (as opposed to decimal)
+ * @return string The XML-safe result
+ * @author Dom Hastings
+ * @see http://www.w3.org/TR/REC-xml/#sec-predefined-ent
+ */
+ function _xml_entities ($s, $hex = true) {
+ // if the string is empty
+ if (empty($s)) {
+ // just return it
+ return $s;
+ }
+
+ // create the return string
+ $r = '';
+ // get the length
+ $l = strlen($s);
+
+ // iterate the string
+ for ($i = 0; $i < $l; $i++) {
+ // get the value of the character
+ $o = $this->_unicode_ord($s, $i);
+
+ // valid cahracters
+ $v = (
+ // \t \n <vertical tab> <form feed> \r
+ ($o >= 9 && $o <= 13) ||
+ // <space> !
+ ($o == 32) || ($o == 33) ||
+ // # $ %
+ ($o >= 35 && $o <= 37) ||
+ // ( ) * + , - . /
+ ($o >= 40 && $o <= 47) ||
+ // numbers
+ ($o >= 48 && $o <= 57) ||
+ // : ;
+ ($o == 58) || ($o == 59) ||
+ // = ?
+ ($o == 61) || ($o == 63) ||
+ // @
+ ($o == 64) ||
+ // uppercase
+ ($o >= 65 && $o <= 90) ||
+ // [ \ ] ^ _ `
+ ($o >= 91 && $o <= 96) ||
+ // lowercase
+ ($o >= 97 && $o <= 122) ||
+ // { | } ~
+ ($o >= 123 && $o <= 126)
+ );
+
+ // if it's valid, just keep it
+ if ($v) {
+ $r .= $s[$i];
+
+ // &
+ } elseif ($o == 38) {
+ $r .= '&amp;';
+
+ // <
+ } elseif ($o == 60) {
+ $r .= '&lt;';
+
+ // >
+ } elseif ($o == 62) {
+ $r .= '&gt;';
+
+ // '
+ } elseif ($o == 39) {
+ $r .= '&apos;';
+
+ // "
+ } elseif ($o == 34) {
+ $r .= '&quot;';
+
+ // unknown, add it as a reference
+ } elseif ($o > 0) {
+ if ($hex) {
+ $r .= '&#x'.strtoupper(dechex($o)).';';
+
+ } else {
+ $r .= '&#'.$o.';';
+ }
+ }
+ }
+
+ return $r;
}
/**
View
88 tests/ActiveResource.php
@@ -24,7 +24,6 @@ function test_build_xml () {
$this->assertEquals ($t->_build_xml ('foo', 'bar'), "<foo>bar</foo>\n");
$this->assertEquals ($t->_build_xml ('foo', array ('bar' => 'asdf')), "<foo><bar>asdf</bar>\n</foo>\n");
$this->assertEquals ($t->_build_xml ('foo', array ('@bar' => 'asdf')), "<foo bar=\"asdf\"></foo>\n");
- $this->assertEquals ($t->_xml_entities ('<'), '&#60;');
$xml = new SimpleXMLElement ('<foo><bar asdf="qwerty" />what</foo>');
$this->assertEquals ($t->_build_xml (0, $xml), "<foo><bar asdf=\"qwerty\"/>what</foo>\n");
@@ -72,6 +71,93 @@ function test_pleuralize () {
$this->assertEquals ($t->pluralize ('entry'), 'entries');
$this->assertEquals ($t->pluralize ('entries'), 'entries');
}
+
+ function test_xml_entities () {
+ $t = new Test;
+
+ $this->assertEquals ($t->_xml_entities ('asdf'), 'asdf');
+ $this->assertEquals ($t->_xml_entities ('<>'), '&lt;&gt;');
+ $this->assertEquals ($t->_xml_entities ('"'), '&quot;');
+ $this->assertEquals ($t->_xml_entities ('\''), '&apos;');
+ $this->assertEquals ($t->_xml_entities ('ä'), '&#xE4;');
+ $this->assertEquals ($t->_xml_entities ('£'), '&#xA3;');
+ $this->assertEquals ($t->_xml_entities (''), '&#x2022;');
+ $this->assertEquals ($t->_xml_entities ('À'), '&#xC0;');
+ $this->assertEquals ($t->_xml_entities ('à'), '&#xE0;');
+ $this->assertEquals ($t->_xml_entities ('Á'), '&#xC1;');
+ $this->assertEquals ($t->_xml_entities ('á'), '&#xE1;');
+ $this->assertEquals ($t->_xml_entities ('Â'), '&#xC2;');
+ $this->assertEquals ($t->_xml_entities ('â'), '&#xE2;');
+ $this->assertEquals ($t->_xml_entities ('Ã'), '&#xC3;');
+ $this->assertEquals ($t->_xml_entities ('ã'), '&#xE3;');
+ $this->assertEquals ($t->_xml_entities ('Ä'), '&#xC4;');
+ $this->assertEquals ($t->_xml_entities ('ä'), '&#xE4;');
+ $this->assertEquals ($t->_xml_entities ('Å'), '&#xC5;');
+ $this->assertEquals ($t->_xml_entities ('å'), '&#xE5;');
+ $this->assertEquals ($t->_xml_entities ('Æ'), '&#xC6;');
+ $this->assertEquals ($t->_xml_entities ('æ'), '&#xE6;');
+ $this->assertEquals ($t->_xml_entities ('Ç'), '&#xC7;');
+ $this->assertEquals ($t->_xml_entities ('ç'), '&#xE7;');
+ $this->assertEquals ($t->_xml_entities ('Ð'), '&#xD0;');
+ $this->assertEquals ($t->_xml_entities ('ð'), '&#xF0;');
+ $this->assertEquals ($t->_xml_entities ('È'), '&#xC8;');
+ $this->assertEquals ($t->_xml_entities ('è'), '&#xE8;');
+ $this->assertEquals ($t->_xml_entities ('É'), '&#xC9;');
+ $this->assertEquals ($t->_xml_entities ('é'), '&#xE9;');
+ $this->assertEquals ($t->_xml_entities ('Ê'), '&#xCA;');
+ $this->assertEquals ($t->_xml_entities ('ê'), '&#xEA;');
+ $this->assertEquals ($t->_xml_entities ('Ë'), '&#xCB;');
+ $this->assertEquals ($t->_xml_entities ('ë'), '&#xEB;');
+ $this->assertEquals ($t->_xml_entities ('Ì'), '&#xCC;');
+ $this->assertEquals ($t->_xml_entities ('ì'), '&#xEC;');
+ $this->assertEquals ($t->_xml_entities ('Í'), '&#xCD;');
+ $this->assertEquals ($t->_xml_entities ('í'), '&#xED;');
+ $this->assertEquals ($t->_xml_entities ('Î'), '&#xCE;');
+ $this->assertEquals ($t->_xml_entities ('î'), '&#xEE;');
+ $this->assertEquals ($t->_xml_entities ('Ï'), '&#xCF;');
+ $this->assertEquals ($t->_xml_entities ('ï'), '&#xEF;');
+ $this->assertEquals ($t->_xml_entities ('Ñ'), '&#xD1;');
+ $this->assertEquals ($t->_xml_entities ('ñ'), '&#xF1;');
+ $this->assertEquals ($t->_xml_entities ('Ò'), '&#xD2;');
+ $this->assertEquals ($t->_xml_entities ('ò'), '&#xF2;');
+ $this->assertEquals ($t->_xml_entities ('Ó'), '&#xD3;');
+ $this->assertEquals ($t->_xml_entities ('ó'), '&#xF3;');
+ $this->assertEquals ($t->_xml_entities ('Ô'), '&#xD4;');
+ $this->assertEquals ($t->_xml_entities ('ô'), '&#xF4;');
+ $this->assertEquals ($t->_xml_entities ('Õ'), '&#xD5;');
+ $this->assertEquals ($t->_xml_entities ('õ'), '&#xF5;');
+ $this->assertEquals ($t->_xml_entities ('Ö'), '&#xD6;');
+ $this->assertEquals ($t->_xml_entities ('ö'), '&#xF6;');
+ $this->assertEquals ($t->_xml_entities ('Ø'), '&#xD8;');
+ $this->assertEquals ($t->_xml_entities ('ø'), '&#xF8;');
+ $this->assertEquals ($t->_xml_entities ('Œ'), '&#x152;');
+ $this->assertEquals ($t->_xml_entities ('œ'), '&#x153;');
+ $this->assertEquals ($t->_xml_entities ('ß'), '&#xDF;');
+ $this->assertEquals ($t->_xml_entities ('Þ'), '&#xDE;');
+ $this->assertEquals ($t->_xml_entities ('þ'), '&#xFE;');
+ $this->assertEquals ($t->_xml_entities ('Ù'), '&#xD9;');
+ $this->assertEquals ($t->_xml_entities ('ù'), '&#xF9;');
+ $this->assertEquals ($t->_xml_entities ('Ú'), '&#xDA;');
+ $this->assertEquals ($t->_xml_entities ('ú'), '&#xFA;');
+ $this->assertEquals ($t->_xml_entities ('Û'), '&#xDB;');
+ $this->assertEquals ($t->_xml_entities ('û'), '&#xFB;');
+ $this->assertEquals ($t->_xml_entities ('Ü'), '&#xDC;');
+ $this->assertEquals ($t->_xml_entities ('ü'), '&#xFC;');
+ $this->assertEquals ($t->_xml_entities ('Ý'), '&#xDD;');
+ $this->assertEquals ($t->_xml_entities ('ý'), '&#xFD;');
+ $this->assertEquals ($t->_xml_entities ('Ÿ'), '&#x178;');
+ $this->assertEquals ($t->_xml_entities ('ÿ'), '&#xFF;');
+ $this->assertEquals ($t->_xml_entities ('Ć'), '&#x106;');
+ $this->assertEquals ($t->_xml_entities ('ć'), '&#x107;');
+ $this->assertEquals ($t->_xml_entities ('Č'), '&#x10C;');
+ $this->assertEquals ($t->_xml_entities ('č'), '&#x10D;');
+ $this->assertEquals ($t->_xml_entities ('Đ'), '&#x110;');
+ $this->assertEquals ($t->_xml_entities ('đ'), '&#x111;');
+ $this->assertEquals ($t->_xml_entities ('Š'), '&#x160;');
+ $this->assertEquals ($t->_xml_entities ('š'), '&#x161;');
+ $this->assertEquals ($t->_xml_entities ('Ž'), '&#x17D;');
+ $this->assertEquals ($t->_xml_entities ('ž'), '&#x17E;');
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.