Skip to content

Commit

Permalink
Allow setting simple values more efficiently
Browse files Browse the repository at this point in the history
Fixes #39.

Simples values, i.e. values other than properties, attributes, images
and usergroups, can now be set using a `Item::add<value>($value,
$usergroup = '')` method.

The corresponding `Item::set<value>(...)` methods still exist, so
compatibility is retained. However, this change should increment the
minor version number because functionality is added.
  • Loading branch information
howard committed Jan 28, 2018
1 parent b2dabb2 commit f7bd63d
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 8 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ Using the XML export is recommended by FINDOLOGIC. The XML is easier to read and
```php
require_once './vendor/autoload.php';

use \FINDOLOGIC\Export\XML\XMLExporter;
use \FINDOLOGIC\Export\Exporter;
use \FINDOLOGIC\Export\Data\Price;

$exporter = Exporter::create(Exporter::TYPE_XML);

$item = $exporter->createItem('123');

$price = new Price();
$price->setValue('13.37');
$item->setPrice($price);
$item->addPrice(13.37);
// Alternative long form:
// $price = new Price();
// $price->setValue(13.37);
// $item->setPrice($price);

$exporter->serializeItems([$item], 0, 1);
$exporter->serializeItems([$item], 0, 1, 1);
```

## Contributors
Expand All @@ -55,4 +57,4 @@ If you want to contribute to this project, feel free to fork the repository. Aft

Tests should be provided if possible.

Running `php-cs-fixer` before commiting will reduce style-caused build failures.
Running `php-cs-fixer` before commiting will reduce style-caused build failures.
5 changes: 3 additions & 2 deletions src/FINDOLOGIC/Export/Data/DateAdded.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FINDOLOGIC\Export\Data;

use DateTime;
use FINDOLOGIC\Export\Helpers\UsergroupAwareSimpleValue;

class DateAdded extends UsergroupAwareSimpleValue
Expand All @@ -16,9 +17,9 @@ public function setValue($value, $usergroup = '')
throw new \BadMethodCallException('Assign DateAdded values by passing a \DateTime to setDateValue()');
}

public function setDateValue(\DateTime $value, $usergroup = '')
public function setDateValue(DateTime $value, $usergroup = '')
{
$formatted = $value->format(\DateTime::ATOM);
$formatted = $value->format(DateTime::ATOM);

parent::setValue($formatted, $usergroup);
}
Expand Down
95 changes: 95 additions & 0 deletions src/FINDOLOGIC/Export/Data/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FINDOLOGIC\Export\Data;

use DateTime;
use FINDOLOGIC\Export\Helpers\Serializable;

abstract class Item implements Serializable
Expand Down Expand Up @@ -65,51 +66,145 @@ public function __construct($id)
$this->ordernumbers = new AllOrdernumbers();
}

public function getName()
{
return $this->name;
}

public function setName(Name $name)
{
$this->name = $name;
}

public function addName($name, $usergroup = '')
{
$this->name->setValue($name, $usergroup);
}

public function getSummary()
{
return $this->summary;
}

public function setSummary(Summary $summary)
{
$this->summary = $summary;
}

public function addSummary($summary, $usergroup = '')
{
$this->summary->setValue($summary, $usergroup);
}

public function getDescription()
{
return $this->description;
}

public function setDescription(Description $description)
{
$this->description = $description;
}

public function addDescription($description, $usergroup = '')
{
$this->description->setValue($description, $usergroup);
}

public function getPrice()
{
return $this->price;
}

public function setPrice(Price $price)
{
$this->price = $price;
}

public function addPrice($price, $usergroup = '')
{
if ($this->price === null) {
$this->price = new Price();
}

$this->price->setValue($price, $usergroup);
}

public function getUrl()
{
return $this->url;
}

public function setUrl(Url $url)
{
$this->url = $url;
}

public function addUrl($url, $usergroup = '')
{
$this->url->setValue($url, $usergroup);
}

public function getBonus()
{
return $this->bonus;
}

public function setBonus(Bonus $bonus)
{
$this->bonus = $bonus;
}

