<commit_before><?php

/**
 * @category CheddarGetter
 * @package CheddarGetter
 * @author <NAME> <<EMAIL>>
 */
/**
 * Response object
 * @category CheddarGetter
 * @package CheddarGetter
 * @author <NAME> <<EMAIL>>
 */
 
class CheddarGetter_Response extends DOMDocument {
	
	private $_responseType;
	private $_array;
	
	/**
	 * Constructor
	 *
	 * @param $response string well formed xml
	 * @throws CheddarGetter_Response_Exception in the event the xml is not well formed
	 */
	public function __construct($response) {
		$this->_array = null;
		parent::__construct('1.0', 'UTF-8');
		
		if (!@$this->loadXML($response)) {
			throw new CheddarGetter_Response_Exception("Response failed to load into the DOM.\n\n$response", CheddarGetter_Response_Exception::UNKNOWN);
		}
		
		$this->_responseType = $this->documentElement->nodeName;
		
		$this->handleError();
	}

	public function __sleep() {
		$this->_xml = $this->saveXML();
		return array('_xml', '_responseType');
	}

	public function __wakeup() {
		$this->loadXML( $this->_xml );
	}
	
	/**
	 * Get the response type for this request object -- usually corresponds to the root node name
	 *
	 * @return string
	 */
	public function getResponseType() {
		return $this->_responseType;
	}
	
	/**
	 * Determine whether or not the response doc contains embedded errors
	 *
	 * @return boolean
	 */
	public function hasEmbeddedErrors() {
		$arr = $this->toArray();
		return isset($arr['errors']);
	}
	
	/**
	 * Get embedded errors if any
	 *
	 * @return array|false
	 */
	public function getEmbeddedErrors() {
		if ($this->hasEmbeddedErrors()) {
			$arr = $this->toArray();
			return $arr['errors'];
		}
		return false;
	}
	
	/**
	 * Get a nested array representation of the response doc
	 *
	 * @return array
	 */
	public function toArray() {
		if ($this->_array) {
			return $this->_array;
		}
		$root = $this->documentElement;
		if ($root == 'error') {
			return array(
				0 => array(
					'id' => $root->getAttribute('id'),
					'code' => $root->getAttribute('code'),
					'auxCode' => $root->getAttribute('auxCode'),
					'message' => $root->firstChild->nodeValue
				)
			);
		}
		$this->_array = $this->_toArray($root->childNodes);
		return $this->_array;
		
	}
	
	/**
	 * Get a JSON encoded string representation of the response doc
	 *
	 * @return string
	 */
	public function toJson() {
		return json_encode($this->toArray());
	}
	
	/**
	 * Recursive method to traverse the dom and produce an array
	 *
	 * @return array
	 */
	protected function _toArray(DOMNodeList $nodes) {
		$array = array();
		foreach ($nodes as $node) {
			if ($node->nodeType != XML_ELEMENT_NODE) {
				continue;
			}
			if ($node->hasAttributes()) { // deep
				if ($node->tagName == 'error') {
					$array = array(
						'id' => $node->getAttribute('id'),
						'code' => $node->getAttribute('code'),
						'auxCode' => $node->getAttribute('auxCode'),
						'message' => $node->nodeValue
					);
				} else {
					// in the case of custom charges, use the id for the key
					if ($node->hasAttribute('code') && $node->getAttribute('code')) {  
						$key = $node->getAttribute('code');
						
						$tmpArr = array();
						
						if ($node->hasAttribute('id')) {
							$tmpArr = array(
								'id' => $node->getAttribute('id')
							);
						}
						$tmpArr = $tmpArr + array('code'=>$key);
						
						// charges need to be a nested array since there can be multiple charges with the same charge code
						if ($node->tagName == 'charge') {
							$array[$key][] = $tmpArr;
						} else {
							$array[$key] = $tmpArr;
						}
						
						unset($tmpArr);
						
					} else {
						$key = $node->getAttribute('id');
						$array[$key] = array();
					}
					$array[$key] = $array[$key] + $this->_toArray($node->childNodes);
				}
			} else {
				if ($node->tagName == 'errors') {
					$array[$node->tagName][] = $this->_toArray($node->childNodes);
				} else if ($node->childNodes->length > 1) { // sub array
					$array[$node->tagName] = $this->_toArray($node->childNodes);
				} else {
					$array[$node->tagName] = $node->nodeValue;
				}
			}
		}
		return $array;
	}
	
