Skip to content

Commit

Permalink
Refactore DcaUtil::getDcaFields(), some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Oct 20, 2023
1 parent 5fa1bef commit db317b1
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util\Data;
namespace HeimrichHannot\UtilsBundle\Util;

class AnonymizeUtil
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\UtilsBundle\Util\Container;
namespace HeimrichHannot\UtilsBundle\Util;

use Contao\CoreBundle\ContaoCoreBundle;
use Contao\CoreBundle\Framework\ContaoFramework;
Expand Down
49 changes: 8 additions & 41 deletions src/Util/Dca/DcaUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,42 +134,9 @@ function ($a) use (&$arrFields) {
/**
* Return a list of dca fields for given table.
* Fields can be filtered by given options.
*
* Options:
* - onlyDatabaseFields (bool): Return only fields with sql definition. Default false
* - allowedInputTypes (array): Return only fields of given types.
* - evalConditions (array): Return only fields with given eval key-value-pairs.
* - localizeLabels (bool): Return also the field labels (key = field name, value = field label). Default false
* - skipSorting (bool): Skip sorting fields by field name alphabetical. Default false
*
* @param array{
* onlyDatabaseFields?: bool,
* allowedInputTypes?: array,
* evalConditions?: array,
* localizeLabels?: bool,
* skipSorting?: bool
* } $options
*
* @throws \Exception
*/
public function getDcaFields(string $table, array $options = []): array
public function getDcaFields(string $table, GetDcaFieldsOptions $options = new GetDcaFieldsOptions()): array
{
$options = array_merge([
'onlyDatabaseFields' => false,
'allowedInputTypes' => [],
'evalConditions' => [],
'localizeLabels' => false,
'skipSorting' => false,
], $options);

if (!\is_array($options['allowedInputTypes'])) {
throw new \Exception('DcaUtil::getDcaFields() option "allowedInputTypes" must be of type array!');
}

if (!\is_array($options['evalConditions'])) {
throw new \Exception('DcaUtil::getDcaFields() option "evalConditions" must be of type array!');
}

$fields = [];

$controller = $this->contaoFramework->getAdapter(Controller::class);
Expand All @@ -181,35 +148,35 @@ public function getDcaFields(string $table, array $options = []): array
}

foreach ($GLOBALS['TL_DCA'][$table]['fields'] as $name => $data) {
if ($options['onlyDatabaseFields']) {
if ($options->isOnlyDatabaseFields()) {
if (!isset($data['sql'])) {
continue;
}
}

// restrict to certain input types
if (!empty($options['allowedInputTypes']) && (!isset($data['inputType']) || !\in_array($data['inputType'], $options['allowedInputTypes']))) {
if ($options->isOnlyAllowedInputTypes() && (!isset($data['inputType']) || !\in_array($data['inputType'], $options->getAllowedInputTypes()))) {
continue;
}

// restrict to certain dca eval
if (!empty($options['evalConditions'])) {
foreach ($options['evalConditions'] as $key => $value) {
if ($options->isHasEvalConditions()) {
foreach ($options->getEvalConditions() as $key => $value) {
if (!isset($data['eval'][$key]) || $data['eval'][$key] !== $value) {
continue 2;
}
}
}

if (!$options['localizeLabels']) {
if (!$options->isLocalizeLabels()) {
$fields[] = $name;
} else {
$fields[$name] = $data['label'][0] ?? $name;
}
}

if (!$options['skipSorting']) {
if ($options['localizeLabels']) {
if (!$options->isSkipSorting()) {
if ($options->isLocalizeLabels()) {
asort($fields);
} else {
sort($fields);
Expand Down
107 changes: 107 additions & 0 deletions src/Util/Dca/GetDcaFieldsOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util\Dca;

class GetDcaFieldsOptions
{
/**
* 'onlyDatabaseFields' => false,
* 'allowedInputTypes' => [],
* 'evalConditions' => [],
* 'localizeLabels' => false,
* 'skipSorting' => false,
*/

private bool $onlyDatabaseFields = false;
private array $allowedInputTypes = [];
private array $evalConditions = [];
private bool $localizeLabels = false;
private bool $skipSorting = false;

public function isOnlyDatabaseFields(): bool
{
return $this->onlyDatabaseFields;
}

public static function create(): self
{
return new self();
}

/**
* Return only fields with sql definition. Default false
*/
public function setOnlyDatabaseFields(bool $onlyDatabaseFields): GetDcaFieldsOptions
{
$this->onlyDatabaseFields = $onlyDatabaseFields;
return $this;
}

public function getAllowedInputTypes(): array
{
return $this->allowedInputTypes;
}

/**
* Return only fields of given types.
*/
public function setAllowedInputTypes(array $allowedInputTypes): GetDcaFieldsOptions
{
$this->allowedInputTypes = $allowedInputTypes;
return $this;
}

public function isOnlyAllowedInputTypes(): bool
{
return !empty($this->allowedInputTypes);
}

public function getEvalConditions(): array
{
return $this->evalConditions;
}

/**
* Return only fields with given eval key-value-pairs.
*/
public function setEvalConditions(array $evalConditions): GetDcaFieldsOptions
{
$this->evalConditions = $evalConditions;
return $this;
}

public function isHasEvalConditions(): bool
{
return !empty($this->evalConditions);
}

public function isLocalizeLabels(): bool
{
return $this->localizeLabels;
}

/**
* Return also the field labels (key = field name, value = field label). Default false
*/
public function setLocalizeLabels(bool $localizeLabels): GetDcaFieldsOptions
{
$this->localizeLabels = $localizeLabels;
return $this;
}

public function isSkipSorting(): bool
{
return $this->skipSorting;
}

/**
* Skip sorting fields by field name alphabetical. Default false
*/
public function setSkipSorting(bool $skipSorting): GetDcaFieldsOptions
{
$this->skipSorting = $skipSorting;
return $this;
}


}
4 changes: 2 additions & 2 deletions src/Util/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace HeimrichHannot\UtilsBundle\Util;

use HeimrichHannot\UtilsBundle\Util\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Util\Data\AnonymizeUtil;
use HeimrichHannot\UtilsBundle\Util\ContainerUtil;
use HeimrichHannot\UtilsBundle\Util\AnonymizeUtil;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\Dca\DcaUtil;
use HeimrichHannot\UtilsBundle\Util\File\FileUtil;
Expand Down
2 changes: 1 addition & 1 deletion tests/Util/Container/ContainerUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\RequestBundle\HeimrichHannotContaoRequestBundle;
use HeimrichHannot\UtilsBundle\HeimrichHannotUtilsBundle;
use HeimrichHannot\UtilsBundle\Util\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Util\ContainerUtil;
use Psr\Log\LogLevel;
use Symfony\Bridge\Monolog\Logger;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
Expand Down
2 changes: 1 addition & 1 deletion tests/Util/Data/AnonymizeUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace HeimrichHannot\UtilsBundle\Tests\Util\Data;

use HeimrichHannot\UtilsBundle\Tests\AbstractUtilsTestCase;
use HeimrichHannot\UtilsBundle\Util\Data\AnonymizeUtil;
use HeimrichHannot\UtilsBundle\Util\AnonymizeUtil;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\TestCase;

Expand Down
39 changes: 25 additions & 14 deletions tests/Util/Dca/DcaUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Contao\Controller;
use HeimrichHannot\UtilsBundle\Tests\AbstractUtilsTestCase;
use HeimrichHannot\UtilsBundle\Util\Dca\DcaUtil;
use HeimrichHannot\UtilsBundle\Util\Dca\GetDcaFieldsOptions;
use PHPUnit\Framework\MockObject\MockBuilder;

class DcaUtilTest extends AbstractUtilsTestCase
Expand Down Expand Up @@ -94,41 +95,51 @@ public function testGetDcaFields()
'title',
], $fields);

$fields = $instance->getDcaFields('table', ['allowedInputTypes' => ['select']]);
$fields = $instance->getDcaFields(
'table',
GetDcaFieldsOptions::create()->setAllowedInputTypes(['select'])
);
$this->assertSame([], $fields);

$fields = $instance->getDcaFields('table', ['localizeLabels' => true]);
$fields = $instance->getDcaFields(
'table',
GetDcaFieldsOptions::create()->setLocalizeLabels(true)
);
$this->assertSame([
'addSubmission' => 'Submission',
'title' => 'Title',
],
$fields);

$fields = $instance->getDcaFields('table', ['skipSorting' => true]);
$fields = $instance->getDcaFields(
'table',
GetDcaFieldsOptions::create()->setSkipSorting(true)
);
$this->assertSame([
'title',
'addSubmission',
], $fields);

$fields = $instance->getDcaFields('table', ['onlyDatabaseFields' => true]);
$fields = $instance->getDcaFields(
'table',
GetDcaFieldsOptions::create()->setOnlyDatabaseFields(true)
);
$this->assertSame([
'title',
], $fields);

$fields = $instance->getDcaFields('table', ['evalConditions' => ['mandatory' => true]]);
$fields = $instance->getDcaFields(
'table',
GetDcaFieldsOptions::create()->setEvalConditions(['mandatory' => true])
);
$this->assertSame([
'title',
], $fields);

$this->expectException(\Exception::class);
$instance->getDcaFields('table', ['allowedInputTypes' => 'checkbox']);
}

public function testGetDcaFieldsWithException()
{
$instance = $this->getTestInstance();

$this->expectException(\Exception::class);
$instance->getDcaFields('table', ['evalConditions' => 'mandatory']);
$instance->getDcaFields(
'table',
GetDcaFieldsOptions::create()->setAllowedInputTypes(['checkbox'])
);
}
}
4 changes: 2 additions & 2 deletions tests/Util/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace HeimrichHannot\UtilsBundle\Tests\Util;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\Util\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Util\Data\AnonymizeUtil;
use HeimrichHannot\UtilsBundle\Util\ContainerUtil;
use HeimrichHannot\UtilsBundle\Util\AnonymizeUtil;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\Dca\DcaUtil;
use HeimrichHannot\UtilsBundle\Util\Html\HtmlUtil;
Expand Down

0 comments on commit db317b1

Please sign in to comment.