Guest User

OrderRequestBuilder

a guest
Sep 4th, 2010
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. class OrderRequestBuilder {
  2.     private $siteId,$shippingMethodId;
  3.     private $items = array();
  4.    
  5.     function __construct($orderOrSiteId) {
  6.         if ( $orderOrSiteId instanceof Order ) {
  7.             $this->siteId = $orderOrSiteId->getSite()->getId();
  8.             $this->shippingMethodId =
  9.                 $orderOrSiteId->getShippingMethod()->getId();
  10.             foreach ( $orderOrSiteId->getLines() as $line ) {
  11.                 $this->items[$line->getSaleableItem()->getId()] =
  12.                     $line->getQuantity();
  13.             }
  14.         } else {
  15.             $this->siteId = $orderOrSiteId;
  16.         }
  17.     }
  18.    
  19.     function setItem($saleableItemId, $quantity) {
  20.         $this->items[$saleableItemId] = $quantity;
  21.     }
  22.    
  23.     function setShippingMethod($shippingMethodId) {
  24.         $this->shippingMethodId = $shippingMethodId;
  25.     }
  26.  
  27.     function buildParams() {
  28.         $items = array();
  29.         foreach ( $this->items as $id => $qty ) {
  30.             $items[] = sprintf('%d:%d', $id, $qty);
  31.         }
  32.         if ( ! $items ) {
  33.             throw new OrderRequestBuilderException('No items');
  34.         }
  35.         if ( ! $this->shippingMethodId ) {
  36.             throw new OrderRequestBuilderException('Shipping method undefined');
  37.         }
  38.         return array(
  39.             'site' => $this->siteId,
  40.             'method' => $this->shippingMethodId,
  41.             'items' => implode(',', $items),
  42.         );
  43.     }
  44.    
  45. }
Advertisement
Add Comment
Please, Sign In to add comment