<?php
//=============================================================================
//
// Description:
// Miscellaneous functions
//
// Functions:
// typeof( Mixed $var )
// constantEval( String $string )
// varSize( Mixed $var )
// byteOrd( Int $bytes, Int $round )
// array2eventData( Array $array )
// eventData_merge( EventData $first, EventData $second )
// arrayContents( Array $variable )
// boolVal( Mixed $value )
//
//=============================================================================
function array2eventData($array)
{
//===========================================================================
// Function: array2eventData
// Author: Bob Jackman
// Date: 2008-10-21
// Description: Convert all elements of an array to an EventData object
//
// Params:
// $array {array} Required: array of elements to convert to an ED object
//
// Returns: {EventData} Resultant EventData object
//===========================================================================
$eventData = new EventData();
if (is_array($array)) // -- passed argument is an array
{
foreach ($array as $key => $value) // -- loop through all elements
{
$eventData->setAttribute($key, $value);
}
}
else // -- passed argument is not an array
{
$eventData->setAttribute(0, $array);
}
return $eventData;
}
function arrayContents($variable)
{
//===========================================================================
// Function: arrayContents
// Author: Bob Jackman
// Date: ????
// Description: output the contents of an array
//
// Params:
// $variable {mixed} Required: Variable to dump
//
// Returns: {void}
//===========================================================================
// ==================== Begin Output
echo '<pre>';
if (is_array($variable) || is_object($variable)) // -- variable is an array or an object
{
echo print_r($variable, true);
}
else // -- variable is some singular value
{
if ($variable === false) // -- variable is a boolean false
{
$variable = "false";
}
elseif ($variable === true) // -- variable is a boolean true
{
$variable = "true";
}
else // -- variable is some other type
{
}
echo 'Singular: '.$variable;
}
echo '</pre>';
}
function boolVal($value)
{
//===========================================================================
// Function: boolVal
// Author: Bob Jackman
// Date: 2008-10-21
// Description: Determine the Boolean value of passed param.
// Params:
// $value {Mixed} Required: Variable for which to determine the boolean
// value of
//
// Returns: {Bool} Resultant boolean value
//
// Notes:
// This function differs from a boolean cast in that a string of 'false'
// causes the return of a boolean FALSE rather than TRUE as a boolean cast
// would return.
//===========================================================================
if (is_numeric($value)) // -- numeric
{
if ($value == 0) // -- zero
{
$value = false;
}
else // -- non-zero
{
$value = true;
}
}
elseif (is_string($value)) // -- string
{
if (strtolower($value) == 'false') // -- false
{
$value = false;
}
else // -- anything else
{
$value = true;
}
}
elseif (!is_bool($value)) // -- not boolean
{
$value = true;
}
return $value;
}
function byteOrd($bytes, $round = 2)
{
//===========================================================================
// Function: byteOrd
// Author: Bob Jackman
// Date: 2008-10-21
// Description: Convert and round some number of bytes to it's ordinal
// equivalent. (kilobyte, megabyte, etc)
// Params:
// $bytes {Integer} Required: Number of bytes to convert
// $round {Integer} Optional: Number of decimal places to round to
// (Defaults to 2)
//
// Returns: {string} Converted number with units ordinal
//===========================================================================
// ==================== Prepare Ordinal Abbreviations
$ords = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$nextOrdThreshold = 0.9;
// ==================== Reduce Bytes to Whole Units
$ord = 0;
while ($bytes >= 1024 || (($bytes / 1024) >= $nextOrdThreshold))
{
$bytes = $bytes / 1024;
$ord++;
}
// ================== Round and Return with Ordinal
return round($bytes, $round).$ords[$ord];
}
function constantEval($string)
{
//===========================================================================
// Function: constantEval
// Author: Bob Jackman
// Date: ????
// Description: Evaluate the resultant value of bitwise operations
// performed on various constants passed in a string
// **does not use eval()**
// Params:
// $string {string} Required: Constants and bitwise operations to evaulate
//
// Returns: {mixed}
//===========================================================================
// ==================== Split String into Separate Constants and Operators
preg_match_all('/(\S*?)\s*([&|])\s*(\S*)/', $string, $matches);
if (sizeof($matches[0])) // -- string contains operators
{
$parts = array();
array_shift($matches); // -- throw away the first element of $matches
// ==================== Reorganize Matches Array for Easier Proccessing
$groups = sizeof($matches);
$subs = sizeof($matches[0]);
for ($i = 0; $i < $subs; $i++)
{
for ($j = 0; $j < $groups; $j++)
{
$parts[] = $matches[$j][$i];
}
}
// ==================== Calculate the Resultant Value of the Evaluated String
$result = 0;
$action = false;
foreach ($parts as $part) // -- loop through each part of the string
{
if (strstr($part, '&')) // -- bitwise AND operator
{
$action = '&';
}
elseif (strstr($part, '|')) // -- bitwise OR operator
{
$action = '|';
}
elseif (!empty($part))
{
if (strstr($part, '~')) // -- this is a negated constant
{
$const = substr($part, 1);
$const = ~constant($const);
}
else // -- this is a natural constant
{
$const = constant($part);
}
if ($action) // -- if there is a bitwise operation to perform
{
switch ($action)
{
case '&': // -- bitwise AND operation
$result &= $const;
break;
case '|': // -- bitwise OR operation
$result |= $const;
break;
}
}
else // -- there is no bitwise operation to perform
{
$result = $const;
}
$action = false; // -- reset action flag
}
}
// ==================== Return Final Calculated Value
return $result;
}
else // -- string does not contain operators
{
if (defined($string)) // -- constant is defined
{
return constant($string);
}
else // -- constant is not defined
{
return false;
}
}
}
function eventData_merge(EventData $first, EventData $second)
{
//===========================================================================
// Function: eventData_merge
// Author: Bob Jackman
// Date: 2008-10-21
// Description: Merge the contents of 2 EventData objects into a single
// EventData object
// Params:
// $first {EventData} Required: first EventData object
// $second {EventData} Required: second EventData object
//
// Returns: {EventData} Resultant, merged EventData object
//
// Notes:
// Attributes from the second EventData object override those of the first
//===========================================================================
$attributes2 = $second->getAttributes();
foreach($attributes2 as $key => $value) // -- loop through all of $second's attributes
{
$first->setAttribute($key, $value);
}
return $first;
}
function typeof($var)
{
//===========================================================================
// Function: typeof
// Author: Bob Jackman
// Date: 2008-10-21
// Description: Determine the datatype of the passed variable
// Params:
// $var {Mixed} Required: variable for which to determine the datatype
//
// Returns: {string} datatype of passed variable
//===========================================================================
if (is_string($var)) // -- string
{
return 'string';
}
elseif (is_float($var)) // -- float
{
return 'float';
}
elseif (is_bool($var)) // -- boolean
{
return 'bool';
}
elseif (is_int($var)) // -- integer
{
return 'int';
}
elseif (is_array($var)) // -- array
{
return 'array';
}
elseif (is_object($var)) // -- object
{
return get_class($var);
}
elseif (is_resource($var)) // -- resource
{
return 'resource';
}
else // -- unknown
{
return 'unknown';
}
}
function varSize($var)
{
//===========================================================================
// Function: varSize
// Author: Bob Jackman
// Date: 2008-10-21
// Description: Approximate the memory space required to store passed
// variable.
// Params:
// $var {Mixed} Required: variable for which to determine the memory size
//
// Returns: {float} memory used in bytes
//
// Notes:
// This function is an approximate estimate. For strings, this function
// assumes that 1 character = 1 byte. This is not true for all non-ascii
// characters. Objects are output buffered with var_dump() to obtain a
// string representation of the object, then the characters are counted.
// 1 character = 1 byte.
//===========================================================================
$size = 0.0;
if (is_array($var)) // -- array
{
foreach ($var as $index => $element) // -- loop through elements
{
$size += varSize($element);
}
}
elseif (is_object($var)) // -- object
{
ob_start();
var_dump($var);
$temp = ob_get_contents();
ob_end_clean();
$size += varSize($temp);
}
elseif (is_string($var)) // -- string
{
$size += strlen($var);
}
elseif (is_bool($var)) // -- boolean
{
$size += (1/8);
}
elseif (is_float($var)) // -- float
{
$size += 8;
}
elseif (is_int($var)) // -- integer
{
if ($var === 0)
{
$size += 1;
}
else
{
$bits = log(($var + 1), 2);
$bytes = ceil($bits / 8);
$size += $bytes;
}
}
elseif (is_resource($var)) // -- resource
{
// to do;
}
elseif (is_null($var)) //-- null
{
$size += 0;
}
else
{
// to do; what else is there?
}
return round($size);
}
?>