| 1 |
<?php |
| 2 |
|
| 3 |
abstract class AbstractSerializer |
| 4 |
{ |
| 5 |
protected $header = array(); |
| 6 |
protected $footer = array(); |
| 7 |
protected $indent = "\t"; |
| 8 |
|
| 9 |
|
| 10 |
abstract public function toObject($raw, $id=null); |
| 11 |
abstract public function toString($raw, $id=null); |
| 12 |
abstract protected function convertToRaw($object); |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
abstract public function ids(); |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
abstract public function set($id, $value); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
abstract protected function getString($id); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function setObject($id, $value) { |
| 34 |
$raw = $this->toRaw($value, $id); |
| 35 |
$this->set($id, $raw); |
| 36 |
return $raw; |
| 37 |
} |
| 38 |
|
| 39 |
protected function buildResources(&$node, $object) { |
| 40 |
if (!empty($object['#resource'])) { |
| 41 |
$node['#resources'][] = array(); |
| 42 |
$resources_node = &$node['#resources'][count($node['#resources']) - 1]; |
| 43 |
foreach ($object['#resource'] as $resource) { |
| 44 |
$resources_node['#resource'][] = array(); |
| 45 |
$resource_node = &$resources_node['#resource'][count($resources_node['#resource']) - 1]; |
| 46 |
$resource_node['content'] = $resource; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if (!empty($object['#provides'])) { |
| 51 |
$node['#provides'][] = array(); |
| 52 |
$provides_node = &$node['#provides'][count($node['#provides']) - 1]; |
| 53 |
foreach ($object['#provides'] as $provide) { |
| 54 |
$provides_node['#provide'][] = array(); |
| 55 |
$provide_node = &$provides_node['#provide'][count($provides_node['#provide']) - 1]; |
| 56 |
$provide_node['content'] = $provide; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
protected function buildMethod(&$method_node, $method, $method_id, $child = TRUE) { |
| 62 |
if ($child) { |
| 63 |
$this->buildResources($method_node, $method); |
| 64 |
|
| 65 |
$method_node['@name'] = $method_id; |
| 66 |
|
| 67 |
if ($method_id == 'preamble' || $method_id == 'postscript') { |
| 68 |
$method_node['@constructor'] = $method_id; |
| 69 |
} |
| 70 |
|
| 71 |
if ($method['instance'] && $method['prototype']) { |
| 72 |
$method_node['@scope'] = 'instance-prototype'; |
| 73 |
} |
| 74 |
elseif($method['instance']) { |
| 75 |
$method_node['@scope'] = 'instance'; |
| 76 |
} |
| 77 |
elseif($method['prototype']) { |
| 78 |
$method_node['@scope'] = 'prototype'; |
| 79 |
} |
| 80 |
else { |
| 81 |
$method_node['@scope'] = 'normal'; |
| 82 |
} |
| 83 |
|
| 84 |
if (!empty($method['tags'])) { |
| 85 |
$method_node['@tags'] = implode(' ', $method['tags']); |
| 86 |
} |
| 87 |
|
| 88 |
if ($method['private']) { |
| 89 |
$method_node['@private'] = true; |
| 90 |
} |
| 91 |
|
| 92 |
if ($method['private_parent']) { |
| 93 |
$method_node['@privateparent'] = true; |
| 94 |
} |
| 95 |
|
| 96 |
if (trim($method['summary'])) { |
| 97 |
$method_node['#summary'][] = array(); |
| 98 |
$description_node = &$method_node['#summary'][count($method_node['#summary']) - 1]; |
| 99 |
$description_node['content'] = $method['summary']; |
| 100 |
} |
| 101 |
|
| 102 |
if (trim($method['description'])) { |
| 103 |
$method_node['#description'][] = array(); |
| 104 |
$description_node = &$method_node['#description'][count($method_node['#description']) - 1]; |
| 105 |
$description_node['content'] = $method['description']; |
| 106 |
} |
| 107 |
|
| 108 |
if (!empty($method['examples'])) { |
| 109 |
$method_node['#examples'][] = array(); |
| 110 |
$examples_node = &$method_node['#examples'][count($method_node['#examples']) - 1]; |
| 111 |
foreach ($method['examples'] as $example) { |
| 112 |
$examples_node['#example'][] = array(); |
| 113 |
$example_node = &$examples_node['#example'][count($examples_node['#example']) - 1]; |
| 114 |
$example_node['content'] = $example; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if (trim($method['return_summary'])) { |
| 120 |
$method_node['#return-description'][] = array(); |
| 121 |
$description_node = &$method_node['#return-description'][count($method_node['#return-description']) - 1]; |
| 122 |
$description_node['content'] = $method['return_summary']; |
| 123 |
} |
| 124 |
|
| 125 |
if (!empty($method['parameters'])) { |
| 126 |
$method_node['#parameters'][] = array(); |
| 127 |
$parameters_node = &$method_node['#parameters'][count($method_node['#parameters']) - 1]; |
| 128 |
foreach ($method['parameters'] as $parameter_name => $parameter) { |
| 129 |
$parameters_node['#parameter'][] = array(); |
| 130 |
$parameter_node = &$parameters_node['#parameter'][count($parameters_node['#parameter']) - 1]; |
| 131 |
$parameter_node['@name'] = $parameter_name; |
| 132 |
$parameter_node['@type'] = $parameter['type']; |
| 133 |
$parameter_node['@usage'] = ($parameter['optional']) ? 'optional' : (($parameter['repeating']) ? 'one-or-more' : 'required'); |
| 134 |
if ($parameter['summary']) { |
| 135 |
$parameter_node['#summary'][] = array(); |
| 136 |
$description_node = &$parameter_node['#summary'][count($parameter_node['#summary']) - 1]; |
| 137 |
$description_node['content'] = $parameter['summary']; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (!empty($method['returns'])) { |
| 143 |
$method_node['#return-types'][] = array(); |
| 144 |
$returns_node = &$method_node['#return-types'][count($method_node['#return-types']) - 1]; |
| 145 |
foreach ($method['returns'] as $return) { |
| 146 |
$returns_node['#return-type'][] = array(); |
| 147 |
$return_node = &$returns_node['#return-type'][count($returns_node['#return-type']) - 1]; |
| 148 |
$return_node['@type'] = $return; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $method_node; |
| 153 |
} |
| 154 |
|
| 155 |
public function toAbstract($object, $id) { |
| 156 |
$object_node = array(); |
| 157 |
|
| 158 |
$object_node['@location'] = $id; |
| 159 |
|
| 160 |
$this->buildResources($object_node, $object); |
| 161 |
|
| 162 |
$methods_node = NULL; |
| 163 |
if ($object['type'] == 'Function') { |
| 164 |
$object_node['@type'] = 'Function'; |
| 165 |
if (!$object['classlike']) { |
| 166 |
$this->buildMethod($object_node, $object, $object['location'], FALSE); |
| 167 |
} |
| 168 |
else { |
| 169 |
$object_node['@classlike'] = TRUE; |
| 170 |
|
| 171 |
$methods_node = array(); |
| 172 |
$method_node = array( |
| 173 |
'@constructor' => 'constructor' |
| 174 |
); |
| 175 |
if (!empty($object['chains']['prototype'])) { |
| 176 |
$superclass = array_shift($object['chains']['prototype']); |
| 177 |
$object_node['@superclass'] = $superclass; |
| 178 |
} |
| 179 |
$methods_node['#method'][] = $this->buildMethod($method_node, $object, $object['location'], FALSE); |
| 180 |
} |
| 181 |
} |
| 182 |
elseif ($object['type'] != 'Object') { |
| 183 |
$object_node['@type'] = $object['type']; |
| 184 |
} |
| 185 |
|
| 186 |
if (!empty($object['tags'])) { |
| 187 |
$object_node['@tags'] = implode(' ', $object['tags']); |
| 188 |
} |
| 189 |
|
| 190 |
if ($object['private']) { |
| 191 |
$object_node['@private'] = true; |
| 192 |
} |
| 193 |
|
| 194 |
if ($object['private_parent']) { |
| 195 |
$object_node['@privateparent'] = true; |
| 196 |
} |
| 197 |
|
| 198 |
if (trim($object['summary'])) { |
| 199 |
$object_node['#summary'][] = array(); |
| 200 |
$description_node = &$object_node['#summary'][count($object_node['#summary']) - 1]; |
| 201 |
$description_node['content'] = $object['summary']; |
| 202 |
} |
| 203 |
|
| 204 |
if (trim($object['description'])) { |
| 205 |
$object_node['#description'][] = array(); |
| 206 |
$description_node = &$object_node['#description'][count($object_node['#description']) - 1]; |
| 207 |
$description_node['content'] = $object['description']; |
| 208 |
} |
| 209 |
|
| 210 |
if (!empty($object['examples'])) { |
| 211 |
$object_node['#examples'][] = array(); |
| 212 |
$examples_node = &$object_node['#examples'][count($object_node['#examples']) - 1]; |
| 213 |
foreach ($object['examples'] as $example) { |
| 214 |
$examples_node['#example'][] = array(); |
| 215 |
$example_node = &$examples_node['#example'][count($examples_node['#example']) - 1]; |
| 216 |
$example_node['content'] = $example; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
$mixins = array(); |
| 221 |
if (!empty($object['chains']['prototype'])) { |
| 222 |
foreach ($object['chains']['prototype'] as $mixin) { |
| 223 |
|
| 224 |
$mixins['prototype']['prototype'][] = $mixin; |
| 225 |
} |
| 226 |
} |
| 227 |
if (!empty($object['mixins']['prototype'])) { |
| 228 |
foreach ($object['mixins']['prototype'] as $mixin) { |
| 229 |
if (strlen($mixin) > 10 && substr($mixin, -10) == '.prototype') { |
| 230 |
$mixins['prototype']['prototype'][] = substr($mixin, 0, -10); |
| 231 |
} |
| 232 |
else { |
| 233 |
$mixins['prototype']['normal'][] = $mixin; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
if (!empty($object['chains']['call'])) { |
| 238 |
foreach ($object['chains']['call'] as $mixin) { |
| 239 |
$mixins['instance']['instance'][] = $mixin; |
| 240 |
} |
| 241 |
} |
| 242 |
if (!empty($object['mixins']['normal'])) { |
| 243 |
foreach ($object['mixins']['normal'] as $mixin) { |
| 244 |
if (strlen($mixin) > 10 && substr($mixin, -10) == '.prototype') { |
| 245 |
$mixins['normal']['prototype'][] = substr($mixin, 0, -10); |
| 246 |
} |
| 247 |
else { |
| 248 |
$mixins['normal']['normal'][] = $mixin; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
foreach ($mixins as $scope => $mixins) { |
| 254 |
$object_node['#mixins'][] = array(); |
| 255 |
$mixins_node = &$object_node['#mixins'][count($object_node['#mixins']) - 1]; |
| 256 |
$mixins_node['@scope'] = $scope; |
| 257 |
foreach ($mixins as $scope => $mixins) { |
| 258 |
foreach (array_unique($mixins) as $mixin) { |
| 259 |
$mixins_node['#mixin'][] = array(); |
| 260 |
$mixin_node = &$mixins_node['#mixin'][count($mixins_node['#mixin']) - 1]; |
| 261 |
$mixin_node['@scope'] = $scope; |
| 262 |
$mixin_node['@location'] = $mixin; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
$methods = array(); |
| 268 |
$properties = array(); |
| 269 |
if (!empty($object['#children'])) { |
| 270 |
foreach ($object['#children'] as $child_id => $child) { |
| 271 |
if ($child['type'] == 'Function') { |
| 272 |
$methods[$child_id] = $child; |
| 273 |
} |
| 274 |
else { |
| 275 |
$properties[$child_id] = $child; |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
if (!empty($properties)) { |
| 281 |
$object_node['#properties'][] = array(); |
| 282 |
$properties_node = &$object_node['#properties'][count($object_node['#properties']) - 1]; |
| 283 |
foreach ($properties as $property_id => $property) { |
| 284 |
$properties_node['#property'][] = array(); |
| 285 |
$property_node = &$properties_node['#property'][count($properties_node['#property']) - 1]; |
| 286 |
|
| 287 |
$property_node['@name'] = $property_id; |
| 288 |
|
| 289 |
$this->buildResources($property_node, $property); |
| 290 |
|
| 291 |
if ($property['instance'] && $property['prototype']) { |
| 292 |
$property_node['@scope'] = 'instance-prototype'; |
| 293 |
} |
| 294 |
elseif($property['instance']) { |
| 295 |
$property_node['@scope'] = 'instance'; |
| 296 |
} |
| 297 |
elseif($property['prototype']) { |
| 298 |
$property_node['@scope'] = 'prototype'; |
| 299 |
} |
| 300 |
else { |
| 301 |
$property_node['@scope'] = 'normal'; |
| 302 |
} |
| 303 |
|
| 304 |
$property_node['@type'] = $property['type']; |
| 305 |
|
| 306 |
if (!empty($property['tags'])) { |
| 307 |
$property_node['@tags'] = implode(' ', $property['tags']); |
| 308 |
} |
| 309 |
|
| 310 |
if ($property['private']) { |
| 311 |
$property_node['@private'] = true; |
| 312 |
} |
| 313 |
|
| 314 |
if ($property['private_parent']) { |
| 315 |
$property_node['@privateparent'] = true; |
| 316 |
} |
| 317 |
|
| 318 |
if ($property['summary']) { |
| 319 |
$property_node['#summary'][] = array(); |
| 320 |
$description_node = &$property_node['#summary'][count($property_node['#summary']) - 1]; |
| 321 |
$description_node['content'] = $property['summary']; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
if (!empty($methods)) { |
| 327 |
if (!$methods_node) { |
| 328 |
$methods_node = array(); |
| 329 |
} |
| 330 |
|
| 331 |
foreach ($methods as $method_id => $method) { |
| 332 |
$method_node = array(); |
| 333 |
$methods_node['#method'][] = $this->buildMethod($method_node, $method, $method_id); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if ($methods_node) { |
| 338 |
$object_node['#methods'][] = $methods_node; |
| 339 |
} |
| 340 |
|
| 341 |
return $object_node; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public function toRaw($object, $id) { |
| 348 |
return $this->convertToRaw($this->toAbstract($object, $id)); |
| 349 |
} |
| 350 |
} |