Skip to content

Instantly share code, notes, and snippets.

@thekid
Created July 22, 2011 21:11
Show Gist options
  • Save thekid/1100437 to your computer and use it in GitHub Desktop.
Save thekid/1100437 to your computer and use it in GitHub Desktop.
XP Framework: Patch for Issue #41
diff --git a/core/src/main/php/lang.base.php b/core/src/main/php/lang.base.php
index b2cdb9f..1dc132f 100644
--- a/core/src/main/php/lang.base.php
+++ b/core/src/main/php/lang.base.php
@@ -93,6 +93,8 @@
return '<null>';
} else if (is_int($arg) || is_float($arg)) {
return (string)$arg;
+ } else if ($arg instanceof PrimitiveWrapper) {
+ return $arg->__toString();
} else if ($arg instanceof Generic && !isset($protect[(string)$arg->hashCode()])) {
$protect[(string)$arg->hashCode()]= TRUE;
$s= $arg->toString();
diff --git a/core/src/main/php/lang/Primitive.class.php b/core/src/main/php/lang/Primitive.class.php
index 63e9d6c..b8dd2a1 100644
--- a/core/src/main/php/lang/Primitive.class.php
+++ b/core/src/main/php/lang/Primitive.class.php
@@ -69,7 +69,7 @@
* @throws lang.IllegalArgumentException in case in cannot be unboxed.
*/
public static function unboxed($in) {
- if ($in instanceof String) return $in->toString();
+ if ($in instanceof String) return $in->__toString();
if ($in instanceof Double) return $in->floatValue();
if ($in instanceof Integer) return $in->intValue();
if ($in instanceof Boolean) return $in->value;
diff --git a/core/src/main/php/lang/types/Number.class.php b/core/src/main/php/lang/types/Number.class.php
index 9153560..6f13164 100644
--- a/core/src/main/php/lang/types/Number.class.php
+++ b/core/src/main/php/lang/types/Number.class.php
@@ -4,6 +4,8 @@
* $Id$
*/
+ uses('lang.types.PrimitiveWrapper');
+
/**
* The abstract class Number is the superclass of classes representing
* numbers
@@ -11,9 +13,8 @@
* @test xp://net.xp_framework.unittest.core.types.NumberTest
* @purpose Base class
*/
- abstract class Number extends Object {
- public
- $value = '';
+ abstract class Number extends Object implements PrimitiveWrapper {
+ public $value = '';
/**
@@ -80,5 +81,14 @@
public function equals($cmp) {
return $cmp instanceof $this && $this->value === $cmp->value;
}
+
+ /**
+ * Casts this to a primitive string
+ *
+ * @return string
+ */
+ public function __toString() {
+ return $this->value;
+ }
}
?>
diff --git a/core/src/main/php/lang/types/PrimitiveWrapper.class.php b/core/src/main/php/lang/types/PrimitiveWrapper.class.php
new file mode 100644
index 0000000..1bb75e6
--- /dev/null
+++ b/core/src/main/php/lang/types/PrimitiveWrapper.class.php
@@ -0,0 +1,21 @@
+<?php
+/* This class is part of the XP framework
+ *
+ * $Id$
+ */
+
+ /**
+ * Primitive wrapper
+ *
+ * @see xp://lang.types.Primitive
+ */
+ interface PrimitiveWrapper {
+
+ /**
+ * Casts this to a primitive string
+ *
+ * @return string
+ */
+ public function __toString();
+ }
+?>
diff --git a/core/src/main/php/lang/types/String.class.php b/core/src/main/php/lang/types/String.class.php
index f5094b1..10bd673 100644
--- a/core/src/main/php/lang/types/String.class.php
+++ b/core/src/main/php/lang/types/String.class.php
@@ -6,7 +6,7 @@
define('STR_ENC', 'UTF-8');
- uses('lang.types.Character', 'lang.types.Bytes');
+ uses('lang.types.PrimitiveWrapper', 'lang.types.Character', 'lang.types.Bytes');
/**
* Represents a string
@@ -15,7 +15,7 @@
* @test xp://net.xp_framework.unittest.core.types.StringTest
* @purpose Wrapper type
*/
- class String extends Object implements ArrayAccess {
+ class String extends Object implements PrimitiveWrapper, ArrayAccess {
protected
$buffer= '',
$length= 0;
@@ -281,12 +281,11 @@
* @return string
*/
public function toString() {
- return iconv(STR_ENC, iconv_get_encoding('output_encoding').'//TRANSLIT', $this->buffer);
+ return $this->getClassName().'("'.$this->__toString().'")';
}
/**
- * Returns a string representation of this string. Uses the current
- * output encoding and transliteration.
+ * Casts this to a primitive string
*
* @return string
*/
diff --git a/core/src/main/php/rdbms/StatementFormatter.class.php b/core/src/main/php/rdbms/StatementFormatter.class.php
index 1b1476a..67628de 100644
--- a/core/src/main/php/rdbms/StatementFormatter.class.php
+++ b/core/src/main/php/rdbms/StatementFormatter.class.php
@@ -137,6 +137,8 @@
} else if ($arg instanceof SQLRenderable) {
$r.= $arg->asSql($this->conn).', ';
continue;
+ } else if ($arg instanceof PrimitiveWrapper) {
+ $p= $arg->__toString();
} else if ($arg instanceof Generic) {
$p= $arg->toString();
} else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment