예제 #1
0
파일: Rate.php 프로젝트: ysilvela/php-sdk
 /**
  * Turn the currency exchange rate collection into a JSON structure.
  *
  * Format:
  *
  * {
  *   "rates": {
  *     "EUR": {
  *       "rate": "0.706700000000",
  *       "price_currency_code": "EUR"
  *     }
  *   },
  *   "valid_until": "2015-02-27T12:00:00Z"
  * }
  *
  * @param NostoCurrencyExchangeRateCollection $collection the rate collection.
  * @return string the JSON structure.
  * @throws NostoException of the rate collection is empty.
  */
 protected function getCollectionAsJson(NostoCurrencyExchangeRateCollection $collection)
 {
     $data = array('rates' => array(), 'valid_until' => null);
     $validUntil = $collection->getValidUntil();
     if (!is_null($validUntil)) {
         /** @var NostoFormatterDate $formatter */
         $formatter = Nosto::formatter('date');
         $data['valid_until'] = $formatter->format($validUntil, new NostoDateFormat(NostoDateFormat::ISO_8601));
     }
     /** @var NostoCurrencyExchangeRate $item */
     foreach ($collection->getArrayCopy() as $item) {
         $data['rates'][$item->getCurrency()->getCode()] = array('rate' => $item->getExchangeRate(), 'price_currency_code' => $item->getCurrency()->getCode());
     }
     if (empty($data['rates'])) {
         throw new NostoException(sprintf('Failed to update currency exchange rates for account %s. No rates found in collection.', $this->account->getName()));
     }
     return json_encode($data);
 }
예제 #2
0
파일: Array.php 프로젝트: ysilvela/php-sdk
 /**
  * Serializes the order into an array structure.
  *
  * @param NostoOrderInterface $order the order to serialize.
  * @return array the serialized data.
  */
 public function serialize(NostoOrderInterface $order)
 {
     /** @var NostoFormatterDate $dateFormatter */
     $dateFormatter = Nosto::formatter('date');
     /** @var NostoFormatterPrice $priceFormatter */
     $priceFormatter = Nosto::formatter('price');
     $data = array('order_number' => $order->getOrderNumber(), 'buyer' => array(), 'purchased_items' => array());
     if ($order->getCreatedDate() instanceof NostoDate) {
         $data['created_at'] = $dateFormatter->format($order->getCreatedDate());
     } else {
         $data['created_at'] = '';
     }
     if ($order->getStatus() instanceof NostoOrderStatusInterface) {
         $data['order_status_code'] = $order->getStatus()->getCode();
         $data['order_status_label'] = $order->getStatus()->getLabel();
     } elseif (is_string($order->getStatus()) || is_numeric($order->getStatus())) {
         $data['order_status_code'] = $order->getStatus();
         $data['order_status_label'] = $order->getStatus();
     }
     if ($order->getPaymentProvider() instanceof NostoOrderPaymentProviderInterface) {
         $data['payment_provider'] = $order->getPaymentProvider()->getProvider();
     } elseif (is_string($order->getPaymentProvider()) || is_numeric($order->getPaymentProvider())) {
         $data['payment_provider'] = $order->getPaymentProvider();
     }
     foreach ($order->getItems() as $item) {
         $itemData = array('product_id' => $item->getItemId(), 'quantity' => (int) $item->getQuantity(), 'name' => $item->getName());
         if ($item->getUnitPrice() instanceof NostoPrice) {
             $itemData['unit_price'] = $priceFormatter->format($item->getUnitPrice());
         } elseif (is_numeric($item->getUnitPrice())) {
             $itemData['unit_price'] = $item->getUnitPrice();
         } else {
             $itemData['unit_price'] = '';
         }
         if ($item->getCurrency() instanceof NostoCurrencyCode) {
             $itemData['price_currency_code'] = $item->getCurrency()->getCode();
         } elseif (is_string($item->getCurrency())) {
             $itemData['price_currency_code'] = $item->getCurrency();
         } else {
             $itemData['price_currency_code'] = '';
         }
         $data['purchased_items'][] = $itemData;
     }
     // Add optional order reference if set.
     if ($order->getExternalRef()) {
         $data['external_order_ref'] = $order->getExternalRef();
     }
     // Add optional buyer info.
     if ($order->getBuyer() instanceof NostoOrderBuyerInterface) {
         $data['buyer']['first_name'] = $order->getBuyer()->getFirstName();
         $data['buyer']['last_name'] = $order->getBuyer()->getLastName();
         $data['buyer']['email'] = $order->getBuyer()->getEmail();
     }
     // Add optional order status history if set.
     if ($order->getHistoryStatuses() !== array()) {
         $dateFormat = new NostoDateFormat(NostoDateFormat::ISO_8601);
         $statuses = array();
         foreach ($order->getHistoryStatuses() as $status) {
             if ($status instanceof NostoOrderStatusInterface && $status->getCreatedAt()) {
                 if (!isset($statuses[$status->getCode()])) {
                     $statuses[$status->getCode()] = array();
                 }
                 $statuses[$status->getCode()][] = $dateFormatter->format($status->getCreatedAt(), $dateFormat);
             }
         }
         if (count($statuses) > 0) {
             $data['order_statuses'] = $statuses;
         }
     }
     return $data;
 }
