diff --git a/README.md b/README.md index 5b5f6e3..2b5d0c3 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,11 @@ $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); $xmlOutput = $exporter->serializeItems([$item], 0, 1, 1); ``` @@ -59,4 +61,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/examples/XmlExample.php b/examples/XmlExample.php index 271c846..4e918a9 100644 --- a/examples/XmlExample.php +++ b/examples/XmlExample.php @@ -1,23 +1,15 @@ serializeItems($itemsToExport, 0, 1, 1); } - private function addAttributes($item, $itemData) + private function addAttributes(Item $item, $itemData) { $attributesData = [ 'cat' => [ @@ -86,73 +78,28 @@ private function addAttributes($item, $itemData) } } - private function addBonuses($item, $itemData) + private function addBonuses(Item $item, $itemData) { - $bonusesData = [ - '' => [ - 3 - ], - 'LNrLF7BRVJ0toQ==' => [ - 5 - ] - ]; - - $bonusElement = new Bonus(); - - foreach ($bonusesData as $usergroup => $bonuses) { - foreach ($bonuses as $bonus) { - $bonusElement->setValue($bonus, $usergroup); - } - } - - $item->setBonus($bonusElement); + $item->addBonus(3); + $item->addBonus(5, 'LNrLF7BRVJ0toQ=='); } - private function addDateAddeds($item, $itemData) + private function addDateAddeds(Item $item, $itemData) { - $dateAddedsData = [ - '' => [ - new \DateTime() - ], - 'LNrLF7BRVJ0toQ==' => [ - new \DateTime() - ] - ]; - - $dateAddedElement = new DateAdded(); - - foreach ($dateAddedsData as $usergroup => $dateAddeds) { - foreach ($dateAddeds as $dateAdded) { - $dateAddedElement->setDateValue($dateAdded, $usergroup); - } - } - - $item->setDateAdded($dateAddedElement); + $item->addDateAdded(new \DateTime()); + $item->addDateAdded(new \DateTime(), 'LNrLF7BRVJ0toQ=='); } - private function addDescriptions($item, $itemData) + private function addDescriptions(Item $item, $itemData) { - $descriptionsData = [ - '' => [ - 'With this sneaker you will walk in style. It\'s available in green and blue.' - ], - 'LNrLF7BRVJ0toQ==' => [ - 'With this men\'s sneaker you will walk in style. It\'s comes in various sizes and colors.' - ] - ]; - - $descriptionElement = new Description(); - - foreach ($descriptionsData as $usergroup => $descriptions) { - foreach ($descriptions as $description) { - $descriptionElement->setValue($description, $usergroup); - } - } - - $item->setDescription($descriptionElement); + $item->addDescription('With this sneaker you will walk in style. It\'s available in green and blue.'); + $item->addDescription( + 'With this men\'s sneaker you will walk in style. It\'s comes in various sizes and colors.', + 'LNrLF7BRVJ0toQ==' + ); } - private function addOrdernumbers($item, $itemData) + private function addOrdernumbers(Item $item, $itemData) { $ordernumbersData = [ '' => [ @@ -171,7 +118,7 @@ private function addOrdernumbers($item, $itemData) } } - private function addImages($item, $itemData) + private function addImages(Item $item, $itemData) { $imagesData = [ '' => [ @@ -191,7 +138,7 @@ private function addImages($item, $itemData) } } - private function addKeywords($item, $itemData) + private function addKeywords(Item $item, $itemData) { $keywordsData = [ '' => [ @@ -210,51 +157,19 @@ private function addKeywords($item, $itemData) } } - private function addNames($item, $itemData) + private function addNames(Item $item, $itemData) { - $namesData = [ - '' => [ - 'Adidas Sneaker' - ], - 'LNrLF7BRVJ0toQ==' => [ - 'Adidas Men\'s Sneaker' - ] - ]; - - $nameElement = new Name(); - - foreach ($namesData as $usergroup => $names) { - foreach ($names as $name) { - $nameElement->setValue($name, $usergroup); - } - } - - $item->setName($nameElement); + $item->addName('Adidas Sneaker'); + $item->addName('Adidas Men\'s Sneaker', 'LNrLF7BRVJ0toQ=='); } - private function addPrices($item, $itemData) + private function addPrices(Item $item, $itemData) { - $pricesData = [ - '' => [ - 44.8 - ], - 'LNrLF7BRVJ0toQ==' => [ - 45.9 - ] - ]; - - $priceElement = new Price(); - - foreach ($pricesData as $usergroup => $prices) { - foreach ($prices as $price) { - $priceElement->setValue($price, $usergroup); - } - } - - $item->setPrice($priceElement); + $item->addPrice(44.8); + $item->addPrice(45.9, 'LNrLF7BRVJ0toQ=='); } - private function addProperties($item, $itemData) + private function addProperties(Item $item, $itemData) { $propertiesData = [ 'sale' => [ @@ -289,95 +204,31 @@ private function addProperties($item, $itemData) } } - private function addSalesFrequencies($item, $itemData) + private function addSalesFrequencies(Item $item, $itemData) { - $salesFrequenciesData = [ - '' => [ - 5 - ], - 'LNrLF7BRVJ0toQ==' => [ - 5 - ] - ]; - - $salesFrequencyElement = new SalesFrequency(); - - foreach ($salesFrequenciesData as $usergroup => $salesFrequencies) { - foreach ($salesFrequencies as $salesFrequency) { - $salesFrequencyElement->setValue($salesFrequency, $usergroup); - } - } - - $item->setSalesFrequency($salesFrequencyElement); + $item->addSalesFrequency(5); + $item->addSalesFrequency(10, 'LNrLF7BRVJ0toQ=='); } - private function addSorts($item, $itemData) + private function addSorts(Item $item, $itemData) { - $sortsData = [ - '' => [ - 5 - ], - 'LNrLF7BRVJ0toQ==' => [ - 7 - ] - ]; - - $sortElement = new Sort(); - - foreach ($sortsData as $usergroup => $sorts) { - foreach ($sorts as $sort) { - $sortElement->setValue($sort, $usergroup); - } - } - - $item->setSort($sortElement); + $item->addSort(5); + $item->addSort(7, 'LNrLF7BRVJ0toQ=='); } - private function addSummaries($item, $itemData) + private function addSummaries(Item $item, $itemData) { - $summariesData = [ - '' => [ - 'A cool and fashionable sneaker' - ], - 'LNrLF7BRVJ0toQ==' => [ - 'A cool and fashionable sneaker for men' - ] - ]; - - $summaryElement = new Summary(); - - foreach ($summariesData as $usergroup => $summaries) { - foreach ($summaries as $summary) { - $summaryElement->setValue($summary, $usergroup); - } - } - - $item->setSummary($summaryElement); + $item->addSummary('A cool and fashionable sneaker'); + $item->addSummary('A cool and fashionable sneaker for men', 'LNrLF7BRVJ0toQ=='); } - private function addUrls($item, $itemData) + private function addUrls(Item $item, $itemData) { - $urlsData = [ - '' => [ - 'https://www.store.com/sneakers/adidas.html' - ], - 'LNrLF7BRVJ0toQ==' => [ - 'https://www.store.com/sneakers/mens/adidas.html' - ] - ]; - - $urlElement = new Url(); - - foreach ($urlsData as $usergroup => $urls) { - foreach ($urls as $url) { - $urlElement->setValue($url, $usergroup); - } - } - - $item->setUrl($urlElement); + $item->addUrl('https://www.store.com/sneakers/adidas.html'); + $item->addUrl('https://www.store.com/sneakers/mens/adidas.html', 'LNrLF7BRVJ0toQ=='); } - private function addUsergroups($item, $itemData) + private function addUsergroups(Item $item, $itemData) { $usergroups = [ 'LNrLF7BRVJ0toQ==', 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()); + } }