xml_manipulator = $xml_manipulator; $this->paapi_helper = new Paapi_Helper(); $this->remote_loader = new Remote_Loader(); } /** * Parses the item lookup response and checks if the SIMPLE XML element object is generated successfully and has no error code from PA-API * * @since 1.8.0 * * @param string $response Well-formed XML string * * @throws \Exception if xml object has an error code from PA-API */ public function validate( $xml_response ) { if ( ! $this->should_render_xml( $xml_response ) ) { throw new \Exception( $xml_response->Items->Request->Errors->Error->Code ); } } /** * Whether to allow xml to be rendered * * @since 1.4.7 * * @param \SimpleXMLElement $xml Well-formed XML string * * @return boolean should_render_xml */ private function should_render_xml( $xml ) { return ! isset( $xml->Items->Request->Errors->Error ) || $this->is_error_acceptable( $xml ); } /** * Whether the error is acceptable. For now, It is acceptable for Invalid Parameter value with at least one item set. * This handles the case when an expired ASIN is present in a list of products and thus unblocks the ad rendering. * * @since 1.8.0 * * @param \SimpleXMLElement $xml Well-formed XML string * * @return boolean is_error_acceptable */ private function is_error_acceptable( $xml ) { return $xml->Items->Request->Errors->Error->Code == Paapi_Constants::INVALID_PARAMETER_VALUE_ERROR && isset( $xml->Items->Item ); } /** * Get the item lookup response by creating required parameters and then making a GET request. * * @param String $marketplace marketplace * @param array $asins_array array of asins * @param String $store_id store id of associate * * @return array array of asin => response items */ public function get_response( $marketplace, $asins_array, $store_id ) { $url = $this->paapi_helper->get_item_lookup_url( $asins_array, $marketplace, $store_id ); $response = $this->remote_loader->load( $url ); $xml_response = $this->xml_manipulator->parse( $response ); $this->validate( $xml_response ); $customized_response = $this->xml_manipulator->get_customized_items_object( $this->xml_manipulator->unescape_numeric_character_references( $response ), $marketplace ); $items_array = $this->break_response_into_asin_response_map( $customized_response ); return $items_array; } /** * Break the response into asin response map * * @since 1.8.0 * * @param \SimpleXMLElement $response Xml response * * @return array Asin => response map */ private function break_response_into_asin_response_map( $response ){ $items_array = array(); foreach ( $response->Item as $item ){ $items_array[$item->ASIN->__toString()] = $item->asXML(); } return $items_array; } }