Skip to content

Commit

Permalink
Added availability to provide shipping info, fixed usage for PHP 5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalybaev committed Jul 27, 2019
1 parent eeb1355 commit 80c8a51
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 75 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: php

php:
- "5.6"
- "7.0"
- "7.1"
- "7.2"
Expand Down
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Run the Composer require command from the Terminal:
```php
use Vitalybaev\GoogleMerchant\Feed;
use Vitalybaev\GoogleMerchant\Product;
use Vitalybaev\GoogleMerchant\Product\Shipping;
use Vitalybaev\GoogleMerchant\Product\Availability\Availability;

// Create feed object
Expand Down Expand Up @@ -46,6 +47,16 @@ foreach ($products as $product) {
$item->setColor($product->color);
$item->setSize($product->size);

// Shipping info
$shipping = new Shipping();
$shipping->setCountry('US');
$shipping->setRegion('CA, NSW, 03');
$shipping->setPostalCode('94043');
$shipping->setLocationId('21137');
$shipping->setService('UPS Express');
$shipping->setPrice('1300 USD');
$item->setShipping($shipping);

// Add this product to the feed
$feed->addProduct($item);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Vitalybaev\GoogleMerchant\Exception;

class InvalidArgumentException extends \Exception
use Exception;

class InvalidArgumentException extends Exception
{

}
107 changes: 107 additions & 0 deletions src/HasProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Vitalybaev\GoogleMerchant;

use Sabre\Xml\Element\Cdata;

trait HasProperties
{
/**
* Attributes
*
* @var ProductProperty[]
*/
private $attributes = [];

/**
* Sets attribute. If attribute is already exist, it would be overwritten.
*
* @param string $name
* @param string $value
* @param bool $isCData
*
* @return self
*/
public function setAttribute($name, $value, $isCData = false)
{
$productProperty = new ProductProperty($name, $value, $isCData);
$this->attributes[strtolower($name)] = $productProperty;

return $this;
}

/**
* Adds attribute. Doesn't overwrite previous attributes.
*
* @param $name
* @param $value
* @param bool $isCData
*
* @return self
*/
public function addAttribute($name, $value, $isCData = false)
{
$productProperty = new ProductProperty($name, $value, $isCData);
$attributeName = strtolower($name);
if (!isset($this->attributes[$attributeName])) {
$this->attributes[$attributeName] = [$productProperty];
return $this;
}

if (!is_array($this->attributes[$attributeName])) {
$this->attributes[$attributeName] = [$this->attributes[$attributeName], $productProperty];
return $this;
}

$this->attributes[$attributeName][] = $productProperty;
return $this;
}

/**
* Returns structure of properties
*
* @param $namespace
*
* @return array
*/
public function getPropertiesXmlStructure($namespace)
{
$result = [];

foreach ($this->attributes as $attributeItem) {
if (is_object($attributeItem) && $attributeItem->getValue() instanceof PropertyBag) {
$result[] = [
'name' => $namespace . $attributeItem->getName(),
'value' => $attributeItem->getValue()->getPropertiesXmlStructure($namespace),
];

continue;
}

$attributes = is_array($attributeItem) ? $attributeItem : [$attributeItem];
/** @var ProductProperty $attribute */
foreach ($attributes as $attribute) {
$result[] = $attribute->getXmlStructure($namespace);
}
}

return $result;
}

/**
* @return PropertyBag
*/
public function getPropertyBag()
{
$propertyBag = new PropertyBag();
foreach ($this->attributes as $attributeItem) {
$attributes = is_array($attributeItem) ? $attributeItem : [$attributeItem];
foreach ($attributes as $attribute) {
$value = $attribute->isCData() ? new Cdata($attribute->getValue()) : $attribute->getValue();
$propertyBag->addAttribute($attribute->getName(), $value, $attribute->isCData());
}
}

return $propertyBag;
}
}
79 changes: 15 additions & 64 deletions src/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,13 @@

namespace Vitalybaev\GoogleMerchant;

use Sabre\Xml\Element\Cdata;
use Vitalybaev\GoogleMerchant\Exception\InvalidArgumentException;
use Vitalybaev\GoogleMerchant\Product\Availability\Availability;
use Vitalybaev\GoogleMerchant\Product\Condition;
use Vitalybaev\GoogleMerchant\Product\Shipping;

class Product
{
/**
* Product's attributes
*
* @var ProductProperty[]
*/
private $attributes = [];

/**
* Sets attribute. If attribute is already exist, it would be overwritten.
*
* @param string $name
* @param string $value
* @param bool $isCData
*
* @return $this
*/
public function setAttribute($name, $value, $isCData = false)
{
$productProperty = new ProductProperty($name, $value, $isCData);
$this->attributes[strtolower($name)] = $productProperty;

return $this;
}

/**
* Adds attribute. Doesn't overwrite previous attributes.
*
* @param $name
* @param $value
* @param bool $isCData
*
* @return Product
*/
public function addAttribute($name, $value, $isCData = false)
{
$productProperty = new ProductProperty($name, $value, $isCData);
$attributeName = strtolower($name);
if (!isset($this->attributes[$attributeName])) {
$this->attributes[$attributeName] = [$productProperty];
return $this;
}

if (!is_array($this->attributes[$attributeName])) {
$this->attributes[$attributeName] = [$this->attributes[$attributeName], $productProperty];
return $this;
}

$this->attributes[$attributeName][] = $productProperty;
return $this;
}
use HasProperties;

/**
* Sets id of product.
Expand Down Expand Up @@ -363,6 +313,18 @@ public function setSize($size)
return $this;
}

/**
* @param Shipping $shipping
*
* @return $this
*/
public function setShipping($shipping)
{
$propertyBag = $shipping->getPropertyBag()->setName('shipping');
$this->setAttribute('shipping', $propertyBag);
return $this;
}

/**
* @param $namespace
*
Expand All @@ -371,20 +333,9 @@ public function setSize($size)
public function getXmlStructure($namespace)
{
$xmlStructure = array(
'item' => array(),
'item' => $this->getPropertiesXmlStructure($namespace),
);

foreach ($this->attributes as $attributeItem) {
$attributes = is_array($attributeItem) ? $attributeItem : [$attributeItem];
foreach ($attributes as $attribute) {
$value = $attribute->isCData() ? new Cdata($attribute->getValue()) : $attribute->getValue();
$xmlStructure['item'][] = [
'name' => $namespace . $attribute->getName(),
'value' => $value,
];
}
}

return $xmlStructure;
}
}
2 changes: 1 addition & 1 deletion src/Product/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Condition
{
const NEW = 'new';
const NEW_PRODUCT = 'new';

const REFURBISHED = 'refurbished';

Expand Down
106 changes: 106 additions & 0 deletions src/Product/Shipping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Vitalybaev\GoogleMerchant\Product;

use Vitalybaev\GoogleMerchant\HasProperties;

/**
* Describes product's specific shipping rules
*
* @see https://support.google.com/merchants/answer/6324484
*/
class Shipping
{
use HasProperties;

/**
* Sets shipping country.
*
* @param string $countryCode ISO 3166-1 country code
*
* @return $this
*/
public function setCountry($countryCode)
{
$this->setAttribute('country', $countryCode, false);
return $this;
}

/**
* Sets shipping country.
*
* @param string $region
*
* @return $this
*/
public function setRegion($region)
{
$this->setAttribute('region', $region, false);
return $this;
}

/**
* Sets shipping postal code.
*
* @param string $postalCode
*
* @return $this
*/
public function setPostalCode($postalCode)
{
$this->setAttribute('postal_code', $postalCode, false);
return $this;
}

/**
* Sets shipping location id.
*
* @param string $locationId
*
* @return $this
*/
public function setLocationId($locationId)
{
$this->setAttribute('location_id', $locationId, false);
return $this;
}

/**
* Sets shipping location group name.
*
* @param string $locationGroupName
*
* @return $this
*/
public function setLocationGroupName($locationGroupName)
{
$this->setAttribute('location_group_name', $locationGroupName, false);
return $this;
}

/**
* Sets shipping service.
*
* @param string $service
*
* @return $this
*/
public function setService($service)
{
$this->setAttribute('service', $service, false);
return $this;
}

/**
* Sets shipping price.
*
* @param string $price
*
* @return $this
*/
public function setPrice($price)
{
$this->setAttribute('price', $price, false);
return $this;
}
}
Loading

0 comments on commit 80c8a51

Please sign in to comment.