Skip to content

Commit

Permalink
Merge pull request #5 from chadicus/fea/copy
Browse files Browse the repository at this point in the history
Add Arrays::copy & Arrays::copyEach
  • Loading branch information
jncarver authored Sep 14, 2020
2 parents 8958109 + a94c8df commit 6c4f92c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ composer require traderinteractive/filter-arrays

## Included Filters

#### Arrays::copy
This filter will copy values from the input array into the resulting array using the destination key map.
```php
$input = ['foo' => 1, 'bar' => 2];
$keyMap = ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar'];
$result = \TraderInteractive\Filter\Arrays::copy($input, $keyMap);
assert($result === ['FOO_VALUE' => 1, 'BAR_VALUE' => 2]);
```

#### Arrays::copyEach
This filter will copy values from each array within the input array into the resulting array using the destination key map.
```php
$input = [
['foo' => 1, 'bar' => 2],
['foo' => 3, 'bar' => 4],
];
$keyMap = ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar'];
$result = \TraderInteractive\Filter\Arrays::copyEach($input, $keyMap);
assert($result === [['FOO_VALUE' => 1, 'BAR_VALUE' => 2], ['FOO_VALUE' => 3, 'BAR_VALUE' => 4]]);
```
#### Arrays::in
This filter is a wrapper around `in_array` including support for strict equality testing.

Expand Down
55 changes: 49 additions & 6 deletions src/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ public static function flatten(array $value) : array
{
$result = [];

array_walk_recursive(
$value,
function ($item) use (&$result) {
$result[] = $item;
}
);
$callBack = function ($item) use (&$result) {
$result[] = $item;
};

array_walk_recursive($value, $callBack);

return $result;
}
Expand All @@ -106,4 +105,48 @@ public static function arrayize($value) : array

return $value;
}

/**
* Copies values from the $source array into a new array using the $keyMap for destination keys.
*
* @param array[] $source The arrays with values to be copied.
* @param array $keyMap mapping of dest keys to source keys. If $keyMap is associative, the keys will be the
* destination keys. If numeric the values will be the destination keys
*
* @return array
*/
public static function copyEach(array $source, array $keyMap) : array
{
$result = [];
foreach ($source as $sourceArray) {
$result[] = self::copy($sourceArray, $keyMap);
}

return $result;
}

/**
* Copies values from the $source array into a new array using the $keyMap for destination keys.
*
* @param array $source The array with values to be copied.
* @param array $keyMap mapping of dest keys to source keys. If $keyMap is associative, the keys will be the
* destination keys. If numeric the values will be the destination keys
*
* @return array
*/
public static function copy(array $source, array $keyMap) : array
{
$result = [];
foreach ($keyMap as $destinationKey => $sourceKey) {
if (is_int($destinationKey)) {
$destinationKey = $sourceKey;
}

if (array_key_exists($sourceKey, $source)) {
$result[$destinationKey] = $source[$sourceKey];
}
}

return $result;
}
}
51 changes: 51 additions & 0 deletions tests/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,55 @@ public function arrayizeConvertsNullToEmptyArray()
{
$this->assertSame([], Arrays::arrayize(null));
}

/**
* @test
* @covers ::copy
*/
public function copy()
{
$source = ['foo' => 1, 'bar' => 2, 'extra' => 3];
$keyMap = [
'far' => 'foo',
'bar',
];
$result = Arrays::copy($source, $keyMap);
$this->assertSame(
[
'far' => $source['foo'],
'bar' => $source['bar'],
],
$result
);
}

/**
* @test
* @covers ::copyEach
*/
public function copyEach()
{
$input = [
['foo' => 1, 'bar' => 2],
['foo' => 3, 'bar' => 4],
];
$keyMap = [
'far' => 'foo',
'bar',
];
$result = Arrays::copyEach($input, $keyMap);
$this->assertSame(
[
[
'far' => 1,
'bar' => 2,
],
[
'far' => 3,
'bar' => 4,
],
],
$result
);
}
}

0 comments on commit 6c4f92c

Please sign in to comment.