/** * Creates a new response object that represents the value returned by a * previous successful Text2Pay API call. * * @param string $raw The raw value from the API call response. * * @abstract * * @see Text2PayApi_ResponseBase * * @author Braga, Bruno <*****@*****.**> * * @since 0.2 * * @throws Text2PayException for unexpected errors. * * @throws Text2PayApiException for all API returned errors. * */ public function __construct($raw) { $this->raw = $raw; // process response try { // only try to parse if applicable if (self::isRawResponseXml($raw)) { $this->response = Text2PayApi_Common::xml2Array(simplexml_load_string($raw, null, LIBXML_NOCDATA)); if (array_key_exists('error_code', $this->response)) { if ($this->response['error_code'] != Text2PayApiException::API_SUCCESS) { throw Text2PayApiException::initWithResult($this->response); } } } else { if (self::isPlainRawResponseError($raw)) { throw Text2PayApiException::initWithPlainResult($raw); } } } catch (Text2PayApiException $taex) { throw $taex; } catch (Exception $ex) { throw new Text2PayException('Unable to properly parse response from API.', Text2PayException::LIB_ERROR_UNKNOWN, $ex); } }
/** * Recursively extracts properties from a {@link SimpleXMLElement} object * to an array format, for simplicity of usage. * * Note: depending on the XML formatting, the array might be dirty formed, * with attributes and such placed as keys in the array list. This * must be treated manually afterwards. * * @param SimpleXMLElement $xmlObject the XML object to be converted * * @return array object representation of the * {@link SimpleXMLElement} object. * * @author Braga, Bruno <*****@*****.**> * @author Martens, Scott <*****@*****.**> * * @example Text2PayApi_Common::xml2Array(@new SimpleXMLElement("<xml...")); * * @since 0.1 * */ public static function xml2Array($xmlObject) { $out = array(); foreach ((array) $xmlObject as $index => $node) { if (is_object($node) || is_array($node)) { $out[$index] = Text2PayApi_Common::xml2Array($node); } else { $out[$index] = $node; } } return $out; }