Created
December 3, 2011 14:09
-
-
Save bastman/1427212 to your computer and use it in GitHub Desktop.
Lib_Utils_Vector_UnsignedInt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Lib_Utils_Vector_UnsignedInt | |
*/ | |
class Lib_Utils_Vector_UnsignedInt | |
{ | |
/** | |
* @static | |
* @param $array|null | |
* @param array|null $excludeValues | |
* @param bool $castFromString | |
* @return array | |
*/ | |
public static function filter($array, $excludeValues, $castFromString) | |
{ | |
$result = array(); | |
if (is_array($array)!==true) { | |
return $result; | |
} | |
if (is_array($excludeValues)!==true) { | |
$excludeValues = array(); | |
} | |
$castFromString = (bool)$castFromString; | |
if ($castFromString === true) { | |
$_excludeValues = array(); | |
foreach ($excludeValues as $excludeValue) { | |
$excludeValueCasted = self::_asUnsignedInt( | |
$excludeValue, | |
null | |
); | |
$_excludeValues[]=$excludeValue; | |
if ($excludeValueCasted !== null) { | |
$_excludeValues[] = $excludeValueCasted; | |
} | |
} | |
$excludeValues = $_excludeValues; | |
} | |
$_array = array(); | |
foreach ($array as $value) { | |
if ((is_int($value)!==true) && ($castFromString === true)) { | |
$value = self::_asUnsignedInt($value, null); | |
if ($value === null) { | |
continue; | |
} | |
} | |
if (((is_int($value)) && ($value>=0)) !==true) { | |
continue; | |
} | |
if ((in_array($value, $excludeValues, true)) === true) { | |
continue; | |
} | |
$_array[] = $value; | |
} | |
return $_array; | |
} | |
/** | |
* @static | |
* @param mixed $value | |
* @param mixed $defaultValue | |
* @return int|mixed | |
*/ | |
protected static function _asUnsignedInt($value, $defaultValue=null) | |
{ | |
$result = $defaultValue; | |
if ($value===null) { | |
return $result; | |
} | |
if ( (is_string($value)) && (ctype_digit($value)) ) { | |
$value = (int)$value; | |
} | |
if ((is_int($value)) && ($value>=0)) { | |
return $value; | |
} | |
return $result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment