From 8b42465eaf7e194d17acceb5dd74e8a9e8f6b5cb Mon Sep 17 00:00:00 2001 From: the_undefined Date: Thu, 8 May 2008 02:59:00 +0000 Subject: [PATCH] Implemented String::insert git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6766 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/string.php | 51 ++++++++++++++++++++ cake/tests/cases/libs/string.test.php | 69 ++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/cake/libs/string.php b/cake/libs/string.php index 3293852be..f5f067237 100644 --- a/cake/libs/string.php +++ b/cake/libs/string.php @@ -430,6 +430,57 @@ class String extends Object { } return $data; } +/** + * Replaces variable placeholders inside a $str with any given $data. Each key in the $data array corresponds to a variable + * placeholder name in $str. Example: + * + * Sample: String::insert('My name is :name and I am :age years old.', array('name' => 'Bob', '65')); + * Returns: My name is Bob and I am 65 years old. + * + * Available $options are: + * before: The character or string in front of the name of the variable placeholder (Defaults to ':') + * after: The character or string after the name of the variable placeholder (Defaults to null) + * escape: The character or string used to escape the before character / string (Defaults to '\') + * format: A regex to use for matching variable placeholders. Default is: '/(? val array where each key stands for a placeholder variable name to be replaced with val + * @param string $options An array of options, see description above + * @return string + * @access public + */ + function insert($str, $data, $options = array()) { + $options = am(array( + 'before' => ':', + 'after' => null, + 'escape' => '\\', + 'format' => null, + 'clean' => false, + ), $options); + + $format = $options['format']; + if (!isset($format)) { + $format = sprintf( + '/(? $val) { + $key = sprintf($format, preg_quote($key, '/')); + $str = preg_replace($key, $val, $str); + } + if (!isset($options['format']) && isset($options['before'])) { + $str = str_replace($options['escape'].$options['before'], $options['before'], $str); + } + if ($options['clean']) { + $str = preg_replace(sprintf('/(%s[^\s]+[\s]*|[\s]*%s[^\s]+)/', $options['before'], $options['before']), '', $str); + } + return $str; + } /** * Return the Code points range for Unicode characters * diff --git a/cake/tests/cases/libs/string.test.php b/cake/tests/cases/libs/string.test.php index 80c9fc716..cf1509443 100644 --- a/cake/tests/cases/libs/string.test.php +++ b/cake/tests/cases/libs/string.test.php @@ -53,6 +53,71 @@ class StringTest extends UnitTestCase { } } + function testInsert() { + $string = '2 + 2 = :sum. Cake is :adjective.'; + $expected = '2 + 2 = 4. Cake is yummy.'; + $r = String::insert($string, array('sum' => '4', 'adjective' => 'yummy')); + $this->assertEqual($r, $expected); + + $string = '2 + 2 = %sum. Cake is %adjective.'; + $r = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%')); + $this->assertEqual($r, $expected); + + $string = '2 + 2 = 2sum2. Cake is 9adjective9.'; + $r = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])%s\\1/')); + $this->assertEqual($r, $expected); + + $string = '2 + 2 = 12sum21. Cake is 23adjective45.'; + $expected = '2 + 2 = 4. Cake is 23adjective45.'; + $r = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])([\d])%s\\2\\1/')); + $this->assertEqual($r, $expected); + + $string = '2 + 2 = .'; + $expected = '2 + 2 = '4', 'adjective' => 'yummy'), array('before' => '<', 'after' => '>')); + $this->assertEqual($r, $expected); + + $string = '2 + 2 = \:sum. Cake is :adjective.'; + $expected = '2 + 2 = :sum. Cake is yummy.'; + $r = String::insert($string, array('sum' => '4', 'adjective' => 'yummy')); + $this->assertEqual($r, $expected); + + $string = '2 + 2 = !:sum. Cake is :adjective.'; + $r = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('escape' => '!')); + $this->assertEqual($r, $expected); + + $string = '2 + 2 = \%sum. Cake is %adjective.'; + $expected = '2 + 2 = %sum. Cake is yummy.'; + $r = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%')); + $this->assertEqual($r, $expected); + + $string = ':a :b \:a :a'; + $expected = '1 2 :a 1'; + $r = String::insert($string, array('a' => 1, 'b' => 2)); + $this->assertEqual($r, $expected); + + $string = ':a :b :c'; + $expected = '2 3'; + $r = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true)); + $this->assertEqual($r, $expected); + + $string = ':a :b :c'; + $expected = '1 3'; + $r = String::insert($string, array('a' => 1, 'c' => 3), array('clean' => true)); + $this->assertEqual($r, $expected); + + $string = ':a :b :c'; + $expected = '2 3'; + $r = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true)); + $this->assertEqual($r, $expected); + + $string = ':a, :b and :c'; + $expected = '2 and 3'; + $r = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true)); + $this->assertEqual($r, $expected); + + } + function testUtf8() { $string = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'; $result = String::utf8($string); @@ -729,13 +794,13 @@ class StringTest extends UnitTestCase { $expected = 32; $this->assertEqual($result, $expected); - $string = '๐€๐๐€๐๐€๐'; + $string = 'ะ€ะะ€ะะ€ะ'; $find = ''; $result = String::strpos($string, $find, 5); $expected = 8; //$this->assertEqual($result, $expected); - $string = '๐ฉ๐ช๐ฉ๐ช๐ฉ๐ช'; + $string = 'ะฉะชะฉะชะฉะช'; $find = ''; $result = String::strpos($string, $find, 5); $expected = 8;