Add some tests
modifié: core/lib/Thelia/Core/FileFormat/Formatter/FormatterData.php modifié: core/lib/Thelia/Tests/FileFormat/Formatter/FormatterDataTest.php
This commit is contained in:
@@ -11,9 +11,10 @@
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Core\FileFormat\Formatter;
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
use Propel\Runtime\ActiveQuery\ModelCriteria;
|
||||
use Propel\Runtime\Collection\ArrayCollection;
|
||||
use Propel\Runtime\Collection\ObjectCollection;
|
||||
use Propel\Runtime\ActiveQuery\ModelJoin;
|
||||
use Propel\Runtime\Map\TableMap;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
|
||||
/**
|
||||
@@ -26,14 +27,74 @@ class FormatterData
|
||||
/** @var array */
|
||||
protected $data;
|
||||
|
||||
/** @var null|array */
|
||||
protected $aliases;
|
||||
|
||||
/** @var Translator */
|
||||
protected $translator;
|
||||
|
||||
public function __construct()
|
||||
/**
|
||||
* @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();
|
||||
@@ -44,23 +105,62 @@ class FormatterData
|
||||
|
||||
$asColumns = $propelData->getFormatter()->getAsColumns();
|
||||
|
||||
if (empty($asColumns) && $propelData instanceof ObjectCollection) {
|
||||
/**
|
||||
* Full request ( without select nor join )
|
||||
*/
|
||||
} elseif (empty($asColumns) && $propelData instanceof ArrayCollection) {
|
||||
/**
|
||||
* Request with joins, but without select
|
||||
*/
|
||||
} elseif (count($asColumns) > 1) {
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user