Skip to content

Instantly share code, notes, and snippets.

@bastman
Created January 8, 2012 10:19
Show Gist options
  • Save bastman/1577928 to your computer and use it in GitHub Desktop.
Save bastman/1577928 to your computer and use it in GitHub Desktop.
Lib_Utils_Array_Dictionary (php)
<?php
/**
* Created by JetBrains PhpStorm.
* User: VAIO
* Date: 02.11.11
* Time: 09:55
* To change this template use File | Settings | File Templates.
*/
class Lib_Utils_Array_Dictionary
{
/**
* @static
* @param $array
* @return int
*/
public static function countKeys($array)
{
$result = 0;
if(!is_array($array)) {
return $result;
}
$keys = (array)array_keys($array);
$result=(int)count($keys);
return $result;
}
public static function keepKeys($array, $keyList)
{
if (!is_array($array)) {
$array=array();
}
if (!is_array($keyList)) {
$keyList=array();
}
$result = array();
foreach($array as $key => $value) {
if (in_array($key, $keyList, true)) {
$result[$key] = $value;
}
}
return $result;
}
public static function removeKeys($array, $keyList)
{
if (!is_array($array)) {
$array=array();
}
if (!is_array($keyList)) {
$keyList=array();
}
$result = array();
foreach($array as $key => $value) {
if (!in_array($key, $keyList, true)) {
$result[$key] = $value;
}
}
return $result;
}
public static function ensureKeys($array, $keyList)
{
if (!is_array($array)) {
$array=array();
}
if (!is_array($keyList)) {
$keyList=array();
}
$result = array();
foreach($array as $key => $value) {
$result[$key] = $value;
}
foreach($keyList as $key) {
if (!array_key_exists($key, $result)) {
$result[$key] = null;
}
}
return $result;
}
/**
* @static
* @param $array
* @param array $keyList
* @return array
*/
public static function keepAndEnsureKeys($array, $keyList)
{
$result = self::keepKeys($array, $keyList);
$result = self::ensureKeys($result, $keyList);
return $result;
}
/**
* @static
* @param array|null|mixed $array
* @param array|null|mixed $mixin
* @return array
*/
public static function mixinOverride($array, $mixin)
{
$result = array();
if(!is_array($array)) {
$array = array();
}
if(!is_array($mixin)) {
$mixin = array();
}
foreach($array as $key => $value) {
$result[$key] = $value;
}
foreach($mixin as $key => $value) {
$result[$key] = $value;
}
return $result;
}
/**
* @static
* @param array|null $array1
* @param array|null $array2
* @param array|null $keyList
* @return bool
*/
public static function compareJsonIsEqual(
$array1, $array2, $keyList
) {
$result = false;
if (($array1 === null) && ($array2 === null)) {
return true;
}
if(is_array($keyList)) {
$array1 = self::keepAndEnsureKeys($array1, $keyList);
$_array1 = array();
foreach($keyList as $key) {
$value = $array1[$key];
$_array1[$key] = $value;
}
$array1 = $_array1;
$array2 = self::keepAndEnsureKeys($array2, $keyList);
$_array2 = array();
foreach($keyList as $key) {
$value = $array2[$key];
$_array2[$key] = $value;
}
$array2 = $_array2;
}
$array1Json = json_encode($array1);
if(!is_string($array1Json)) {
return $result;
}
$array2Json = json_encode($array2);
if(!is_string($array2Json)) {
return $result;
}
$array1checksum = md5($array1Json);
$array2checksum = md5($array2Json);
if($array1checksum !== $array2checksum) {
return $result;
}
$array1checksum = md5("foo_".$array1Json);
$array2checksum = md5("foo_".$array2Json);
if($array1checksum !== $array2checksum) {
return $result;
}
return true;
}
// ++++++++++++++++++++++++++++++++++++++++++++
/**
* @static
* @throws Exception
* @param array|null $array
* @param string $prefix
* @param bool $ignoreCase
* @param bool $removePrefix
* @return array
*/
public static function collectByKeyPrefix(
$array, $prefix, $ignoreCase, $removePrefix
)
{
if(!is_bool($ignoreCase)) {
throw new Exception("Invalid 'ignoreCase' at ".__METHOD__);
}
if(!is_bool($removePrefix)) {
throw new Exception("Invalid 'removePrefix' at ".__METHOD__);
}
$prefix = "".$prefix;
$result = array();
if (!is_array($array)) {
$array = array();
}
foreach($array as $key => $value) {
$key = "".$key;
if(!Lib_Utils_String::startsWith($key, $prefix, $ignoreCase)) {
continue;
}
if ($removePrefix) {
$key = Lib_Utils_String::removePrefix(
$key, $prefix, $ignoreCase
);
}
$result[$key] = $value;
}
return $result;
}
/**
* @static
* @throws Exception
* @param array|null $array
* @param string|null $prefix
* @param array|null $keyList
* @return array
*/
public static function addKeyPrefix($array, $prefix, $keyList)
{
$result = array();
if(!is_array($array)) {
$array = array();
}
if($keyList !== null) {
if(!is_array($keyList)) {
throw new Exception(
"Invalid parameter keyList at ".__METHOD__
);
}
}
$useKeyList = is_array($keyList);
$usePrefix = ($prefix !== null);
if($usePrefix) {
$prefix = "".$prefix;
}
foreach($array as $key => $value) {
$prefixedKey = $key;
if($usePrefix) {
$prefixedKey = $prefix.$key;
}
// prefix all keys
if(!$useKeyList) {
$result[$prefixedKey] = $value;
continue;
}
//prefix just a selection of keys
if(in_array($key, $keyList, true)) {
// do prefix
$result[$prefixedKey] = $value;
} else {
// do not prefix
$result[$key] = $value;
}
}
return $result;
}
// +++++++++++++++++++++++++++++++++++++++++++++
/*
public static function globFindKeyNamesMatchOne(
$array, $globPatternList, $globFlags
)
{
if (!is_array($array)) {
$array=array();
}
if (!is_array($globPatternList)) {
$globPatternList=array();
}
$result = array();
foreach($array as $key => $value) {
$matched = self::_globMatchOne($key, $globPatternList, $globFlags);
if ($matched) {
$result[] = $key;
}
}
return $result;
}
public static function globFindKeyNamesMatchAll(
$array, $globPatternList, $globFlags
)
{
if (!is_array($array)) {
$array=array();
}
if (!is_array($globPatternList)) {
$globPatternList=array();
}
$result = array();
foreach($array as $key => $value) {
$matched = self::_globMatchAll($key, $globPatternList, $globFlags);
if ($matched) {
$result[] = $key;
}
}
return $result;
}
protected static function _globMatchOne($string, $patternList, $flags)
{
$result = false;
if (!is_array($patternList)) {
return $result;
}
foreach($patternList as $pattern) {
if (fnmatch($pattern, $string, $flags)) {
return true;
}
}
return $result;
}
protected static function _globMatchAll($string, $patternList, $flags)
{
$result = false;
if (!is_array($patternList)) {
return $result;
}
$matchCount = 0;
foreach($patternList as $pattern) {
if (fnmatch($pattern, $string, $flags)) {
$matchCount++;
} else {
return $result;
}
}
return ($matchCount>0);
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment