Serialize.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Twilio;
  3. class Serialize {
  4. private static function flatten($map, $result = array(), $previous = array()) {
  5. foreach ($map as $key => $value) {
  6. if (is_array($value)) {
  7. $result = self::flatten($value, $result, array_merge($previous, array($key)));
  8. } else {
  9. $result[join(".", array_merge($previous, array($key)))] = $value;
  10. }
  11. }
  12. return $result;
  13. }
  14. public static function prefixedCollapsibleMap($map, $prefix) {
  15. if (is_null($map) || $map == \Twilio\Values::NONE) {
  16. return array();
  17. }
  18. $flattened = self::flatten($map);
  19. $result = array();
  20. foreach ($flattened as $key => $value) {
  21. $result[$prefix . '.' . $key] = $value;
  22. }
  23. return $result;
  24. }
  25. public static function iso8601Date($dateTime) {
  26. if (is_null($dateTime) || $dateTime == \Twilio\Values::NONE) {
  27. return \Twilio\Values::NONE;
  28. }
  29. if (is_string($dateTime)) {
  30. return $dateTime;
  31. }
  32. $utcDate = clone $dateTime;
  33. $utcDate->setTimezone(new \DateTimeZone('UTC'));
  34. return $utcDate->format('Y-m-d');
  35. }
  36. public static function iso8601DateTime($dateTime) {
  37. if (is_null($dateTime) || $dateTime == \Twilio\Values::NONE) {
  38. return \Twilio\Values::NONE;
  39. }
  40. if (is_string($dateTime)) {
  41. return $dateTime;
  42. }
  43. $utcDate = clone $dateTime;
  44. $utcDate->setTimezone(new \DateTimeZone('UTC'));
  45. return $utcDate->format('Y-m-d\TH:i:s\Z');
  46. }
  47. public static function booleanToString($boolOrStr) {
  48. if (is_null($boolOrStr) || is_string($boolOrStr)) {
  49. return $boolOrStr;
  50. }
  51. return $boolOrStr ? 'True' : 'False';
  52. }
  53. public static function json_object($object) {
  54. trigger_error("Serialize::json_object has been deprecated in favor of Serialize::jsonObject", E_USER_NOTICE);
  55. return Serialize::jsonObject($object);
  56. }
  57. public static function jsonObject($object) {
  58. if (is_array($object)) {
  59. return json_encode($object);
  60. }
  61. return $object;
  62. }
  63. public static function map($values, $map_func) {
  64. if (!is_array($values)) {
  65. return $values;
  66. }
  67. return array_map($map_func, $values);
  68. }
  69. }