Obj.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Luracast\Restler\Data;
  3. /**
  4. * Convenience class that converts the given object
  5. * in to associative array
  6. *
  7. * @category Framework
  8. * @package Restler
  9. * @author R.Arul Kumaran <arul@luracast.com>
  10. * @copyright 2010 Luracast
  11. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  12. * @link http://luracast.com/products/restler/
  13. *
  14. */
  15. class Obj
  16. {
  17. /**
  18. * @var bool|string|callable
  19. */
  20. public static $stringEncoderFunction = false;
  21. /**
  22. * @var bool|string|callable
  23. */
  24. public static $numberEncoderFunction = false;
  25. /**
  26. * @var array key value pairs for fixing value types using functions.
  27. * For example
  28. *
  29. * 'id'=>'intval' will make sure all values of the id properties
  30. * will be converted to integers intval function
  31. * 'password'=> null will remove all the password entries
  32. */
  33. public static $fix = array();
  34. /**
  35. * @var string character that is used to identify sub objects
  36. *
  37. * For example
  38. *
  39. * when Object::$separatorChar = '.';
  40. *
  41. * array('my.object'=>true) will result in
  42. *
  43. * array(
  44. * 'my'=>array('object'=>true)
  45. * );
  46. */
  47. public static $separatorChar = null;
  48. /**
  49. * @var bool set it to true when empty arrays, blank strings, null values
  50. * to be automatically removed from response
  51. */
  52. public static $removeEmpty = false;
  53. /**
  54. * @var bool set it to true to remove all null values from the result
  55. */
  56. public static $removeNull = false;
  57. /**
  58. * Convenience function that converts the given object
  59. * in to associative array
  60. *
  61. * @static
  62. *
  63. * @param mixed $object that needs to be converted
  64. *
  65. * @param bool $forceObjectTypeWhenEmpty when set to true outputs
  66. * actual type (array or
  67. * object) rather than
  68. * always an array when the
  69. * array/object is empty
  70. *
  71. * @return array
  72. */
  73. public static function toArray($object,
  74. $forceObjectTypeWhenEmpty = false)
  75. {
  76. //if ($object instanceof JsonSerializable) { //wont work on PHP < 5.4
  77. if (is_object($object)) {
  78. if (method_exists($object, 'jsonSerialize')) {
  79. $object = $object->jsonSerialize();
  80. } elseif (method_exists($object, '__sleep')) {
  81. $properties = $object->__sleep();
  82. $array = array();
  83. foreach ($properties as $key) {
  84. $value = self::toArray($object->{$key},
  85. $forceObjectTypeWhenEmpty);
  86. if (self::$stringEncoderFunction && is_string($value)) {
  87. $value = self::$stringEncoderFunction ($value);
  88. } elseif (self::$numberEncoderFunction && is_numeric($value)) {
  89. $value = self::$numberEncoderFunction ($value);
  90. }
  91. $array [$key] = $value;
  92. }
  93. return $array;
  94. }
  95. }
  96. if (is_array($object) || is_object($object)) {
  97. $count = 0;
  98. $array = array();
  99. foreach ($object as $key => $value) {
  100. if (
  101. is_string(self::$separatorChar) &&
  102. false !== strpos($key, self::$separatorChar)
  103. ) {
  104. list($key, $obj) = explode(self::$separatorChar, $key, 2);
  105. $object[$key][$obj] = $value;
  106. $value = $object[$key];
  107. }
  108. if (self::$removeEmpty && empty($value) && !is_numeric($value) && !is_bool($value)) {
  109. continue;
  110. } elseif (self::$removeNull && is_null($value)) {
  111. continue;
  112. }
  113. if (array_key_exists($key, self::$fix)) {
  114. if (isset(self::$fix[$key])) {
  115. $value = call_user_func(self::$fix[$key], $value);
  116. } else {
  117. continue;
  118. }
  119. }
  120. $value = self::toArray($value, $forceObjectTypeWhenEmpty);
  121. if (self::$stringEncoderFunction && is_string($value)) {
  122. $value = self::$encoderFunctionName ($value);
  123. } elseif (self::$numberEncoderFunction && is_numeric($value)) {
  124. $value = self::$numberEncoderFunction ($value);
  125. }
  126. $array [$key] = $value;
  127. $count++;
  128. }
  129. return $forceObjectTypeWhenEmpty && $count == 0 ? $object : $array;
  130. }
  131. return $object;
  132. }
  133. public function __get($name)
  134. {
  135. isset(self::$fix[$name]) ? self::$fix[$name] : null;
  136. }
  137. public function __set($name, $function)
  138. {
  139. self::$fix[$name] = $function;
  140. }
  141. public function __isset($name)
  142. {
  143. return isset(self::$fix[$name]);
  144. }
  145. public function __unset($name)
  146. {
  147. unset(self::$fix[$name]);
  148. }
  149. }