예제 #3
0
파일: Array.php 프로젝트: ysilvela/php-sdk
 /**
  * Serializes the product into an array structure.
  *
  * Example:
  *
  * array(
  *     'url' => 'http://www.example.com/product/CANOE123',
  *     'product_id' => 'CANOE123',
  *     'name' => 'ACME Foldable Canoe',
  *     'image_url' => 'http://www.example.com/product/images/CANOE123.jpg',
  *     'price' => '1269.00',
  *     'price_currency_code' => 'EUR',
  *     'availability' => 'InStock',
  *     'categories' => array('/Outdoor/Boats/Canoes', '/Sales/Boats'),
  *     'description' => 'This foldable canoe is easy to travel with.',
  *     'list_price' => '1299.00',
  *     'brand' => 'ACME',
  *     'tag1' => array('Men'),
  *     'tag2' => array('Foldable'),
  *     'tag3' => array('Brown', 'Black', 'Orange'),
  *     'date_published' => '2011-12-31',
  *     'variation_id' => 'EUR'
  * )
  *
  * @param NostoProductInterface $product the product to serialize.
  * @return array the serialized product array.
  */
 public function serialize(NostoProductInterface $product)
 {
     /** @var NostoFormatterDate $dateFormatter */
     $dateFormatter = Nosto::formatter('date');
     /** @var NostoFormatterPrice $priceFormatter */
     $priceFormatter = Nosto::formatter('price');
     $data = array('url' => $product->getUrl(), 'product_id' => $product->getProductId(), 'name' => $product->getName(), 'image_url' => $product->getImageUrl(), 'categories' => array());
     if ($product->getAvailability() instanceof NostoProductAvailability) {
         $data['availability'] = $product->getAvailability()->getAvailability();
     } elseif (is_string($product->getAvailability())) {
         $data['availability'] = $product->getAvailability();
     } else {
         $data['availability'] = '';
     }
     if ($product->getPrice() instanceof NostoPrice) {
         $data['price'] = $priceFormatter->format($product->getPrice());
     } elseif (is_numeric($product->getPrice())) {
         $data['price'] = $product->getPrice();
     } else {
         $data['price'] = '';
     }
     if ($product->getCurrency() instanceof NostoCurrencyCode) {
         $data['price_currency_code'] = $product->getCurrency()->getCode();
     } elseif (is_string($product->getCurrency())) {
         $data['price_currency_code'] = $product->getCurrency();
     } else {
         $data['price_currency_code'] = '';
     }
     foreach ($product->getCategories() as $category) {
         if ($category instanceof NostoCategoryInterface) {
             $data['categories'][] = $category->getPath();
         } elseif (is_string($category) || is_numeric($category)) {
             $data['categories'][] = $category;
         }
     }
     // Optional properties.
     if ($product->getThumbUrl()) {
         $data['thumb_url'] = $product->getThumbUrl();
     }
     if ($product->getDescription()) {
         $data['description'] = $product->getDescription();
     }
     if ($product->getListPrice() instanceof NostoPrice) {
         $data['list_price'] = $priceFormatter->format($product->getListPrice());
     } elseif (is_numeric($product->getListPrice())) {
         $data['list_price'] = $product->getListPrice();
     } else {
         $data['list_price'] = '';
     }
     if ($product->getBrand()) {
         $data['brand'] = $product->getBrand();
     }
     foreach ($product->getTags() as $type => $tags) {
         if (is_array($tags) && count($tags) > 0) {
             $data[$type] = $tags;
         }
     }
     if ($product->getDatePublished() instanceof NostoDate) {
         $data['date_published'] = $dateFormatter->format($product->getDatePublished());
     }
     if ($product->getVariationId()) {
         $data['variation_id'] = $product->getVariationId();
     }
     if (count($product->getVariations()) > 0) {
         $data['variations'] = array();
         foreach ($product->getVariations() as $variation) {
             $variationData = array();
             if ($variation->getCurrency()) {
                 $variationData['price_currency_code'] = $variation->getCurrency()->getCode();
             }
             if ($variation->getPrice() instanceof NostoPrice) {
                 $variationData['price'] = $priceFormatter->format($variation->getPrice());
             }
             if ($variation->getListPrice() instanceof NostoPrice) {
                 $variationData['list_price'] = $priceFormatter->format($variation->getListPrice());
             }
             if ($variation->getAvailability() instanceof NostoProductAvailability) {
                 $variationData['availability'] = $variation->getAvailability()->getAvailability();
             } elseif (is_string($variation->getAvailability())) {
                 $variationData['availability'] = $variation->getAvailability();
             }
             if ($variation->getId()) {
                 $variationData['variation_id'] = $variation->getId();
                 $data['variations'][$variation->getId()] = $variationData;
             } else {
                 $data['variations'][] = $variationData;
             }
         }
     }
     return $data;
 }