Permalink
Browse files

fixing issue #19 on github where passing an empty array for a value t…

…o String::insert() throws error.
  • Loading branch information...
1 parent c233402 commit c433ae93354a2171c1ce7ffd97a4277c838f5b67 @gwoo gwoo committed Jul 19, 2011
Showing with 15 additions and 3 deletions.
  1. +13 −0 tests/cases/util/StringTest.php
  2. +2 −3 util/String.php
@@ -257,6 +257,19 @@ public function testInsertWithObject() {
}
/**
+ * Test that an empty array is not added to the string
+ *
+ * @return void
+ */
+ public function testInsertWithEmptyArray() {
+ $result = String::insert("Hey, what are you tryin' to {:action} on us?",
+ array('action' => array())
+ );
+ $expected = "Hey, what are you tryin' to on us?";
+ $this->assertEqual($expected, $result);
+ }
+
+ /**
* test Clean Insert
*
* @return void
View
@@ -239,6 +239,7 @@ public static function insert($str, array $data, array $options = array()) {
$replace = array();
foreach ($data as $key => $value) {
+ $value = empty($value) ? null : $value;
$replace["{$options['before']}{$key}{$options['after']}"] = $value;
}
$str = strtr($str, $replace);
@@ -271,9 +272,7 @@ public static function insert($str, array $data, array $options = array()) {
$value = '';
}
}
- if (!is_array($value)) {
- $str = str_replace($hashVal, $value, $str);
- }
+ $str = str_replace($hashVal, (string) $value, $str);
}
if (!isset($options['format']) && isset($options['before'])) {

0 comments on commit c433ae9

Please sign in to comment.