Example #1
0
 /**
  * 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;
 }
 /**
  * Converts the product object into an array and returns it.
  *
  * 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'
  * )
  *
  * @param NostoProductInterface $product the object.
  * @return array the newly created array.
  */
 protected function getProductAsArray(NostoProductInterface $product)
 {
     $data = array('url' => $product->getUrl(), 'product_id' => $product->getProductId(), 'name' => $product->getName(), 'image_url' => $product->getImageUrl(), 'price' => Nosto::helper('price')->format($product->getPrice()), 'price_currency_code' => strtoupper($product->getCurrencyCode()), 'availability' => $product->getAvailability(), 'categories' => $product->getCategories());
     // Optional properties.
     if ($product->getFullDescription()) {
         $data['description'] = $product->getFullDescription();
     }
     if ($product->getListPrice()) {
         $data['list_price'] = Nosto::helper('price')->format($product->getListPrice());
     }
     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()) {
         $data['date_published'] = Nosto::helper('date')->format($product->getDatePublished());
     }
     return $data;
 }
 /**
  * Sends a product re-crawl request to nosto.
  *
  * @param NostoProductInterface $product the product to re-crawl.
  * @param NostoAccountInterface $account the account to re-crawl the product for.
  * @return bool true on success, false otherwise.
  * @throws NostoException if the request fails or cannot be made.
  */
 public static function send(NostoProductInterface $product, NostoAccountInterface $account)
 {
     return self::sendRequest($account, array('products' => array(array('product_id' => $product->getProductId(), 'url' => $product->getUrl()))));
 }