mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Implemented String::insert
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6766 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
4519261b39
commit
8b42465eaf
2 changed files with 118 additions and 2 deletions
|
@ -430,6 +430,57 @@ class String extends Object {
|
||||||
}
|
}
|
||||||
return $data;
|
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: '/(?<!\\)\:%s/' (Overwrites before, after, breaks escape / clean)
|
||||||
|
* clean: A boolean, if set to true all variable placeholders that were not overwritten with $data items are going to be removed, including whitespace around them.
|
||||||
|
*
|
||||||
|
* @param string $str A string containing variable placeholders
|
||||||
|
* @param string $data A key => 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(
|
||||||
|
'/(?<!%s)%s%%s%s/',
|
||||||
|
preg_quote($options['escape'], '/'),
|
||||||
|
str_replace('%', '%%', preg_quote($options['before'], '/')),
|
||||||
|
str_replace('%', '%%', preg_quote($options['after'], '/'))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($data as $key => $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
|
* Return the Code points range for Unicode characters
|
||||||
*
|
*
|
||||||
|
|
|
@ -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 = <sum. Cake is <adjective>.';
|
||||||
|
$expected = '2 + 2 = <sum. Cake is yummy.';
|
||||||
|
$r = String::insert($string, array('sum' => '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() {
|
function testUtf8() {
|
||||||
$string = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
|
$string = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
|
||||||
$result = String::utf8($string);
|
$result = String::utf8($string);
|
||||||
|
@ -729,13 +794,13 @@ class StringTest extends UnitTestCase {
|
||||||
$expected = 32;
|
$expected = 32;
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$string = '𐐀𐐁𐐀𐐁𐐀𐐁';
|
$string = 'ЀЁЀЁЀЁ';
|
||||||
$find = '';
|
$find = '';
|
||||||
$result = String::strpos($string, $find, 5);
|
$result = String::strpos($string, $find, 5);
|
||||||
$expected = 8;
|
$expected = 8;
|
||||||
//$this->assertEqual($result, $expected);
|
//$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$string = '𐐩𐐪𐐩𐐪𐐩𐐪';
|
$string = 'ЩЪЩЪЩЪ';
|
||||||
$find = '';
|
$find = '';
|
||||||
$result = String::strpos($string, $find, 5);
|
$result = String::strpos($string, $find, 5);
|
||||||
$expected = 8;
|
$expected = 8;
|
||||||
|
|
Loading…
Add table
Reference in a new issue