/** * Serialize * * @param var $payload * @param io.streams.OutputStream $out * @return void */ public function serialize($payload, $out) { $sep = ''; foreach ($payload as $key => $value) { $out->write($sep . $key . '=' . urlencode($value)); if ('' === $sep) { $sep = '&'; } } }
/** * Serialize * * @param var $payload * @param io.streams.OutputStream $out * @return void */ public function serialize($payload, $out) { $t = new Tree(); $t->setEncoding('UTF-8'); if ($payload instanceof Payload) { $root = isset($payload->properties['name']) ? $payload->properties['name'] : 'root'; $val = $payload->value; } else { $root = 'root'; $val = $payload; } $out->write($t->getDeclaration() . "\n"); if ($val instanceof \Traversable) { $i = 0; $map = null; foreach ($val as $key => $element) { if (0 === $i++) { $out->write('<' . $root . '>'); $map = 0 !== $key; } $out->write($this->node($map ? $key : $root, $element)->getSource(INDENT_NONE, 'utf-8')); } if (null === $map) { $out->write('<' . $root . '/>'); } else { $out->write('</' . $root . '>'); } } else { $out->write($t->withRoot($this->node($root, $val))->getSource(INDENT_NONE)); } }