Util.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * This file is part of amfPHP
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the license that is bundled
  8. * with this package in the file license.txt.
  9. */
  10. /**
  11. * utils for Amf handling
  12. *
  13. * @package Amfphp_Core_Amf
  14. * @author Ariel Sommeria-klein
  15. */
  16. class Amfphp_Core_Amf_Util {
  17. /**
  18. * looks if the system is Big Endain or not
  19. * @return <Boolean>
  20. */
  21. static public function isSystemBigEndian() {
  22. $tmp = pack('d', 1); // determine the multi-byte ordering of this machine temporarily pack 1
  23. return ($tmp == "\0\0\0\0\0\0\360\77");
  24. }
  25. /**
  26. * applies a function to all objects contained by $obj and $obj itself.
  27. * iterates on $obj and its sub objects, which can iether be arrays or objects
  28. * @param mixed $obj the object/array that will be iterated on
  29. * @param array $callBack the function to apply to obj and subobjs. must take 1 parameter, and return the modified object
  30. * @param int $recursionDepth current recursion depth. The first call should be made with this set 0. default is 0
  31. * @param int $maxRecursionDepth default is 30
  32. * @param bool $ignoreAmfTypes ignore objects with type in Amfphp_Core_Amf_Types package. could maybe be replaced by a regexp, but this is better for performance
  33. * @return mixed array or object, depending on type of $obj
  34. */
  35. static public function applyFunctionToContainedObjects($obj, $callBack, $recursionDepth = 0, $maxRecursionDepth = 30, $ignoreAmfTypes = true) {
  36. if ($recursionDepth == $maxRecursionDepth) {
  37. throw new Amfphp_Core_Exception("couldn't recurse deeper on object. Probably a looped reference");
  38. }
  39. //don't apply to Amfphp types such as byte array
  40. if ($ignoreAmfTypes && is_object($obj) && substr(get_class($obj), 0, 21) == 'Amfphp_Core_Amf_Types') {
  41. return $obj;
  42. }
  43. //apply callBack to obj itself
  44. $obj = call_user_func($callBack, $obj);
  45. //if $obj isn't a complex type don't go any further
  46. if (!is_array($obj) && !is_object($obj)) {
  47. return $obj;
  48. }
  49. foreach ($obj as $key => $data) { // loop over each element
  50. $modifiedData = null;
  51. if (is_object($data) || is_array($data)) {
  52. //data is complex, so don't apply callback directly, but recurse on it
  53. $modifiedData = self::applyFunctionToContainedObjects($data, $callBack, $recursionDepth + 1, $maxRecursionDepth);
  54. } else {
  55. //data is simple, so apply data
  56. $modifiedData = call_user_func($callBack, $data);
  57. }
  58. //store converted data
  59. if (is_array($obj)) {
  60. $obj[$key] = $modifiedData;
  61. } else {
  62. $obj->$key = $modifiedData;
  63. }
  64. }
  65. return $obj;
  66. }
  67. }
  68. ?>