From 2b19d1fe33db5ba7728c22750dd17e1db7e7a121 Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Mon, 7 Jul 2014 16:50:10 +0200 Subject: [PATCH] =?UTF-8?q?End=20formatter=20data=20and=20refactor=20Theli?= =?UTF-8?q?a\Core\FileFormat\Formatter=20=09renomm=C3=A9:=20=20=20=20=20?= =?UTF-8?q?=20=20=20=20core/lib/Thelia/Core/FileFormat/Formatter/AbstractF?= =?UTF-8?q?ormatter.php=20->=20core/lib/Thelia/Core/FileFormat/Formatting/?= =?UTF-8?q?AbstractFormatter.php=20=09renomm=C3=A9:=20=20=20=20=20=20=20?= =?UTF-8?q?=20=20core/lib/Thelia/Core/FileFormat/Formatter/Exception/BadFo?= =?UTF-8?q?rmattedStringException.php=20->=20core/lib/Thelia/Core/FileForm?= =?UTF-8?q?at/Formatting/Exception/BadFormattedStringException.php=20=09re?= =?UTF-8?q?nomm=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Core/File?= =?UTF-8?q?Format/Formatter/FormatterData.php=20->=20core/lib/Thelia/Core/?= =?UTF-8?q?FileFormat/Formatting/FormatterData.php=20=09renomm=C3=A9:=20?= =?UTF-8?q?=20=20=20=20=20=20=20=20core/lib/Thelia/Core/FileFormat/Formatt?= =?UTF-8?q?er/FormatterInterface.php=20->=20core/lib/Thelia/Core/FileForma?= =?UTF-8?q?t/Formatting/FormatterInterface.php=20=09renomm=C3=A9:=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20core/lib/Thelia/Core/FileFormat/Formatter/For?= =?UTF-8?q?matterManager.php=20->=20core/lib/Thelia/Core/FileFormat/Format?= =?UTF-8?q?ting/FormatterManager.php=20=09renomm=C3=A9:=20=20=20=20=20=20?= =?UTF-8?q?=20=20=20core/lib/Thelia/Tests/FileFormat/Formatter/FormatterDa?= =?UTF-8?q?taTest.php=20->=20core/lib/Thelia/Tests/FileFormat/Formatting/F?= =?UTF-8?q?ormatterDataTest.php=20=09renomm=C3=A9:=20=20=20=20=20=20=20=20?= =?UTF-8?q?=20core/lib/Thelia/Tests/FileFormat/Formatter/FormatterManagerT?= =?UTF-8?q?est.php=20->=20core/lib/Thelia/Tests/FileFormat/Formatting/Form?= =?UTF-8?q?atterManagerTest.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Formatter/AbstractFormatter.php | 24 -- .../Exception/BadFormattedStringException.php | 23 -- .../FileFormat/Formatter/FormatterData.php | 166 -------- .../Formatter/FormatterInterface.php | 39 -- .../FileFormat/Formatter/FormatterManager.php | 102 ----- .../Formatter/FormatterDataTest.php | 370 ------------------ .../Formatter/FormatterManagerTest.php | 74 ---- 7 files changed, 798 deletions(-) delete mode 100644 core/lib/Thelia/Core/FileFormat/Formatter/AbstractFormatter.php delete mode 100644 core/lib/Thelia/Core/FileFormat/Formatter/Exception/BadFormattedStringException.php delete mode 100644 core/lib/Thelia/Core/FileFormat/Formatter/FormatterData.php delete mode 100644 core/lib/Thelia/Core/FileFormat/Formatter/FormatterInterface.php delete mode 100644 core/lib/Thelia/Core/FileFormat/Formatter/FormatterManager.php delete mode 100644 core/lib/Thelia/Tests/FileFormat/Formatter/FormatterDataTest.php delete mode 100644 core/lib/Thelia/Tests/FileFormat/Formatter/FormatterManagerTest.php diff --git a/core/lib/Thelia/Core/FileFormat/Formatter/AbstractFormatter.php b/core/lib/Thelia/Core/FileFormat/Formatter/AbstractFormatter.php deleted file mode 100644 index 605bc6362..000000000 --- a/core/lib/Thelia/Core/FileFormat/Formatter/AbstractFormatter.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -abstract class AbstractFormatter implements FormatInterface, FormatterInterface -{ - -} diff --git a/core/lib/Thelia/Core/FileFormat/Formatter/Exception/BadFormattedStringException.php b/core/lib/Thelia/Core/FileFormat/Formatter/Exception/BadFormattedStringException.php deleted file mode 100644 index 18d28bb78..000000000 --- a/core/lib/Thelia/Core/FileFormat/Formatter/Exception/BadFormattedStringException.php +++ /dev/null @@ -1,23 +0,0 @@ - - */ -class BadFormattedStringException extends \ErrorException -{ - -} diff --git a/core/lib/Thelia/Core/FileFormat/Formatter/FormatterData.php b/core/lib/Thelia/Core/FileFormat/Formatter/FormatterData.php deleted file mode 100644 index f2aaa8507..000000000 --- a/core/lib/Thelia/Core/FileFormat/Formatter/FormatterData.php +++ /dev/null @@ -1,166 +0,0 @@ - - */ -class FormatterData -{ - /** @var array */ - protected $data; - - /** @var null|array */ - protected $aliases; - - /** @var Translator */ - protected $translator; - - /** - * @param array $aliases - * - * $aliases is a associative array where the key represents propel TYPE_PHP_NAME of column if you use - * loadModelCriteria, or your own aliases for setData, and the value - * is the alias. It can be null or empty if you don't want aliases, - * but remember to always define all the fields the you want: - * non aliases fields will be ignored. - */ - public function __construct(array $aliases = null) - { - $this->translator = Translator::getInstance(); - - if (!is_array($aliases)) { - $aliases = []; - } - - /** - * Lower all the values - */ - foreach ($aliases as $key => $value) { - $lowerKey = strtolower($key); - $lowerValue = strtolower($value); - if ($lowerKey !== $key) { - $aliases[$lowerKey] = $lowerValue; - unset($aliases[$key]); - } else { - $aliases[$key] = $lowerValue; - } - } - - $this->aliases = $aliases; - } - - /** - * @param array $data - * @return $this - * - * Sets raw data with aliases - */ - public function setData(array $data) - { - if (empty($this->aliases)) { - $this->data = $data; - return $this; - } - - $this->data = $this->applyAliases($data, $this->aliases); - - return $this; - } - - /** - * @param ModelCriteria $criteria - * @return $this|null - * - * Loads a model criteria. - * Warning: if you want to do multi table export, - * you'll have to use you own select and not the joinYourTable method. - * For more details, please see the unit test - * Thelia\Tests\FileFormat\Formatter\FormatterDataTest::testFormatSimpleMultipleTableQuery - */ - public function loadModelCriteria(ModelCriteria $criteria) - { - $propelData = $criteria->find(); - - if (empty($propelData)) { - return null; - } - - $asColumns = $propelData->getFormatter()->getAsColumns(); - - /** - * Format it correctly - * After this pass, we MUST have a 2D array. - * The first may be keyed with integers. - */ - $formattedResult = $propelData - ->toArray(null, false, TableMap::TYPE_COLNAME); - - if (count($asColumns) > 1) { - /** - * Request with multiple select - * Apply propel aliases - */ - $formattedResult = $this->applyAliases($formattedResult, $asColumns); - } elseif (count($asColumns) === 1) { - /** - * Request with one select - */ - $key = str_replace("\"", "", array_keys($asColumns)[0]); - $formattedResult = [[$key => $formattedResult[0]]]; - } - - $data = $this->applyAliases($formattedResult, $this->aliases); - - /** - * Then store it - */ - $this->data = $data; - - return $this; - } - - /** - * @param array $data - * @param array $aliases - */ - protected function applyAliases(array $data, array $aliases) - { - $formattedData = []; - - foreach ($data as $key=>$entry) { - $key = strtolower($key); - - if (is_array($entry)) { - $formattedData[$key] = $this->applyAliases($entry, $aliases); - } else { - $alias = isset($aliases[$key]) ? $aliases[$key] : $key; - $formattedData[$alias] = $entry; - } - } - - return $formattedData; - } - - public function getData() - { - return $this->data; - } -} diff --git a/core/lib/Thelia/Core/FileFormat/Formatter/FormatterInterface.php b/core/lib/Thelia/Core/FileFormat/Formatter/FormatterInterface.php deleted file mode 100644 index 4d2d2d91e..000000000 --- a/core/lib/Thelia/Core/FileFormat/Formatter/FormatterInterface.php +++ /dev/null @@ -1,39 +0,0 @@ - - */ -interface FormatterInterface -{ - /** - * @param FormatterData $data - * @return mixed - * - * This method must use a FormatterData object and output - * a formatted value. - */ - public function encode(FormatterData $data); - - /** - * @param $rawData - * @return FormatterData - * - * This must takes raw data as argument and outputs - * a FormatterData object. - */ - public function decode($rawData); -} diff --git a/core/lib/Thelia/Core/FileFormat/Formatter/FormatterManager.php b/core/lib/Thelia/Core/FileFormat/Formatter/FormatterManager.php deleted file mode 100644 index b7801c7a9..000000000 --- a/core/lib/Thelia/Core/FileFormat/Formatter/FormatterManager.php +++ /dev/null @@ -1,102 +0,0 @@ - - */ -class FormatterManager -{ - - protected $formatters = array(); - - /** - * @param $archiveCreator - * @return $this - */ - public function add(AbstractFormatter $formatter) - { - if (null !== $formatter) { - $this->formatters[$formatter->getName()] = $formatter; - } - - return $this; - } - - /** - * @param $name - * @return $this - * @throws \OutOfBoundsException - */ - public function delete($name) - { - if (!array_key_exists($name, $this->formatters)) { - $this->throwOutOfBounds($name); - } - - unset($this->formatters[$name]); - - return $this; - } - - public function get($name) - { - if (!array_key_exists($name, $this->formatters)) { - $this->throwOutOfBounds($name); - } - - return $this->formatters[$name]; - } - - /** - * @return array[AbstractFormatter] - */ - public function getAll() - { - return $this->formatters; - } - - /** - * @return array - */ - public function getNames() - { - $names = []; - - /** @var AbstractFormatter $formatter */ - foreach($this->formatters as $formatter) { - $names[] = $formatter->getName(); - } - - return $names; - } - - /** - * @param $name - * @throws \OutOfBoundsException - */ - protected function throwOutOfBounds($name) - { - throw new \OutOfBoundsException( - Translator::getInstance()->trans( - "The formatter %name doesn't exist", - [ - "%name" => $name - ] - ) - ); - } -} diff --git a/core/lib/Thelia/Tests/FileFormat/Formatter/FormatterDataTest.php b/core/lib/Thelia/Tests/FileFormat/Formatter/FormatterDataTest.php deleted file mode 100644 index 31949a60c..000000000 --- a/core/lib/Thelia/Tests/FileFormat/Formatter/FormatterDataTest.php +++ /dev/null @@ -1,370 +0,0 @@ - - */ -class FormatterDataTest extends \PHPUnit_Framework_TestCase -{ - - public function setUp() - { - new Translator(new Container()); - - /* - ->filterById([3,4,5], Criteria::IN); - - $this->data->loadModelCriteria($query); - - $query = ProductSaleElementsQuery::create() - ->joinProduct() - ->select(["ProductSaleElements.id", "Product.id"]) - ->filterById([3,4,5], Criteria::IN) - ; - - $this->data->loadModelCriteria($query); - - $query = ProductSaleElementsQuery::create() - ->joinProduct() - ->select(["ProductSaleElements.id"]) - ->filterById([3,4,5], Criteria::IN) - ; - - $this->data->loadModelCriteria($query); - - $query = ProductQuery::create() - ->joinProductSaleElements() - ->filterById([3,4,5], Criteria::IN); - - $this->data->loadModelCriteria($query);*/ - } - - public function testFormatSimpleQuery() - { - $formatterData = new FormatterData(); - - $query = ConfigQuery::create() - ->limit(1) - ; - - $formattedData = $formatterData - ->loadModelCriteria($query) - ->getData() - ; - - /** @var \Thelia\Model\Config $result */ - $result = $query->findOne(); - - $formattedResult = [ - [ - "config.id" => $result->getId(), - "config.name" => $result->getName(), - "config.value" => $result->getValue(), - "config.created_at" => $result->getCreatedAt(), - "config.updated_at" => $result->getUpdatedAt(), - "config.hidden" => $result->getHidden(), - "config.secured" => $result->getHidden(), - ], - ]; - - $this->assertEquals($formattedResult,$formattedData); - } - - public function testFormatSimpleQueryWithAliases() - { - /** - * Aliases must not be case sensitive - */ - $aliases = [ - "coNfiG.iD" => "id", - "conFig.NaMe" => "name", - "CoNfIg.Value" => "value", - "config.hidden" => "hidden", - "ConFig.Secured" => "secured", - ]; - - $formatterData = new FormatterData($aliases); - - $query = ConfigQuery::create() - ->limit(1) - ; - - $formattedData = $formatterData - ->loadModelCriteria($query) - ->getData() - ; - - /** @var \Thelia\Model\Config $result */ - $result = $query->findOne(); - - $formattedResult = [ - [ - "id" => $result->getId(), - "name" => $result->getName(), - "value" => $result->getValue(), - "config.created_at" => $result->getCreatedAt(), - "config.updated_at" => $result->getUpdatedAt(), - "hidden" => $result->getHidden(), - "secured" => $result->getHidden(), - ], - ]; - - $this->assertEquals($formattedResult,$formattedData); - } - - public function testFormatSimpleMultipleTableQuery() - { - $formatterData = new FormatterData(); - - - } - - public function testFormatSimpleMultipleTableQueryWithAliases() - { - /** - * Aliases must not be case sensitive - */ - $aliases = [ - "coNfiG.iD" => "id", - "conFig.NaMe" => "name", - "CoNfIg.Value" => "value", - "config.hidden" => "hidden", - "ConFig.Secured" => "secured", - ]; - - $formatterData = new FormatterData($aliases); - - $query = ConfigQuery::create() - ->limit(1) - ; - - $formattedData = $formatterData - ->loadModelCriteria($query) - ->getData() - ; - - /** @var \Thelia\Model\Config $result */ - $result = $query->findOne(); - - $formattedResult = [ - [ - "id" => $result->getId(), - "name" => $result->getName(), - "value" => $result->getValue(), - "config.created_at" => $result->getCreatedAt(), - "config.updated_at" => $result->getUpdatedAt(), - "hidden" => $result->getHidden(), - "secured" => $result->getHidden(), - ], - ]; - - $this->assertEquals($formattedResult,$formattedData); - } - - public function testSetRawDataDepth1() { - $formatterData = new FormatterData(); - - $data = [ - "foo" => "bar", - "baz" => "foo", - ]; - - $formattedData = $formatterData - ->setData($data) - ->getData() - ; - - $this->assertEquals($data,$formattedData); - } - - public function testSetRawDataDepth1WithAliases() { - $aliases = [ - "FoO" => "orange", - "Baz" => "banana", - ]; - - $formatterData = new FormatterData($aliases); - - $data = [ - "fOo" => "bar", - "bAZ" => "foo", - ]; - - $expectedData = [ - "orange" => "bar", - "banana" => "foo", - ]; - - $formattedData = $formatterData - ->setData($data) - ->getData() - ; - - $this->assertEquals($expectedData,$formattedData); - } - - public function testSetRawDataDepth2() { - $formatterData = new FormatterData(); - - $data = [ - [ - "orange" => "banana", - "apple" => "pear", - ], - [ - "strawberry" => "raspberry", - "blackberry" => "cranberry", - ] - ]; - - $formattedData = $formatterData - ->setData($data) - ->getData() - ; - - $this->assertEquals($data,$formattedData); - } - - public function testSetRawDataDepth2WithAliases() { - $aliases = [ - "orange" => "cherry", - "blackberry" => "banana", - ]; - - $formatterData = new FormatterData($aliases); - - $data = [ - [ - "orange" => "banana", - "apple" => "pear", - ], - [ - "strawberry" => "raspberry", - "blackberry" => "cranberry", - ] - ]; - - $expectedData = [ - [ - "cherry" => "banana", - "apple" => "pear", - ], - [ - "strawberry" => "raspberry", - "banana" => "cranberry", - ] - ]; - - $formattedData = $formatterData - ->setData($data) - ->getData() - ; - - $this->assertEquals($expectedData,$formattedData); - } - - public function testSetRawDataMultipleDepth() { - $formatterData = new FormatterData(); - - $data = [ - [ - "orange" => "banana", - "apple" => "pear", - ], - [ - "strawberry" => "raspberry", - "blackberry" => "cranberry", - ] - ]; - - $formattedData = $formatterData - ->setData($data) - ->getData() - ; - - $this->assertEquals($data,$formattedData); - } - - public function testSetRawDataMultipleDepthWithAliases() { - $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", - ] - ], - ]; - - $expectedData = [ - "cherry" => "banana", - "apple" => "pear", - [ - "cherry" => "tomato", - "pepper" => "pear", - ], - [ - [ - "strawberry" => "raspberry", - "banana" => "cranberry", - ], - [ - "cherry" => "lemon", - "mango" => "cranberry", - ] - ], - ]; - - $formattedData = $formatterData - ->setData($data) - ->getData() - ; - - $this->assertEquals($expectedData,$formattedData); - } - -} diff --git a/core/lib/Thelia/Tests/FileFormat/Formatter/FormatterManagerTest.php b/core/lib/Thelia/Tests/FileFormat/Formatter/FormatterManagerTest.php deleted file mode 100644 index 39bf06cb3..000000000 --- a/core/lib/Thelia/Tests/FileFormat/Formatter/FormatterManagerTest.php +++ /dev/null @@ -1,74 +0,0 @@ - - */ -class FormatterManagerTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var FormatterManager - */ - protected $manager; - - public function setUp() - { - new Translator( - new Container() - ); - $this->manager = new FormatterManager(); - } - - public function testAddFormatter() - { - /** @var AbstractFormatter $instance */ - $instance = $this->getMock("Thelia\\Core\\FileFormat\\Formatter\\AbstractFormatter"); - - $this->manager->add($instance); - - $archiveBuilders = $this->manager->getAll(); - - $this->assertTrue( - array_key_exists($instance->getName(), $archiveBuilders) - ); - } - - public function testDeleteFormatter() - { - /** @var AbstractFormatter $instance */ - $instance = $this->getMock("Thelia\\Core\\FileFormat\\Formatter\\AbstractFormatter"); - - $this->manager->add($instance); - - $this->manager->delete($instance->getName()); - - $this->assertTrue( - count($this->manager->getAll()) === 0 - ); - } - - /** - * @expectedException \OutOfBoundsException - */ - public function testDeleteNotExistingFormatter() - { - $this->manager->delete("foo"); - } -}