Skip to content
This repository
Browse code

Adding dimensions() and maxDimensions()

Splitting the Set::countDim() into two methods.  This creates a simpler
API, as each method only does one job, and only has one argument.  Also
replacing recursion with iteration.
  • Loading branch information...
commit 51e3ee0425cfa7cb696f6d760f5b10d8802e82ea 1 parent db3485d
Mark Story authored January 14, 2012
79  lib/Cake/Test/Case/Utility/Set2Test.php
@@ -142,4 +142,83 @@ public function testGet() {
142 142
 		$result = Set2::get($data, '1.Article');
143 143
 		$this->assertEquals($data[1]['Article'], $result);
144 144
 	}
  145
+
  146
+/**
  147
+ * Test dimensions.
  148
+ *
  149
+ * @return void
  150
+ */
  151
+	public function testDimensions() {
  152
+		$result = Set2::dimensions(array());
  153
+		$this->assertEquals($result, 0);
  154
+
  155
+		$data = array('one', '2', 'three');
  156
+		$result = Set2::dimensions($data);
  157
+		$this->assertEquals($result, 1);
  158
+
  159
+		$data = array('1' => '1.1', '2', '3');
  160
+		$result = Set2::dimensions($data);
  161
+		$this->assertEquals($result, 1);
  162
+
  163
+		$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  164
+		$result = Set2::dimensions($data);
  165
+		$this->assertEquals($result, 2);
  166
+
  167
+		$data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  168
+		$result = Set2::dimensions($data);
  169
+		$this->assertEquals($result, 1);
  170
+
  171
+		$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  172
+		$result = Set2::dimensions($data);
  173
+		$this->assertEquals($result, 2);
  174
+
  175
+	}
  176
+
  177
+/**
  178
+ * Test maxDimensions
  179
+ *
  180
+ * @return void
  181
+ */
  182
+	public function testMaxDimensions() {
  183
+		$data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  184
+		$result = Set2::maxDimensions($data);
  185
+		$this->assertEquals($result, 2);
  186
+
  187
+		$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  188
+		$result = Set2::maxDimensions($data);
  189
+		$this->assertEquals($result, 3);
  190
+
  191
+		$data = array(
  192
+			'1' => array('1.1' => '1.1.1'),
  193
+			array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))),
  194
+			'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  195
+		);
  196
+		$result = Set2::maxDimensions($data);
  197
+		$this->assertEquals($result, 4);
  198
+
  199
+		$data = array(
  200
+			'1' => array('1.1' => '1.1.1'),
  201
+			array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))),
  202
+			'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  203
+		);
  204
+		$result = Set2::maxDimensions($data);
  205
+		$this->assertEquals($result, 5);
  206
+
  207
+		$data = array(
  208
+			'1' => array('1.1' => '1.1.1'),
  209
+			array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
  210
+			'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  211
+		);
  212
+		$result = Set2::maxDimensions($data);
  213
+		$this->assertEquals($result, 5);
  214
+
  215
+		$data = array(
  216
+			'1' => array('1.1' => '1.1.1'),
  217
+			array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
  218
+			'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  219
+		);
  220
+		$result = Set2::maxDimensions($data);
  221
+		$this->assertEquals($result, 5);
  222
+	}
  223
+
145 224
 }
54  lib/Cake/Utility/Set2.php
@@ -90,15 +90,57 @@ public static function merge(array $data, $merge) {
90 90
 
91 91
 	}
92 92
 
  93
+/**
  94
+ * Counts the dimensions of an array. 
  95
+ * Only considers the dimension of the first element in the array.
  96
+ *
  97
+ * If you have an un-even or hetrogenous array, consider using Set2::maxDimensions() 
  98
+ * to get the dimensions of the array.
  99
+ *
  100
+ * @param array $array Array to count dimensions on
  101
+ * @return integer The number of dimensions in $data
  102
+ * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::countDim
  103
+ */
93 104
 	public static function dimensions(array $data) {
94  
-	
  105
+		if (empty($data)) {
  106
+			return 0;
  107
+		}
  108
+		reset($data);
  109
+		$depth = 1;
  110
+		while ($elem = array_shift($data)) {
  111
+			if (is_array($elem)) {
  112
+				$depth += 1;
  113
+				$data =& $elem;
  114
+			} else {
  115
+				break;
  116
+			}
  117
+		}
  118
+		return $depth;
95 119
 	}
96 120
 
97  
-	/**
98  
-	 * Map a callback across all elements in a set.
99  
-	 * Can be provided a path to only modify slices of the set.
100  
-	 *
101  
-	 */
  121
+/**
  122
+ * Counts the dimensions of *all* array elements. Useful for finding the maximum
  123
+ * number of dimensions in a mixed array.
  124
+ * 
  125
+ * @param array $data Array to count dimensions on
  126
+ * @return integer The maximum number of dimensions in $data
  127
+ * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::countDim
  128
+ */
  129
+	public static function maxDimensions(array $data) {
  130
+		$depth = array();
  131
+		if (is_array($data) && reset($data) !== false) {
  132
+			foreach ($data as $value) {
  133
+				$depth[] = Set2::dimensions((array)$value) + 1;
  134
+			}
  135
+		}
  136
+		return max($depth);
  137
+	}
  138
+
  139
+/**
  140
+ * Map a callback across all elements in a set.
  141
+ * Can be provided a path to only modify slices of the set.
  142
+ *
  143
+ */
102 144
 	public static function map(array $data, $path, $function = null) {
103 145
 
104 146
 	}

0 notes on commit 51e3ee0

Please sign in to comment.
Something went wrong with that request. Please try again.