public function addBonus($bonus, $usergroup = '')
{
$this->bonus->setValue($bonus, $usergroup);
}

public function getSalesFrequency()
{
return $this->salesFrequency;
}

public function setSalesFrequency(SalesFrequency $salesFrequency)
{
$this->salesFrequency = $salesFrequency;
}

public function addSalesFrequency($salesFrequency, $usergroup = '')
{
$this->salesFrequency->setValue($salesFrequency, $usergroup);
}

public function getDateAdded()
{
return $this->dateAdded;
}

public function setDateAdded(DateAdded $dateAdded)
{
$this->dateAdded = $dateAdded;
}

public function addDateAdded(DateTime $dateAdded, $usergroup = '')
{
$this->dateAdded->setDateValue($dateAdded, $usergroup);
}

public function getSort()
{
return $this->sort;
}

public function setSort(Sort $sort)
{
$this->sort = $sort;
}

public function addSort($sort, $usergroup = '')
{
$this->sort->setValue($sort, $usergroup);
}

public function addProperty(Property $property)
{
foreach ($property->getAllValues() as $usergroup => $value) {
Expand Down
78 changes: 78 additions & 0 deletions tests/FINDOLOGIC/Export/Tests/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FINDOLOGIC\Export\Tests;

use DateTime;
use FINDOLOGIC\Export\Data\Attribute;
use FINDOLOGIC\Export\Data\Image;
use FINDOLOGIC\Export\Data\Keyword;
Expand Down Expand Up @@ -272,4 +273,81 @@ public function testAttemptingToGetCsvFromAnXmlItemResultsInAnException()
$this->assertEquals('XMLItem does not implement CSV export.', $e->getMessage());
}
}

/**
* @return array Name of add method to call in a test to add a certain value, and an array of values with
* usergroup names as key.
*/
public function simpleValueAddingShortcutProvider()
{
$stringValuesWithUsergroupKeys = [
'' => 'No usergroup',
'foo' => 'One usergroup',
'bar' => 'Another usergroup'
];

$integerValuesWithUsergroupKeys = [
'' => 0,
'foo' => 3,
'bar' => 10
];

return [
'name' => ['Name', $stringValuesWithUsergroupKeys],
'summary' => ['Summary', $stringValuesWithUsergroupKeys],
'description' => ['Description', $stringValuesWithUsergroupKeys],
'price' => ['Price', [
'' => 13.37,
'foo' => 42,
'bar' => 12.00
]],
'url' => ['Url', [
'' => 'https://example.org/product.html',
'foo' => 'https://example.org/product.html?group=foo',
'bar' => 'https://example.org/product.html?group=bar'
]],
'bonus' => ['Bonus', $integerValuesWithUsergroupKeys],
'sales frequency' => ['SalesFrequency', $integerValuesWithUsergroupKeys],
'sort' => ['Sort', $integerValuesWithUsergroupKeys],
];
}

/**
* @dataProvider simpleValueAddingShortcutProvider
*
* @param string $valueType
* @param array $values
*/
public function testSimpleValuesAddedToItemViaShortcutAccumulate($valueType, array $values)
{
$item = new XMLItem(123);

foreach ($values as $usergroup => $value) {
$item->{'add' . $valueType}($value, $usergroup);
}

$this->assertEquals($values, $item->{'get' . $valueType}()->getValues());
}

public function testDateValuesAddedToItemViaShortcutAccumulate()
{
$values = [
'' => new DateTime('today midnight'),
'foo' => new DateTime('yesterday midnight'),
'bar' => new DateTime('tomorrow midnight')
];

// On assignment, dates are converted to strings according to the format set in the schema.
$expectedValues = array_map(function (DateTime $date) {
return $date->format(DATE_ATOM);
}, $values);

$item = new XMLItem(123);

foreach ($values as $usergroup => $value) {
$item->addDateAdded($value, $usergroup);
}

$this->assertEquals($expectedValues, $item->getDateAdded()->getValues());
}
}

0 comments on commit f7bd63d

Please sign in to comment.