<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://code.google.com/p/android-market-license-verification/source/browse/trunk/LICENSE
 */

/**
 * A representation of the data returned by the licensing service
 *
 * @category   AndroidMarket
 * @package    AndroidMarket_Licensing
 */
class AndroidMarket_Licensing_ResponseData
{
    /**
	 * @var array
	 */
    protected $_orders;

    /**
     * @var integer
     */
    protected $_nonce;

    /**
     * @param string $responseData
     */
    public function  __construct($responseData)
    {
        if (!is_string($responseData)) {
            throw new AndroidMarket_Licensing_InvalidArgumentException("Invalid response data, expected string");
        }
	
        $jsonResponse = json_decode($responseData);
        
        if(empty($jsonResponse->nonce))
        	throw new AndroidMarket_Licensing_InvalidArgumentException("No nonce");
         
        $this->_nonce = $jsonResponse->nonce;
        
        if(empty($jsonResponse->orders))
        	throw new AndroidMarket_Licensing_InvalidArgumentException("No orders");
        
        $_orders = array();

        foreach( $jsonResponse->orders as $order )
        {
            $gpOrder = new GooglePlayOrder( $order );
        	array_push( $_orders, array( 'notificationId' => $gpOrder->getNotificationId(), 
                                         'orderId'        => $gpOrder->getOrderId(),
                                         'packageName'    => $gpOrder->getPackageName(),
                                         'productId'      => $gpOrder->getProductId(),
                                         'purchaseTime'   => $gpOrder->getPurchaseTime(),
                                         'purchaseState'  => $gpOrder->getPurchaseState() ) );
        }

        $this->_orders = $_orders;
    }

    /**
     * Get one-time nonce
     * 
     * @return string
     */
    public function getNonce()
    {
        return $this->_nonce;
    }

    /**
     * Get the array of orders
     *
     * @return string
     */
    public function getOrders()
    {
        return $this->_orders;
    }

    public function getPackageName()
    {
        return $this->_orders[0]['packageName'];
    }
}
?>