Skip to content

Commit

Permalink
Added Arr::whiteList()
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliodeg authored and niden committed Jun 14, 2019
1 parent df6fa15 commit 99066eb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Added `Phalcon\Http\PayloadFactory`: Factory to create payload objects. [#14123](https://github.com/phalcon/cphalcon/issues/14123)
- Added `Phalcon\Http\Message\ServerRequestFactory::load`: Method to create a `ServerRequest` object from globals. [#14154](https://github.com/phalcon/cphalcon/pull/14154)
- Added `Phalcon\Collection\ReadCollection`: Read only collection. [#14154](https://github.com/phalcon/cphalcon/pull/14154)
- Added `whiteList()` to `Phalcon\Helper\Arr` [#13954](https://github.com/phalcon/cphalcon/pull/13954)

## Changed
- Renamed `Phalcon\Annotations\Adapter\Files` to `Phalcon\Annotations\Adapter\Stream`. [#13672](https://github.com/phalcon/cphalcon/issues/13672)
Expand Down
22 changes: 5 additions & 17 deletions phalcon/Helper/Arr.zep
Original file line number Diff line number Diff line change
Expand Up @@ -407,32 +407,20 @@ class Arr
*/
final public static function whiteList(array! collection, array! whiteList) -> array
{
array result;


/**
* Clean whitelist, just strings and integers
*/
let whiteList = array_filter(
whiteList,
function (element)
{
if (is_int(element)) {
return true;
}

if (is_string(element)) {
let element = trim(element);

return mb_strlen(element) > 0;
}

return false;
return is_int(element) || is_string(element);
}
);

let result = array_intersect_key(
return array_intersect_key(
collection,
array_flip(whiteList)
);

return result;
}
}
56 changes: 56 additions & 0 deletions tests/unit/Helper/Arr/WhiteListCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Unit\Helper\Arr;

use Phalcon\Helper\Arr;
use UnitTester;

class WhiteListCest
{
/**
* Unit Tests Phalcon\Helper\Arr :: whiteList()
*
* @author Phalcon Team <[email protected]>
* @since 2019-06-14
*/
public function helperArrWhiteList(UnitTester $I)
{
$I->wantToTest('Helper\Arr - whiteList()');

$value = [
'value-1',
' key ' => 'value-2',
5 => 'value-3',
6 => 'value-4',
7 => 'value-5',
' key-2' => 'value-6',
'key-3 ' => 'value-7',
'key-4' => 'value-8',
];

$whiteList = [
7, 5, 0, 'key-3 ', null, -13, new \stdClass(), [], 3.1415,
];

$expected = [
0 => 'value-1',
5 => 'value-3',
7 => 'value-5',
'key-3 ' => 'value-7',
];

$actual = Arr::whiteList($value, $whiteList);

$I->assertSame($expected, $actual);
}
}

0 comments on commit 99066eb

Please sign in to comment.