Implementing Set::format() to combine extracted array values

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5512 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-08-11 21:32:25 +00:00
parent b6c6721528
commit 823672217a
2 changed files with 90 additions and 0 deletions

View file

@ -303,6 +303,50 @@ class Set extends Object {
}
return $return;
}
/**
* Returns a series of values extracted from an array, formatted in a format string.
*
* @param array $data Source array from which to extract the data
* @param string $format Format string into which values will be inserted
* @param array $keys An array containing one or more Set::extract()-style key paths
* @return array An array of strings extracted from $keys and formatted with $format
* @access public
*/
function format($data, $format, $keys) {
$extracted = array();
$count = count($keys);
if (!$count) {
return;
}
for ($i = 0; $i < $count; $i++) {
$extracted[] = Set::extract($data, $keys[$i]);
}
if (preg_match_all('/\{([0-9]+)\}/msi', $format, $keys) && isset($keys[1])) {
$out = array();
$keys = $keys[1];
$data = $extracted;
$count = count($data[0]);
$format = preg_split('/\{([0-9]+)\}/msi', $format);
$count2 = count($format);
for ($j = 0; $j < $count; $j++) {
$formatted = '';
for ($i = 0; $i <= $count2; $i++) {
if (isset($format[$i])) {
$formatted .= $format[$i];
}
if (isset($keys[$i]) && isset($data[$keys[$i]][$j])) {
$formatted .= $data[$keys[$i]][$j];
}
}
$out[] = $formatted;
}
}
return $out;
}
/**
* Gets a value from an array or object that maps a given path.
* The special {n}, as seen in the Model::generateList method, is taken care of here.

View file

@ -508,5 +508,51 @@ class SetTest extends UnitTestCase {
$result = Set::reverse($map);
$this->assertIdentical($result, $expected);
}
function testFormatting() {
$data = array(
array('Person' => array(
'first_name' => 'Nate',
'last_name' => 'Abele',
'city' => 'Boston',
'state' => 'MA',
'something' => '42'
)),
array('Person' => array(
'first_name' => 'Larry',
'last_name' => 'Masters',
'city' => 'Boondock',
'state' => 'TN',
'something' => '{0}'
)),
array('Person' => array(
'first_name' => 'Garrett',
'last_name' => 'Woodworth',
'city' => 'Venice Beach',
'state' => 'CA',
'something' => '{1}'
))
);
$result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name'));
$expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett');
$this->assertEqual($result, $expected);
$result = Set::format($data, '{0}, {1}', array('{n}.Person.last_name', '{n}.Person.first_name'));
$this->assertEqual($result, $expected);
$result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state'));
$expected = array('Boston, MA', 'Boondock, TN', 'Venice Beach, CA');
$this->assertEqual($result, $expected);
$result = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state'));
$expected = array('{Boston, MA}', '{Boondock, TN}', '{Venice Beach, CA}');
$this->assertEqual($result, $expected);
$result = Set::format($data, '{{0}, {1}}', array('{n}.Person.something', '{n}.Person.something'));
$expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
$this->assertEqual($result, $expected);
}
}
?>