Browse Source

ArrayHelper update, unit tests added

tags/0.8
Paul Rock 7 years ago
parent
commit
fa615d670a
  1. 32
      src/Helpers/ArrayHelper.php
  2. 26
      tests/Helpers/ArrayHelperTest.php

32
src/Helpers/ArrayHelper.php

@ -11,32 +11,32 @@ namespace RouterOS\Helpers;
class ArrayHelper
{
/**
* Check if required keys in array
* Check if required single key in array of parameters
*
* @param string $key
* @param array $array
* @return bool
*/
public static function checkIfKeyNotExist(string $key, array $array): bool
{
return (!array_key_exists($key, $array) && empty($array[$key]));
}
/**
* Check if required keys in array of parameters
*
* @param array $keys
* @param array $array
* @return string|bool Return true if all fine, and string with name of key which was not found
* @return array|bool Return true if all fine, and string with name of key which was not found
*/
public static function checkIfKeysNotExist(array $keys, array $array)
{
$output = [];
foreach ($keys as $key) {
if (!array_key_exists($key, $array) && isset($array[$key])) {
if (!array_key_exists($key, $array) && empty($array[$key])) {
$output[] = $key;
}
}
return !empty($output) ? implode(',', $output) : true;
}
/**
* Check if key in list of parameters
*
* @param string $key
* @param array $array
* @return bool
*/
public static function checkIfKeyNotExist(string $key, array $array): bool
{
return (!array_key_exists($key, $array));
return !empty($output) ? $output : true;
}
}

26
tests/Helpers/ArrayHelperTest.php

@ -0,0 +1,26 @@
<?php
namespace RouterOS\Helpers;
use PHPUnit\Framework\TestCase;
class ArrayHelperTest extends TestCase
{
public function testCheckIfKeyNotExist()
{
$test1 = ArrayHelper::checkIfKeyNotExist(1, [0 => 'a', 1 => 'b', 2 => 'c']);
$this->assertFalse($test1);
$test2 = ArrayHelper::checkIfKeyNotExist('a', [1 => 'a', 2 => 'b', 3 => 'c']);
$this->assertTrue($test2);
}
public function testCheckIfKeysNotExist()
{
$test1 = ArrayHelper::checkIfKeysNotExist([1, 2], [0 => 'a', 1 => 'b', 2 => 'c']);
$this->assertTrue($test1);
$test2 = ArrayHelper::checkIfKeysNotExist([3, 4], [0 => 'a', 1 => 'b', 2 => 'c']);
$this->assertNotTrue($test2);
}
}
Loading…
Cancel
Save