Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Docs + coding standards.

  • Loading branch information...
commit 95d4549c9b51ca1f82d12fbf40316bece7d34eeb 1 parent c087576
Mark Story markstory authored
Showing with 44 additions and 47 deletions.
  1. +16 −20 lib/Cake/Test/Case/Utility/HashTest.php
  2. +28 −27 lib/Cake/Utility/Hash.php
36 lib/Cake/Test/Case/Utility/HashTest.php
View
@@ -17,7 +17,7 @@
class HashTest extends CakeTestCase {
public static function articleData() {
- return array(
+ return array(
array(
'Article' => array(
'id' => '1',
@@ -225,7 +225,6 @@ public function testDimensions() {
$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
$result = Hash::dimensions($data);
$this->assertEquals($result, 2);
-
}
/**
@@ -360,7 +359,6 @@ public function testDiff() {
);
$this->assertEquals($expected, $result);
-
$b = array(
0 => array('name' => 'me'),
1 => array('name' => 'about')
@@ -377,7 +375,6 @@ public function testDiff() {
$result = Hash::diff($a, $b);
$this->assertEquals($result, $b);
-
$a = array('name' => 'bob', 'address' => 'home');
$b = array();
$result = Hash::diff($a, $b);
@@ -453,7 +450,7 @@ public function testMerge() {
'cakephp',
'ice-cream'
);
- $result = Hash::merge($a, $b);
+ $result = Hash::merge($a, $b);
$this->assertEquals($expected, $result);
$c = array(
@@ -478,7 +475,7 @@ public function testMerge() {
'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')
)
);
- $b = array(
+ $b = array(
'Cacheable' => array('enabled' => false),
'Limit',
'Bindable',
@@ -659,7 +656,7 @@ public function testExtractNumericKey() {
$data = self::articleData();
$result = Hash::extract($data, '{n}.Article.title');
$expected = array(
- 'First Article', 'Second Article',
+ 'First Article', 'Second Article',
'Third Article', 'Fourth Article',
'Fifth Article'
);
@@ -728,7 +725,7 @@ public function testExtractNumericNonZero() {
$expected = array('John', 'Bob', 'Tony');
$this->assertEquals($expected, $result);
}
-
+
/**
* Test the {s} selector.
*
@@ -883,7 +880,7 @@ public function testExtractUnevenKeys() {
array('test5', 'test6')
);
$this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
-
+
$data['Level1']['Level2'] = array('test1', 'test2');
$this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
}
@@ -1039,14 +1036,14 @@ public function testSortWithOutOfOrderKeys() {
* @return void
*/
public function testSortString() {
- $to_sort = array(
+ $toSort = array(
'four' => array('number' => 4, 'some' => 'foursome'),
'six' => array('number' => 6, 'some' => 'sixsome'),
'five' => array('number' => 5, 'some' => 'fivesome'),
'two' => array('number' => 2, 'some' => 'twosome'),
'three' => array('number' => 3, 'some' => 'threesome')
);
- $sorted = Hash::sort($to_sort, '{s}.number', 'asc');
+ $sorted = Hash::sort($toSort, '{s}.number', 'asc');
$expected = array(
'two' => array('number' => 2, 'some' => 'twosome'),
'three' => array('number' => 3, 'some' => 'threesome'),
@@ -1290,7 +1287,6 @@ public function testCombineWithGroupPath() {
);
$this->assertEquals($expected, $result);
-
$result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
$expected = array(
1 => array(
@@ -1450,7 +1446,7 @@ public function testFormatNullValues() {
public function testMap() {
$data = self::articleData();
- $result = Hash::map($data, '{n}.Article.id', array($this, '_mapCallback'));
+ $result = Hash::map($data, '{n}.Article.id', array($this, 'mapCallback'));
$expected = array(2, 4, 6, 8, 10);
$this->assertEquals($expected, $result);
}
@@ -1470,7 +1466,7 @@ public function testApply() {
public function testReduce() {
$data = self::articleData();
- $result = Hash::reduce($data, '{n}.Article.id', array($this, '_reduceCallback'));
+ $result = Hash::reduce($data, '{n}.Article.id', array($this, 'reduceCallback'));
$this->assertEquals(15, $result);
}
@@ -1480,7 +1476,7 @@ public function testReduce() {
* @param mixed $value
* @return mixed.
*/
- public function _mapCallback($value) {
+ public function mapCallback($value) {
return $value * 2;
}
@@ -1491,7 +1487,7 @@ public function _mapCallback($value) {
* @param mixed $two
* @return mixed.
*/
- public function _reduceCallback($one, $two) {
+ public function reduceCallback($one, $two) {
return $one + $two;
}
@@ -1903,7 +1899,7 @@ public function testMissingParent() {
);
$result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
- foreach($result as &$row) {
+ foreach ($result as &$row) {
if (empty($row['children'])) {
unset($row['children']);
}
@@ -1936,10 +1932,10 @@ public function testMergeDiff() {
$result = Hash::mergeDiff($array1, array());
$this->assertEquals($result, $array1);
-
+
$result = Hash::mergeDiff(array(), $array1);
$this->assertEquals($result, $array1);
-
+
$array3 = array(
'ModelOne' => array(
'id' => 1003,
@@ -2009,7 +2005,7 @@ public function testMergeDiff() {
$this->assertEquals($result, $array1);
$result = Hash::mergeDiff($array1, $array2);
- $this->assertEquals($result, $array1+$array2);
+ $this->assertEquals($result, $array1 + $array2);
}
/**
55 lib/Cake/Utility/Hash.php
View
@@ -19,10 +19,10 @@
* Library of array functions for manipulating and extracting data
* from arrays or 'sets' of data.
*
- * `Hash` provides an improved interface and more consistent and
+ * `Hash` provides an improved interface, more consistent and
* predictable set of features over `Set`. While it lacks the spotty
* support for pseudo Xpath, its more fully featured dot notation provides
- * the similar features in a more consistent way.
+ * similar features in a more consistent implementation.
*
* @package Cake.Utility
*/
@@ -30,8 +30,8 @@ class Hash {
/**
* Get a single value specified by $path out of $data.
- * Does not support the full dot notation feature set,
- * but is faster for simple operations.
+ * Does not support the full dot notation feature set,
+ * but is faster for simple read operations.
*
* @param array $data Array of data to operate on.
* @param mixed $path The path being searched for. Either a dot
@@ -53,7 +53,7 @@ public static function get(array $data, $path) {
} else {
return null;
}
-
+
}
return $data;
}
@@ -63,10 +63,9 @@ public static function get(array $data, $path) {
* The path expression is a dot separated expression, that can contain a set
* of patterns and expressions:
*
- * - `{n}` Matches any numeric key.
+ * - `{n}` Matches any numeric key, or integer.
* - `{s}` Matches any string key.
- * - `[id]` Matches elements with an `id` index.
- * - `[id>2]` Matches elements that have an `id` index greater than 2.
+ * - `Foo` Matches any key with the exact same value.
*
* There are a number of attribute operators:
*
@@ -80,11 +79,11 @@ public static function get(array $data, $path) {
* - `{n}.User.name` Get the name of every user in the set of users.
* - `{n}.User[id]` Get the name of every user with an id key.
* - `{n}.User[id>=2]` Get the name of every user with an id key greater than or equal to 2.
- * - `{n}.User[username=/^paul/]` Get User elements with username containing `^paul`.
+ * - `{n}.User[username=/^paul/]` Get User elements with username matching `^paul`.
*
* @param array $data The data to extract from.
* @param string $path The path to extract.
- * @return array An array of the extracted values. Returns an empty array
+ * @return array An array of the extracted values. Returns an empty array
* if there are no matches.
*/
public static function extract(array $data, $path) {
@@ -94,7 +93,7 @@ public static function extract(array $data, $path) {
// Simple paths.
if (!preg_match('/[{\[]/', $path)) {
- return (array) self::get($data, $path);
+ return (array)self::get($data, $path);
}
if (strpos('[', $path) === false) {
@@ -107,7 +106,7 @@ public static function extract(array $data, $path) {
$context = array($_key => array($data));
- do {
+ do {
$token = array_shift($tokens);
$next = array();
@@ -125,7 +124,7 @@ public static function extract(array $data, $path) {
}
}
}
-
+
// Filter for attributes.
if ($conditions) {
$filter = array();
@@ -163,12 +162,12 @@ protected static function _matchToken($key, $token) {
}
/**
- * Checks whether or not $data matches the selector
+ * Checks whether or not $data matches the attribute patterns
*
* @param array $data Array of data to match.
- * @param string $selector The selector to match.
+ * @param string $selector The patterns to match.
* @return boolean Fitness of expression.
- */
+ */
protected static function _matches(array $data, $selector) {
preg_match_all(
'/(\[ (?<attr>[^=><!]+?) (\s* (?<op>[><!]?[=]|[><]) \s* (?<val>[^\]]+) )? \])/x',
@@ -220,7 +219,8 @@ protected static function _matches(array $data, $selector) {
}
/**
- * Insert $values into an array with the given $path.
+ * Insert $values into an array with the given $path. You can use
+ * `{n}` and `{s}` elements to insert $data multiple times.
*
* @param array $data The data to insert into.
* @param string $path The path to insert at.
@@ -289,6 +289,8 @@ protected static function _simpleOp($op, $data, $path, $values = null) {
/**
* Remove data matching $path from the $data array.
+ * You can use `{n}` and `{s}` to remove multiple elements
+ * from $data.
*
* @param array $data The data to operate on
* @param string $path A path expression to use to remove.
@@ -313,7 +315,6 @@ public static function remove(array $data, $path) {
return $data;
}
-
/**
* Creates an associative array using `$keyPath` as the path to build its keys, and optionally
* `$valuePath` as path to get the values. If `$valuePath` is not specified, all values will be initialized
@@ -411,10 +412,10 @@ public static function format(array $data, array $paths, $format) {
$data = $extracted;
$count = count($data[0]);
- $count2 = count($data);
+ $countTwo = count($data);
for ($j = 0; $j < $count; $j++) {
$args = array();
- for ($i = 0; $i < $count2; $i++) {
+ for ($i = 0; $i < $countTwo; $i++) {
if (array_key_exists($j, $data[$i])) {
$args[] = $data[$i][$j];
}
@@ -584,7 +585,7 @@ public static function expand($data, $separator = '.') {
/**
* This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`.
*
- * The difference between this method and the built-in ones, is that if an array key contains another array, then
+ * The difference between this method and the built-in ones, is that if an array key contains another array, then
* Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for
* keys that contain scalar values (unlike `array_merge_recursive`).
*
@@ -600,7 +601,7 @@ public static function merge(array $data, $merge) {
$return = current($args);
while (($arg = next($args)) !== false) {
- foreach ((array)$arg as $key => $val) {
+ foreach ((array)$arg as $key => $val) {
if (!empty($return[$key]) && is_array($return[$key]) && is_array($val)) {
$return[$key] = self::merge($return[$key], $val);
} elseif (is_int($key)) {
@@ -626,14 +627,14 @@ public static function numeric(array $data) {
}
$values = array_values($data);
$str = implode('', $values);
- return (bool) ctype_digit($str);
+ return (bool)ctype_digit($str);
}
/**
- * Counts the dimensions of an array.
+ * Counts the dimensions of an array.
* Only considers the dimension of the first element in the array.
*
- * If you have an un-even or hetrogenous array, consider using Hash::maxDimensions()
+ * If you have an un-even or hetrogenous array, consider using Hash::maxDimensions()
* to get the dimensions of the array.
*
* @param array $array Array to count dimensions on
@@ -660,7 +661,7 @@ public static function dimensions(array $data) {
/**
* Counts the dimensions of *all* array elements. Useful for finding the maximum
* number of dimensions in a mixed array.
- *
+ *
* @param array $data Array to count dimensions on
* @return integer The maximum number of dimensions in $data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::maxDimensions
@@ -756,7 +757,7 @@ public static function sort(array $data, $path, $dir) {
if ($numeric) {
$sorted[] = $data[$k];
continue;
- }
+ }
if (isset($originalKeys[$k])) {
$sorted[$originalKeys[$k]] = $data[$originalKeys[$k]];
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.