Skip to content

Commit

Permalink
Move Set::format across.
Browse files Browse the repository at this point in the history
Remove the {0} style of formatting.  Custom formatting
syntax is a bit silly. sprintf() is more than expressive enough
for this method.
  • Loading branch information
markstory committed Mar 27, 2012
1 parent e4a5057 commit ff5e72c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 28 deletions.
78 changes: 61 additions & 17 deletions lib/Cake/Test/Case/Utility/Set2Test.php
Expand Up @@ -1208,13 +1208,12 @@ public function testCombineWithGroupPath() {
* @return void
*/
public function testCombineWithFormatting() {
$this->markTestIncomplete('Not done, format() is not implemented');
$a = self::userData();

$result = Set2::combine(
$a,
'{n}.User.id',
array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'),
array('%1$s: %2$s', '{n}.User.Data.user', '{n}.User.Data.name'),
'{n}.User.group_id'
);
$expected = array(
Expand All @@ -1230,9 +1229,11 @@ public function testCombineWithFormatting() {

$result = Set2::combine(
$a,
array('{0}: {1}',
'{n}.User.Data.user',
'{n}.User.Data.name'),
array(
'%s: %s',
'{n}.User.Data.user',
'{n}.User.Data.name'
),
'{n}.User.id'
);
$expected = array(
Expand All @@ -1242,18 +1243,6 @@ public function testCombineWithFormatting() {
);
$this->assertEquals($expected, $result);

$result = Set2::combine(
$a,
array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'),
'{n}.User.id'
);
$expected = array(
'Mariano Iglesias: mariano.iglesias' => 2,
'Larry E. Masters: phpnut' => 14,
'The Gwoo: gwoo' => 25
);
$this->assertEquals($expected, $result);

$result = Set2::combine(
$a,
array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'),
Expand All @@ -1278,4 +1267,59 @@ public function testCombineWithFormatting() {
);
$this->assertEquals($expected, $result);
}

/**
* testFormat method
*
* @return void
*/
public function testFormat() {
$data = self::userData();

$result = Set2::format(
$data,
array('{n}.User.Data.user', '{n}.User.id'),
'%s, %s'
);
$expected = array(
'mariano.iglesias, 2',
'phpnut, 14',
'gwoo, 25'
);
$this->assertEquals($expected, $result);

$result = Set2::format(
$data,
array('{n}.User.Data.user', '{n}.User.id'),
'%2$s, %1$s'
);
$expected = array(
'2, mariano.iglesias',
'14, phpnut',
'25, gwoo'
);
$this->assertEquals($expected, $result);
}

/**
* testFormattingNullValues method
*
* @return void
*/
public function testFormatNullValues() {
$this->markTestIncomplete('Not done yet');

$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' => null)),
array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null)));

$result = Set2::format($data, '%s', array('{n}.Person.something'));
$expected = array('42', '', '');
$this->assertEquals($expected, $result);

$result = Set2::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.something'));
$expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
$this->assertEquals($expected, $result);
}
}
61 changes: 50 additions & 11 deletions lib/Cake/Utility/Set2.php
Expand Up @@ -149,13 +149,14 @@ public static function extract(array $data, $path) {
protected static function _matchToken($key, $token) {
if ($token === '{n}') {
return is_numeric($key);
} elseif ($token === '{s}') {
}
if ($token === '{s}') {
return is_string($key);
} elseif (is_numeric($token)) {
}
if (is_numeric($token)) {
return ($key == $token);
} else {
return ($key === $token);
}
return ($key === $token);
}

/**
Expand Down Expand Up @@ -268,7 +269,8 @@ protected static function _simpleOp($op, $data, $path, $values = null) {
if (!is_array($_list)) {
return array();
}
} elseif ($op === 'remove') {
}
if ($op === 'remove') {
if ($i === count($path) - 1) {
unset($_list[$key]);
} else {
Expand Down Expand Up @@ -329,7 +331,7 @@ public static function combine(array $data, $keyPath, $valuePath = null, $groupP

if (is_array($keyPath)) {
$format = array_shift($keyPath);
$keys = self::format($data, $format, $keyPath);
$keys = self::format($data, $keyPath, $format);
} else {
$keys = self::extract($data, $keyPath);
}
Expand All @@ -339,19 +341,17 @@ public static function combine(array $data, $keyPath, $valuePath = null, $groupP

if (!empty($valuePath) && is_array($valuePath)) {
$format = array_shift($valuePath);
$vals = self::format($data, $format, $valuePath);
$vals = self::format($data, $valuePath, $format);
} elseif (!empty($valuePath)) {
$vals = self::extract($data, $valuePath);
}

$count = count($keys);
for ($i = 0; $i < $count; $i++) {
if (!isset($vals[$i])) {
$vals[$i] = null;
}
$vals[$i] = isset($vals[$i]) ? $vals[$i] : null;
}

if ($groupPath != null) {
if ($groupPath !== null) {
$group = self::extract($data, $groupPath);
if (!empty($group)) {
$c = count($keys);
Expand All @@ -371,7 +371,46 @@ public static function combine(array $data, $keyPath, $valuePath = null, $groupP
return array();
}
return array_combine($keys, $vals);
}

/**
* Returns a formated series of values extracted from `$data`, using
* `$format` as the format and `$paths` as the values to extract.
*
* @param array $data Source array from which to extract the data
* @param string $paths An array containing one or more Set2::extract()-style key paths
* @param string $format Format string into which values will be inserted, see sprintf()
* @return array An array of strings extracted from `$path` and formatted with `$format`
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::format
* @see sprintf()
* @see Set2::extract()
*/
public static function format(array $data, array $paths, $format) {
$extracted = array();
$count = count($paths);

if (!$count) {
return;
}

for ($i = 0; $i < $count; $i++) {
$extracted[] = Set::extract($data, $paths[$i]);
}
$out = array();
$data = $extracted;
$count = count($data[0]);

$count2 = count($data);
for ($j = 0; $j < $count; $j++) {
$args = array();
for ($i = 0; $i < $count2; $i++) {
if (array_key_exists($j, $data[$i])) {
$args[] = $data[$i][$j];
}
}
$out[] = vsprintf($format, $args);
}
return $out;
}

/**
Expand Down

0 comments on commit ff5e72c

Please sign in to comment.