Skip to content

Commit

Permalink
Fixed #7 - Multiple attribute's values
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalybaev committed Feb 2, 2019
1 parent ff968a9 commit c006b82
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 15 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: php

php:
- 7.0
- 7.1
- 7.2

before_script:
- composer self-update
- composer install --no-interaction --prefer-source
- wget -O phpunit.phar https://phar.phpunit.de/phpunit-6.5.6.phar

script:
- php phpunit.phar --configuration ./build/travis-ci.phpunit.xml

notifications:
email: false
13 changes: 13 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="phpbu">
<directory>tests/feed</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ $feedXml = $feed->build();

```

## Working with attributes
`Product` class provides several methods for managing various attributes. But it doesn't cover all Google Merchant attributes. In case of nonexistent methods you should use one of the following:

1. `$product->setAttribute($attributeName, $attributeValue, $isCData = false)` - sets attribute. Replaces old attribute if it existed.
2. `$product->addAttribute($attributeName, $attributeValue, $isCData = false)` - adds one more attribute's value. For example feed can have multiple `additional_image_link` attributes.

## TO-DO
* Cover all Google Merchant feed properties
* Write tests
Expand Down
65 changes: 51 additions & 14 deletions src/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Product
private $attributes = [];

/**
* Sets attribute.
* Sets attribute. If attribute is already exist, it would be overwritten.
*
* @param string $name
* @param string $value
Expand All @@ -28,14 +28,35 @@ class Product
public function setAttribute($name, $value, $isCData = false)
{
$productProperty = new ProductProperty($name, $value, $isCData);
foreach ($this->attributes as $index => $attribute) {
if (mb_strtolower($name) == mb_strtolower($attribute->getName())) {
$this->attributes[$index] = $productProperty;
return $this;
}
$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[] = $productProperty;
$this->attributes[$attributeName][] = $productProperty;
return $this;
}

Expand Down Expand Up @@ -130,6 +151,19 @@ public function setAdditionalImage($imageUrl)
return $this;
}

/**
* Sets additional image of the product.
*
* @param string $imageUrl
*
* @return $this
*/
public function addAdditionalImage($imageUrl)
{
$this->addAttribute('additional_image_link', $imageUrl, true);
return $this;
}

/**
* Sets availability of the product.
*
Expand Down Expand Up @@ -187,7 +221,7 @@ public function setGoogleCategory($category)
$this->setAttribute('google_product_category', $category, false);
return $this;
}

/**
* Sets Google product_type of the product.
*
Expand Down Expand Up @@ -340,12 +374,15 @@ public function getXmlStructure($namespace)
'item' => array(),
);

foreach ($this->attributes as $attribute) {
$value = $attribute->isCData() ? new Cdata($attribute->getValue()) : $attribute->getValue();
$xmlStructure['item'][] = [
'name' => $namespace . $attribute->getName(),
'value' => $value,
];
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;
Expand Down
2 changes: 1 addition & 1 deletion src/ProductProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ProductProperty
*/
public function __construct($name, $value, $isCData)
{
$this->name = $name;
$this->name = strtolower($name);
$this->value = $value;
$this->isCData = $isCData;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require __DIR__ . '/../vendor/autoload.php';
73 changes: 73 additions & 0 deletions tests/feed/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

class ProductTest extends \PHPUnit\Framework\TestCase
{
const PRODUCT_NAMESPACE = '{http://base.google.com/ns/1.0}';

/**
* Tests setting attribute
*/
public function testSetAttribute()
{
$product = new \Vitalybaev\GoogleMerchant\Product();

// Set some value
$product->setAttribute('attr', 'value', false);
$this->assertEquals([
'item' => [
['name' => "{http://base.google.com/ns/1.0}attr", "value" => "value"],
],
], $product->getXmlStructure(static::PRODUCT_NAMESPACE));

// Set another value to this attribute
$product->setAttribute('attr', 'value_new', false);
$this->assertEquals([
'item' => [
['name' => "{http://base.google.com/ns/1.0}attr", "value" => "value_new"],
],
], $product->getXmlStructure(static::PRODUCT_NAMESPACE));

// Check for case insensitive
$product->setAttribute('Attr', 'value_2', false);
$this->assertEquals([
'item' => [
['name' => "{http://base.google.com/ns/1.0}attr", "value" => "value_2"],
],
], $product->getXmlStructure(static::PRODUCT_NAMESPACE));
}

public function testAddingAttribute()
{
$product = new \Vitalybaev\GoogleMerchant\Product();

$product->addAttribute('additional_image_link', 'https://example.com/image1.jpg');
$this->assertEquals([
'item' => [
['name' => "{http://base.google.com/ns/1.0}additional_image_link", "value" => "https://example.com/image1.jpg"],
],
], $product->getXmlStructure(static::PRODUCT_NAMESPACE));

$product->addAttribute('additional_image_link', 'https://example.com/image2.jpg');
$this->assertEquals([
'item' => [
['name' => "{http://base.google.com/ns/1.0}additional_image_link", "value" => "https://example.com/image1.jpg"],
['name' => "{http://base.google.com/ns/1.0}additional_image_link", "value" => "https://example.com/image2.jpg"],
],
], $product->getXmlStructure(static::PRODUCT_NAMESPACE));
}

/**
* Tests setting Id to product.
*/
public function testProductSetsId()
{
$product = new \Vitalybaev\GoogleMerchant\Product();
$product->setId('some_id');

$this->assertEquals([
'item' => [
['name' => "{http://base.google.com/ns/1.0}id", "value" => "some_id"],
],
], $product->getXmlStructure(static::PRODUCT_NAMESPACE));
}
}

0 comments on commit c006b82

Please sign in to comment.