Skip to content

Commit 53b0be3

Browse files
author
coa
committed
isStructureSame method added along with the tests
1 parent 4479e28 commit 53b0be3

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ SimpleArrayLibrary::isNumeric(array('a' => 1, array(1))); // false
244244
SimpleArrayLibrary::isNumeric(array(1, 1)); // true
245245
SimpleArrayLibrary::isNumeric(array()); // false
246246
```
247+
isStructureSame
248+
------------------------------
249+
Checks whether 2 arrays have same structure (depth and keys). Values of leaf nodes are ignored, only keys are compared.
250+
```php
251+
SimpleArrayLibrary::isStructureSame(1, 'a'); // true
252+
SimpleArrayLibrary::isStructureSame(array(1, array(1)), array(2, array(3)); // true
253+
SimpleArrayLibrary::isStructureSame(array(1, array(1)), array(1, 1)); // false
254+
SimpleArrayLibrary::isStructureSame(array(), array()); // true
255+
```
247256
isSubArray
248257
------------------------------
249258
Checks whether array is sub-array of the other array (whether all key-value pairs of sub-array are present in array).

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
4.2.0
2+
==============================
3+
isStructureSame method added
14
4.1.0
25
==============================
36
isNumeric method added

src/SimpleArrayLibrary.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,41 @@ public static function isNumeric(array $array)
453453
return array_keys($array) == range(0, count($array) - 1);
454454
}
455455

456+
/**
457+
* @param mixed $input1
458+
* @param mixed $input2
459+
*
460+
* @return bool
461+
*/
462+
public static function isStructureSame($input1, $input2)
463+
{
464+
$return = true;
465+
if (is_array($input1) && is_array($input2)) {
466+
if (!self::compareArrays($input1, $input2) || !self::compareArrays($input2, $input1)) {
467+
$return = false;
468+
}
469+
} else {
470+
$return = !is_array($input1) && !is_array($input2);
471+
}
472+
473+
return $return;
474+
}
475+
476+
private function compareArrays(array $input1, array $input2)
477+
{
478+
foreach ($input1 as $key => $value) {
479+
if (!array_key_exists($key, $input2)) {
480+
return false;
481+
} else {
482+
if (!self::isStructureSame($value, $input2[$key])) {
483+
return false;
484+
}
485+
}
486+
}
487+
488+
return true;
489+
}
490+
456491
/**
457492
* Checks whether $subArray is contained in $array
458493
*
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
use SimpleArrayLibrary\SimpleArrayLibrary;
4+
5+
class IsStructureSameTest extends PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @param array $data
9+
*
10+
* @return void
11+
* @dataProvider getData
12+
*/
13+
public function test_function(array $data)
14+
{
15+
// invoke logic & test
16+
$this->assertEquals($data['expResult'], SimpleArrayLibrary::isStructureSame($data['array1'], $data['array2']));
17+
}
18+
19+
/**
20+
* @return array
21+
*/
22+
public function getData()
23+
{
24+
return array(
25+
// #0 neither is arrays
26+
array(
27+
array(
28+
'array1' => 1,
29+
'array2' => 'a',
30+
'expResult' => true
31+
)
32+
),
33+
// #1 empty arrays
34+
array(
35+
array(
36+
'array1' => array(),
37+
'array2' => array(),
38+
'expResult' => true
39+
)
40+
),
41+
// #2 equal arrays
42+
array(
43+
array(
44+
'array1' => array(array(array()), array(), array(array(), array())),
45+
'array2' => array(array(array()), array(), array(array(), array())),
46+
'expResult' => true
47+
)
48+
),
49+
// #3 equal depths, no non-array leafs
50+
array(
51+
array(
52+
'array1' => array('a' => 1, 'b' => array(1)),
53+
'array2' => array('a' => 2),
54+
'expResult' => false
55+
)
56+
),
57+
// #4 equal keys, different values
58+
array(
59+
array(
60+
'array1' => array('a' => 1, 'b' => 'a'),
61+
'array2' => array('a' => 2, 'b' => 1.1),
62+
'expResult' => true
63+
)
64+
),
65+
// #5 array & non-array
66+
array(
67+
array(
68+
'array1' => array('a' => 1, 'b' => array(1)),
69+
'array2' => 4,
70+
'expResult' => false
71+
)
72+
),
73+
// #6 arrays different depths
74+
array(
75+
array(
76+
'array1' => array('a' => array(1)),
77+
'array2' => array('a' => 2),
78+
'expResult' => false
79+
)
80+
),
81+
// #7 arrays non same keys, same values
82+
array(
83+
array(
84+
'array1' => array('a' => 1, 'b' => array(1)),
85+
'array2' => array('c' => 1, 'd' => array(1)),
86+
'expResult' => false
87+
)
88+
),
89+
);
90+
}
91+
}

0 commit comments

Comments
 (0)