/** * Register default mime handlers. Is idempotent. */ public static function registerHandlers() { if (self::$registered === true) { return; } // @todo check a conf file to load from that instead of // hardcoding into the library? $handlers = array(\HttpFull\Mime::JSON => new \HttpFull\Handlers\JsonHandler(), \HttpFull\Mime::XML => new \HttpFull\Handlers\XmlHandler(), \HttpFull\Mime::FORM => new \HttpFull\Handlers\FormHandler(), \HttpFull\Mime::CSV => new \HttpFull\Handlers\CsvHandler()); foreach ($handlers as $mime => $handler) { // Don't overwrite if the handler has already been registered if (Httpfull::hasParserRegistered($mime)) { continue; } Httpfull::register($mime, $handler); } self::$registered = true; }
/** * Parse the response into a clean data structure * (most often an associative array) based on the expected * Mime type. * @param string Http response body * @return array|string|object the response parse accordingly */ public function _parse($body) { // If the user decided to forgo the automatic // smart parsing, short circuit. if (!$this->request->auto_parse) { return $body; } // If provided, use custom parsing callback if (isset($this->request->parse_callback)) { return call_user_func($this->request->parse_callback, $body); } // Decide how to parse the body of the response in the following order // 1. If provided, use the mime type specifically set as part of the `Request` // 2. If a MimeHandler is registered for the content type, use it // 3. If provided, use the "parent type" of the mime type from the response // 4. Default to the content-type provided in the response $parse_with = $this->request->expected_type; if (empty($this->request->expected_type)) { $parse_with = Httpfull::hasParserRegistered($this->content_type) ? $this->content_type : $this->parent_type; } return Httpfull::get($parse_with)->parse($body); }