From f7bd63d4196733ab24fd37e75577acab991280d0 Mon Sep 17 00:00:00 2001 From: Chris Ortner Date: Sun, 28 Jan 2018 15:47:49 +0100 Subject: [PATCH] Allow setting simple values more efficiently Fixes #39. Simples values, i.e. values other than properties, attributes, images and usergroups, can now be set using a `Item::add($value, $usergroup = '')` method. The corresponding `Item::set(...)` methods still exist, so compatibility is retained. However, this change should increment the minor version number because functionality is added. --- README.md | 14 +-- src/FINDOLOGIC/Export/Data/DateAdded.php | 5 +- src/FINDOLOGIC/Export/Data/Item.php | 95 +++++++++++++++++++ .../Export/Tests/XmlSerializationTest.php | 78 +++++++++++++++ 4 files changed, 184 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0fdac77..48da330 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. \ No newline at end of file +Running `php-cs-fixer` before commiting will reduce style-caused build failures. diff --git a/src/FINDOLOGIC/Export/Data/DateAdded.php b/src/FINDOLOGIC/Export/Data/DateAdded.php index 96a2842..b3c57cf 100644 --- a/src/FINDOLOGIC/Export/Data/DateAdded.php +++ b/src/FINDOLOGIC/Export/Data/DateAdded.php @@ -2,6 +2,7 @@ namespace FINDOLOGIC\Export\Data; +use DateTime; use FINDOLOGIC\Export\Helpers\UsergroupAwareSimpleValue; class DateAdded extends UsergroupAwareSimpleValue @@ -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); } diff --git a/src/FINDOLOGIC/Export/Data/Item.php b/src/FINDOLOGIC/Export/Data/Item.php index 58508fc..6c62bba 100644 --- a/src/FINDOLOGIC/Export/Data/Item.php +++ b/src/FINDOLOGIC/Export/Data/Item.php @@ -2,6 +2,7 @@ namespace FINDOLOGIC\Export\Data; +use DateTime; use FINDOLOGIC\Export\Helpers\Serializable; abstract class Item implements Serializable @@ -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) { diff --git a/tests/FINDOLOGIC/Export/Tests/XmlSerializationTest.php b/tests/FINDOLOGIC/Export/Tests/XmlSerializationTest.php index f3125f3..d999c26 100644 --- a/tests/FINDOLOGIC/Export/Tests/XmlSerializationTest.php +++ b/tests/FINDOLOGIC/Export/Tests/XmlSerializationTest.php @@ -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; @@ -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()); + } }