	/**
	 * Get an array representation of a single plan node
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one plan
	 * @return array
	 */
	public function getPlan($code = null) {
		if ($this->getResponseType() != 'plans') {
			throw new CheddarGetter_Response_Exception("Can't get a plan from a response doc that isn't of type 'plans'", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$code && $this->getElementsByTagName('plan')->length > 1) {
			throw new CheddarGetter_Response_Exception("This response contains more than one plan so you need to provide the code for the plan you wish to get", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$code) {
			return current($this->toArray());
		}
		$array = $this->toArray();
		return $array[$code];
	}
	
	/**
	 * Get an array representation of all of the plan items
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one plan
	 * @return array
	 */
	public function getPlanItems($code = null) {
		$plan = $this->getPlan($code);
		return $plan['items'];
	}
	
	/**
	 * Get an array representation of a single plan item node
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one plan
	 * @return array
	 */
	public function getPlanItem($code = null, $itemCode = null) {
		$items = $this->getPlanItems($code);
		if (!$itemCode && count($items) > 1) {
			throw new CheddarGetter_Response_Exception("This plan contains more than one item so you need to provide the code for the item you wish to get", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$itemCode) {
			return current($items);
		}
		return $items[$itemCode];
	}
	
	/**
	 * Get an array representation of a single customer node
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one customer
	 * @return array
	 */
	public function getCustomer($code = null) {
		if ($this->getResponseType() != 'customers') {
			throw new CheddarGetter_Response_Exception("Can't get a customer from a response doc that isn't of type 'customers'", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$code && $this->getElementsByTagName('customer')->length > 1) {
			throw new CheddarGetter_Response_Exception("This response contains more than one customer so you need to provide the code for the customer you wish to get", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$code) {
			return current($this->toArray());
		}
		$array = $this->toArray();
		return $array[$code];
	}
	
	/**
	 * Get an array representation of a single customer's current subscription
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one customer
	 * @return array
	 */
	public function getCustomerSubscription($code = null) {
		$subscriptions = $this->getCustomerSubscriptions($code);
		return current($subscriptions);
	}
	
	/**
	 * Get an array representation of a single customer's current subscription
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one customer
	 * @return array
	 */
	public function getCustomerIsActive($code = null) {
		$subscription = $this->getCustomerSubscription($code);
		if ($subscription['canceledDatetime']) {
			if (strtotime($subscription['canceledDatetime']) <= time()) {
				return false;
			}
		}
		return true;
	}
	
	/**<commit_msg>Conv method to determine if subscription is in paypal-wait state.<commit_after><?php

/**
 * @category CheddarGetter
 * @package CheddarGetter
 * @author <NAME> <<EMAIL>>
 */
/**
 * Response object
 * @category CheddarGetter
 * @package CheddarGetter
 * @author <NAME> <<EMAIL>>
 */
 
class CheddarGetter_Response extends DOMDocument {
	
	private $_responseType;
	private $_array;
	
	/**
	 * Constructor
	 *
	 * @param $response string well formed xml
	 * @throws CheddarGetter_Response_Exception in the event the xml is not well formed
	 */
	public function __construct($response) {
		$this->_array = null;
		parent::__construct('1.0', 'UTF-8');
		
		if (!@$this->loadXML($response)) {
			throw new CheddarGetter_Response_Exception("Response failed to load into the DOM.\n\n$response", CheddarGetter_Response_Exception::UNKNOWN);
		}
		
		$this->_responseType = $this->documentElement->nodeName;
		
		$this->handleError();
	}

	public function __sleep() {
		$this->_xml = $this->saveXML();
		return array('_xml', '_responseType');
	}

	public function __wakeup() {
		$this->loadXML( $this->_xml );
	}
	
	/**
	 * Get the response type for this request object -- usually corresponds to the root node name
	 *
	 * @return string
	 */
	public function getResponseType() {
		return $this->_responseType;
	}
	
	/**
	 * Determine whether or not the response doc contains embedded errors
	 *
	 * @return boolean
	 */
	public function hasEmbeddedErrors() {
		$arr = $this->toArray();
		return isset($arr['errors']);
	}
	
	/**
	 * Get embedded errors if any
	 *
	 * @return array|false
	 */
	public function getEmbeddedErrors() {
		if ($this->hasEmbeddedErrors()) {
			$arr = $this->toArray();
			return $arr['errors'];
		}
		return false;
	}
	
	/**
	 * Get a nested array representation of the response doc
	 *
	 * @return array
	 */
	public function toArray() {
		if ($this->_array) {
			return $this->_array;
		}
		$root = $this->documentElement;
		if ($root == 'error') {
			return array(
				0 => array(
					'id' => $root->getAttribute('id'),
					'code' => $root->getAttribute('code'),
					'auxCode' => $root->getAttribute('auxCode'),
					'message' => $root->firstChild->nodeValue
				)
			);
		}
		$this->_array = $this->_toArray($root->childNodes);
		return $this->_array;
		
	}
	
	/**
	 * Get a JSON encoded string representation of the response doc
	 *
	 * @return string
	 */
	public function toJson() {
		return json_encode($this->toArray());
	}
	
	/**
	 * Recursive method to traverse the dom and produce an array
	 *
	 * @return array
	 */
	protected function _toArray(DOMNodeList $nodes) {
		$array = array();
		foreach ($nodes as $node) {
			if ($node->nodeType != XML_ELEMENT_NODE) {
				continue;
			}
			if ($node->hasAttributes()) { // deep
				if ($node->tagName == 'error') {
					$array = array(
						'id' => $node->getAttribute('id'),
						'code' => $node->getAttribute('code'),
						'auxCode' => $node->getAttribute('auxCode'),
						'message' => $node->nodeValue
					);
				} else {
					// in the case of custom charges, use the id for the key
					if ($node->hasAttribute('code') && $node->getAttribute('code')) {  
						$key = $node->getAttribute('code');
						
						$tmpArr = array();
						
						if ($node->hasAttribute('id')) {
							$tmpArr = array(
								'id' => $node->getAttribute('id')
							);
						}
						$tmpArr = $tmpArr + array('code'=>$key);
						
						// charges need to be a nested array since there can be multiple charges with the same charge code
						if ($node->tagName == 'charge') {
							$array[$key][] = $tmpArr;
						} else {
							$array[$key] = $tmpArr;
						}
						
						unset($tmpArr);
						
					} else {
						$key = $node->getAttribute('id');
						$array[$key] = array();
					}
					$array[$key] = $array[$key] + $this->_toArray($node->childNodes);
				}
			} else {
				if ($node->tagName == 'errors') {
					$array[$node->tagName][] = $this->_toArray($node->childNodes);
				} else if ($node->childNodes->length > 1) { // sub array
					$array[$node->tagName] = $this->_toArray($node->childNodes);
				} else {
					$array[$node->tagName] = $node->nodeValue;
				}
			}
		}
		return $array;
	}
	
	/**
	 * Get an array representation of a single plan node
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one plan
	 * @return array
	 */
	public function getPlan($code = null) {
		if ($this->getResponseType() != 'plans') {
			throw new CheddarGetter_Response_Exception("Can't get a plan from a response doc that isn't of type 'plans'", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$code && $this->getElementsByTagName('plan')->length > 1) {
			throw new CheddarGetter_Response_Exception("This response contains more than one plan so you need to provide the code for the plan you wish to get", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$code) {
			return current($this->toArray());
		}
		$array = $this->toArray();
		return $array[$code];
	}
	
	/**
	 * Get an array representation of all of the plan items
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one plan
	 * @return array
	 */
	public function getPlanItems($code = null) {
		$plan = $this->getPlan($code);
		return $plan['items'];
	}
	
	/**
	 * Get an array representation of a single plan item node
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one plan
	 * @return array
	 */
	public function getPlanItem($code = null, $itemCode = null) {
		$items = $this->getPlanItems($code);
		if (!$itemCode && count($items) > 1) {
			throw new CheddarGetter_Response_Exception("This plan contains more than one item so you need to provide the code for the item you wish to get", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$itemCode) {
			return current($items);
		}
		return $items[$itemCode];
	}
	
	/**
	 * Get an array representation of a single customer node
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one customer
	 * @return array
	 */
	public function getCustomer($code = null) {
		if ($this->getResponseType() != 'customers') {
			throw new CheddarGetter_Response_Exception("Can't get a customer from a response doc that isn't of type 'customers'", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$code && $this->getElementsByTagName('customer')->length > 1) {
			throw new CheddarGetter_Response_Exception("This response contains more than one customer so you need to provide the code for the customer you wish to get", CheddarGetter_Response_Exception::USAGE_INVALID);
		}
		if (!$code) {
			return current($this->toArray());
		}
		$array = $this->toArray();
		return $array[$code];
	}
	
	/**
	 * Get an array representation of a single customer's current subscription
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one customer
	 * @return array
	 */
	public function getCustomerSubscription($code = null) {
		$subscriptions = $this->getCustomerSubscriptions($code);
		return current($subscriptions);
	}
	
	/**
	 * Get an array representation of a single customer's current subscription
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one customer
	 * @return array
	 */
	public function getCustomerIsActive($code = null) {
		$subscription = $this->getCustomerSubscription($code);
		if ($subscription['canceledDatetime']) {
			if (strtotime($subscription['canceledDatetime']) <= time()) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Is this customer's account pending paypal preapproval confirmation?
	 *
	 * @throws CheddarGetter_Response_Exception if the response type is incompatible or if a $code is not provided and the response contains more than one customer
	 * @return array
	 */
	public function getCustomerIsWaitingForPayPal($code = null) {
		$subscription = $this->getCustomerSubscription($code);
		if (
			$subscription['canceledDatetime']
			&& strtotime($subscription['canceledDatetime']) <= time()
			&& $subscription['cancelType'] == 'paypal-wait'
		) {
			return true;
		}
		return false;
	}
	
	/**
{'fraction_non_alphanumeric': 0.13505154639175257, 'fraction_numerical': 0.0009702850212249849, 'mean_word_length': 3.5479867622724766, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 182, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1913151', 'n_tokens_mistral': 5691, 'n_tokens_neox': 4876, 'n_words': 1492}