Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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