diff --git a/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php b/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php index 103167d42..8a015f9bb 100644 --- a/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php +++ b/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php @@ -12,6 +12,11 @@ namespace Thelia\Core\FileFormat\Formatting; use Thelia\Core\FileFormat\FormatInterface; +<<<<<<< HEAD +======= +use Thelia\Core\Translation\Translator; +use Thelia\Log\Tlog; +>>>>>>> Remove conflicts /** * Class AbstractFormatter @@ -20,5 +25,35 @@ use Thelia\Core\FileFormat\FormatInterface; */ abstract class AbstractFormatter implements FormatInterface, FormatterInterface { +<<<<<<< HEAD +======= + /** @var \Thelia\Core\Translation\Translator */ + protected $translator; + + /** @var \Thelia\Log\Tlog */ + protected $logger; + + /** @var array */ + protected $aliases = array(); + + public function __construct() + { + $this->translator = Translator::getInstance(); + + $this->logger = Tlog::getInstance(); + } + + public function setAliases(array $aliases) + { + $this->aliases = $aliases; + + return $this; + } + + public function getAliases() + { + return $this->aliases; + } +>>>>>>> Remove conflicts } diff --git a/core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php b/core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php index 103e3d616..bba78c6b2 100644 --- a/core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php +++ b/core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php @@ -16,7 +16,11 @@ use Propel\Runtime\ActiveQuery\ModelCriteria; use Propel\Runtime\ActiveQuery\ModelJoin; use Propel\Runtime\Map\TableMap; use Thelia\Core\Translation\Translator; +<<<<<<< HEAD +======= +use Thelia\Model\Map\ProductTableMap; +>>>>>>> Remove conflicts /** * Class FormatterData * @package Thelia\Core\FileFormat\Formatting @@ -25,7 +29,11 @@ use Thelia\Core\Translation\Translator; class FormatterData { /** @var array */ +<<<<<<< HEAD protected $data; +======= + protected $data = array(); +>>>>>>> Remove conflicts /** @var null|array */ protected $aliases; @@ -72,6 +80,10 @@ class FormatterData * @return $this * * Sets raw data with aliases +<<<<<<< HEAD +======= + * may bug with some formatter +>>>>>>> Remove conflicts */ public function setData(array $data) { @@ -158,8 +170,80 @@ class FormatterData return $formattedData; } +<<<<<<< HEAD +======= + /** + * @param array $row + * @return $this + */ + public function addRow(array $row) + { + $this->data += [$this->applyAliases($row, $this->aliases)]; + + return $this; + } + + /** + * @param int $index + * @return array|bool + */ + public function popRow($index = 0) + { + $row = $this->getRow($index); + + if (false !== $row) { + unset($this->data[$index]); + } + + return $row; + } + + /** + * @param int $index + * @return array|bool + * @throws \OutOfBoundsException + */ + public function getRow($index = 0) + { + if (empty($this->data)) { + return false; + } elseif (!isset($this->data[$index])) { + throw new \OutOfBoundsException( + $this->translator->trans( + "Bad index value %idx", + [ + "%idx" => $index + ] + ) + ); + } + + $row = $this->reverseAliases($this->data[$index], $this->aliases); + + return $row; + } + + /** + * @param array $data + * @param array $aliases + * @return array + */ + protected function reverseAliases(array $data, array $aliases) + { + return $this->applyAliases($data, array_flip($aliases)); + } + +>>>>>>> Remove conflicts public function getData() { return $this->data; } +<<<<<<< HEAD +======= + + public function getDataReverseAliases() + { + return $this->reverseAliases($this->data, $this->aliases); + } +>>>>>>> Remove conflicts } diff --git a/core/lib/Thelia/Tests/FileFormat/Formatting/FormatterDataTest.php b/core/lib/Thelia/Tests/FileFormat/Formatting/FormatterDataTest.php index d5747ef33..f2ae7542f 100644 --- a/core/lib/Thelia/Tests/FileFormat/Formatting/FormatterDataTest.php +++ b/core/lib/Thelia/Tests/FileFormat/Formatting/FormatterDataTest.php @@ -359,4 +359,109 @@ class FormatterDataTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedData,$formattedData); } +<<<<<<< HEAD +======= + public function testSetRawDataMultipleDepthWithReverseAliases() { + $aliases = [ + "orange" => "foo", + "blackberry" => "banana", + ]; + + $formatterData = new FormatterData($aliases); + + $data = [ + "orange" => "banana", + "apple" => "pear", + [ + "orange" => "tomato", + "pepper" => "pear", + ], + [ + [ + "strawberry" => "raspberry", + "blackberry" => "cranberry", + ], + [ + "cherry" => "lemon", + "mango" => "cranberry", + ] + ], + ]; + + $formattedData = $formatterData + ->setData($data) + ->getDataReverseAliases() + ; + + $this->assertEquals($data,$formattedData); + } + + /** + * That's why an alias MUST not be the same as a present value + */ + public function testSetRawDataMultipleDepthWithReverseAliasesFail() { + $aliases = [ + "orange" => "cherry", + "blackberry" => "banana", + ]; + + $formatterData = new FormatterData($aliases); + + $data = [ + "orange" => "banana", + "apple" => "pear", + [ + "orange" => "tomato", + "pepper" => "pear", + ], + [ + [ + "strawberry" => "raspberry", + "blackberry" => "cranberry", + ], + [ + "cherry" => "lemon", + "mango" => "cranberry", + ] + ], + ]; + + $formattedData = $formatterData + ->setData($data) + ->getDataReverseAliases() + ; + + $this->assertNotEquals($data,$formattedData); + } + + public function testAddRow() + { + $data = new FormatterData(); + + $row = [ + "title" => "A super book", + "author" => "Manu", + ]; + + $data->addRow($row); + + $this->assertEquals([$row], $data->getData()); + $this->assertEquals($row, $data->getRow()); + } + + public function testPopRow() + { + $data = new FormatterData(); + + $row = [ + "title" => "A super book", + "author" => "Manu", + ]; + + $data->addRow($row); + + $this->assertEquals($row, $data->popRow()); + $this->assertFalse($data->getRow()); + } +>>>>>>> Remove conflicts }