Value.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace IXR;
  3. /**
  4. * IXR值
  5. *
  6. * @package IXR
  7. */
  8. class Value
  9. {
  10. private $data;
  11. private $type;
  12. /**
  13. * @param mixed $data
  14. * @param bool|string $type
  15. */
  16. public function __construct($data, $type = false)
  17. {
  18. $this->data = $data;
  19. if (!$type) {
  20. $type = $this->calculateType();
  21. }
  22. $this->type = $type;
  23. if ($type == 'struct') {
  24. /* Turn all the values in the array in to new IXR_Value objects */
  25. foreach ($this->data as $key => $value) {
  26. $this->data[$key] = new Value($value);
  27. }
  28. }
  29. if ($type == 'array') {
  30. for ($i = 0, $j = count($this->data); $i < $j; $i++) {
  31. $this->data[$i] = new Value($this->data[$i]);
  32. }
  33. }
  34. }
  35. public function getXml(): string
  36. {
  37. /* Return XML for this value */
  38. switch ($this->type) {
  39. case 'boolean':
  40. return '<boolean>' . (($this->data) ? '1' : '0') . '</boolean>';
  41. case 'int':
  42. return '<int>' . $this->data . '</int>';
  43. case 'double':
  44. return '<double>' . $this->data . '</double>';
  45. case 'string':
  46. return '<string>' . htmlspecialchars($this->data) . '</string>';
  47. case 'array':
  48. $return = '<array><data>' . "\n";
  49. foreach ($this->data as $item) {
  50. $return .= ' <value>' . $item->getXml() . "</value>\n";
  51. }
  52. $return .= '</data></array>';
  53. return $return;
  54. case 'struct':
  55. $return = '<struct>' . "\n";
  56. foreach ($this->data as $name => $value) {
  57. $return .= " <member><name>$name</name><value>";
  58. $return .= $value->getXml() . "</value></member>\n";
  59. }
  60. $return .= '</struct>';
  61. return $return;
  62. case 'date':
  63. case 'base64':
  64. return $this->data->getXml();
  65. }
  66. return false;
  67. }
  68. /**
  69. * @return string
  70. */
  71. private function calculateType(): string
  72. {
  73. if ($this->data === true || $this->data === false) {
  74. return 'boolean';
  75. }
  76. if (is_integer($this->data)) {
  77. return 'int';
  78. }
  79. if (is_double($this->data)) {
  80. return 'double';
  81. }
  82. // Deal with IXR object types base64 and date
  83. if (is_object($this->data) && is_a($this->data, 'IXR_Date')) {
  84. return 'date';
  85. }
  86. if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) {
  87. return 'base64';
  88. }
  89. // If it is a normal PHP object convert it in to a struct
  90. if (is_object($this->data)) {
  91. $this->data = get_object_vars($this->data);
  92. return 'struct';
  93. }
  94. if (!is_array($this->data)) {
  95. return 'string';
  96. }
  97. /* We have an array - is it an array or a struct ? */
  98. if ($this->isStruct($this->data)) {
  99. return 'struct';
  100. } else {
  101. return 'array';
  102. }
  103. }
  104. private function isStruct($array): bool
  105. {
  106. /* Nasty function to check if an array is a struct or not */
  107. $expected = 0;
  108. foreach ($array as $key => $value) {
  109. if ((string)$key != (string)$expected) {
  110. return true;
  111. }
  112. $expected++;
  113. }
  114. return false;
  115. }
  116. }