End formatter data and refactor Thelia\Core\FileFormat\Formatter

renommé:         core/lib/Thelia/Core/FileFormat/Formatter/AbstractFormatter.php -> core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php
	renommé:         core/lib/Thelia/Core/FileFormat/Formatter/Exception/BadFormattedStringException.php -> core/lib/Thelia/Core/FileFormat/Formatting/Exception/BadFormattedStringException.php
	renommé:         core/lib/Thelia/Core/FileFormat/Formatter/FormatterData.php -> core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php
	renommé:         core/lib/Thelia/Core/FileFormat/Formatter/FormatterInterface.php -> core/lib/Thelia/Core/FileFormat/Formatting/FormatterInterface.php
	renommé:         core/lib/Thelia/Core/FileFormat/Formatter/FormatterManager.php -> core/lib/Thelia/Core/FileFormat/Formatting/FormatterManager.php
	renommé:         core/lib/Thelia/Tests/FileFormat/Formatter/FormatterDataTest.php -> core/lib/Thelia/Tests/FileFormat/Formatting/FormatterDataTest.php
	renommé:         core/lib/Thelia/Tests/FileFormat/Formatter/FormatterManagerTest.php -> core/lib/Thelia/Tests/FileFormat/Formatting/FormatterManagerTest.php
This commit is contained in:
Benjamin Perche
2014-07-07 16:50:10 +02:00
parent ab241c8581
commit 2b19d1fe33
7 changed files with 0 additions and 798 deletions

View File

@@ -1,24 +0,0 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core\FileFormat\Formatter;
use Thelia\Core\FileFormat\FormatInterface;
/**
* Class AbstractFormatter
* @package Thelia\Core\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
abstract class AbstractFormatter implements FormatInterface, FormatterInterface
{
}

View File

@@ -1,23 +0,0 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core\FileFormat\Formatter\Exception;
/**
* Class BadFormattedStringException
* @package Thelia\Core\FileFormat\Formatter\Exception
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class BadFormattedStringException extends \ErrorException
{
}

View File

@@ -1,166 +0,0 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core\FileFormat\Formatter;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\ActiveQuery\ModelJoin;
use Propel\Runtime\Map\TableMap;
use Thelia\Core\Translation\Translator;
/**
* Class FormatterData
* @package Thelia\Core\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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;
}
}

View File

@@ -1,39 +0,0 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core\FileFormat\Formatter;
/**
* Interface FormatterInterface
* @package Thelia\Core\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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);
}

View File

@@ -1,102 +0,0 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core\FileFormat\Formatter;
use Thelia\Core\Translation\Translator;
/**
* Class FormatterManager
* @package Thelia\Core\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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
]
)
);
}
}

View File

@@ -1,370 +0,0 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Tests\FileFormat\Formatter;
use Propel\Generator\Builder\Om\QueryBuilder;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\DataFetcher\ArrayDataFetcher;
use Propel\Runtime\Formatter\ArrayFormatter;
use Propel\Runtime\Map\TableMap;
use Propel\Runtime\Propel;
use Symfony\Component\DependencyInjection\Container;
use Thelia\Core\FileFormat\Formatter\FormatterData;
use Thelia\Core\Thelia;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Base\ProductQuery;
use Thelia\Model\Base\ProductSaleElementsQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Map\ProductTableMap;
use Thelia\Model\Product;
/**
* Class FormatterDataTest
* @package Thelia\Tests\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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);
}
}

View File

@@ -1,74 +0,0 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Tests\FileFormat\Formatter;
use Symfony\Component\DependencyInjection\Container;
use Thelia\Core\FileFormat\Formatter\AbstractFormatter;
use Thelia\Core\FileFormat\Formatter\FormatterManager;
use Thelia\Core\Translation\Translator;
/**
* Class FormatterManagerTest
* @package Thelia\Tests\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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");
}
}