/** * Constructor * * @param io.streams.OutputStream out * @param int level default 6 * @throws lang.IllegalArgumentException if the level is not between 0 and 9 */ public function __construct(OutputStream $out, $level = 6) { if ($level < 0 || $level > 9) { throw new \lang\IllegalArgumentException('Level ' . $level . ' out of range [0..9]'); } // Write GZIP format header: // * ID1, ID2 (Identification, \x1F, \x8B) // * CM (Compression Method, 8 = deflate) // * FLG (Flags, use 0) // * MTIME (Modification time, Un*x timestamp) // * XFL (Extra flags, 2 = compressor used maximum compression) // * OS (Operating system, 255 = unknown) $out->write(pack('CCCCVCC', 0x1f, 0x8b, 8, 0, time(), 2, 255)); // Now, convert stream to file handle and append deflating filter $this->out = Streams::writeableFd($out); if (!($this->filter = stream_filter_append($this->out, 'zlib.deflate', STREAM_FILTER_WRITE, $level))) { fclose($this->out); $this->out = null; throw new \io\IOException('Could not append stream filter'); } $this->md = CRC32::digest(); }
/** * Write this result to an output stream * * @param io.streams.OutputStream out */ public function writeTo(\io\streams\OutputStream $out) { $out->write('<?php '); $out->write($this->source); $out->write("\n?>\n"); }
/** * Store to an output stream, e.g. a file * * @param io.streams.OutputStream out * @throws io.IOException */ public function store(\io\streams\OutputStream $out) { foreach (array_keys($this->_data) as $section) { $out->write('[' . $section . "]\n"); foreach ($this->_data[$section] as $key => $val) { if (';' == $key[0]) { $out->write("\n; " . $val . "\n"); } else { if (is_array($val)) { if (empty($val)) { $out->write($key . "=\n"); } else { if (0 === key($val)) { foreach ($val as $v) { $out->write($key . '[]=' . $this->quote($v) . "\n"); } } else { foreach ($val as $k => $v) { $out->write($key . '[' . $k . ']=' . $this->quote($v) . "\n"); } } } } else { $out->write($key . '=' . $this->quote($val) . "\n"); } } } $out->write("\n"); } }
/** * Encode PHP data into JSON via stream * * Gets the PHP data and a stream.<br/> * It converts the data to JSON and will put every atom into the stream as soon * as it is available.<br/> * The usage is similar to encode() except the second argument. * * @param var data * @param io.streams.OutputStream stream * @return boolean * @throws webservices.json.JsonException if the data could not be serialized */ public function encodeTo($data, OutputStream $stream) { switch (gettype($data)) { case 'string': $stream->write('"' . $this->escape(iconv(\xp::ENCODING, 'utf-8', $data)) . '"'); return true; case 'integer': $stream->write(strval($data)); return true; case 'double': $stream->write(strval($data)); return true; case 'boolean': $stream->write($data ? 'true' : 'false'); return true; case 'NULL': $stream->write('null'); return true; case 'array': if ($this->_isVector($data)) { // Bail out early on bordercase if (0 == sizeof($data)) { $stream->write('[ ]'); return true; } $stream->write('[ '); // Get first element $stream->write($this->encode(array_shift($data))); foreach ($data as $value) { $stream->write(' , ' . $this->encode($value)); } $stream->write(' ]'); return true; } else { // Reset array internal pointer reset($data); $stream->write('{ '); $value = each($data); $stream->write($this->encode((string) $value['key']) . ' : ' . $this->encode($value['value'])); while ($value = each($data)) { $stream->write(' , ' . $this->encode((string) $value['key']) . ' : ' . $this->encode($value['value'])); } $stream->write(' }'); return true; } case 'object': // Converts a string object into an normal json string if ($data instanceof \lang\types\String) { $stream->write('"' . $this->escape((string) $data->getBytes('utf-8')) . '"'); break; } if (!method_exists($data, '__sleep')) { $vars = get_object_vars($data); } else { $vars = []; foreach ($data->__sleep() as $var) { $vars[$var] = $data->{$var}; } } $this->encodeTo($vars, $stream); return true; default: throw new JsonException('Cannot encode data of type ' . gettype($data)); } }