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