/** * Renders data and sets headers for proper browser display * * @param string $zone optional * @param string $type optional * @param string $format optional * @param bool $return optional * @return mixed returns string if return is true, else it echo's the output */ public function render( $zone = null, $type = null, $format = null, $return = false ) { if ($zone !== null) { $this->zone = $zone; } if ($type !== null) { $this->type = $type; } if ($format !== null) { $this->format = strtolower( $format ); } switch ($this->type) { case 'd': $this->type = 'day'; break; case 'w': $this->type = 'week'; break; } $this->format = ! empty( $this->formats[$this->format] ) ? $this->format : 'xml'; $output = null; try { if (! in_array( $this->type, $this->app->types, true ) || ! in_array( $this->zone, $this->app->zones, true )) { throw new InvalidRequestException(); } $output = $this->app->get( $this->zone, $this->type, $this->format ); } catch ( InvalidRequestException $e ) { $err = array( 'msg' => 'Missing or invalid request parameters (zone or type)', 'code' => 1 ); } catch ( HTTPFetchException $e ) { $err = array( 'msg' => 'An error has occured contacting third party service', 'code' => 2 ); } catch ( Exception $e ) { $err = array( 'msg' => 'An error has occured', 'code' => 3 ); } if (isset( $e ) && $e instanceof Exception) { $output = (string) $this->error( $err, $this->format ); } if (! $return) { $ctype = ! empty( $this->formats[$this->format] ) ? $this->formats[$this->format] : 'application/octet-stream'; $charset = strtolower( $this->app->getOutputEncoding() ); header( "Content-Type: $ctype;charset=$charset" ); echo $output; } else { return $output; } } // render }}}