chg: [behaviors:metafields] Moved type handlers to the meta-template-fields table

pull/93/head
Sami Mokaddem 2022-03-01 09:49:33 +01:00
parent ad6362eed4
commit 4b5b2bc7e2
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 38 additions and 13 deletions

View File

@ -65,7 +65,8 @@ class IPv4Type extends TextType
protected function fetchAllMetatemplateFieldsIdForThisType(\App\Model\Entity\MetaTemplateField $metaTemplateField = null): Query
{
$conditions =[];
$this->MetaTemplateFields = TableRegistry::getTableLocator()->get('MetaTemplateFields');
$conditions = [];
if (!is_null($metaTemplateField)) {
$conditions['id'] = $metaTemplateField->id;
} else {

View File

@ -12,7 +12,6 @@ class TextType
public function __construct()
{
$this->MetaFields = TableRegistry::getTableLocator()->get('MetaFields');
$this->MetaTemplateFields = TableRegistry::getTableLocator()->get('MetaTemplateFields');
}
public function validate(string $value): bool

View File

@ -55,20 +55,15 @@ class MetaFieldsBehavior extends Behavior
public function initialize(array $config): void
{
$this->bindAssociations();
$this->_metaTemplateFieldTable = $this->_table;
$this->_metaTemplateTable = $this->_table;
$this->_metaTemplateFieldTable = $this->_table->MetaFields->MetaTemplateFields;
$this->_metaTemplateTable = $this->_table->MetaFields->MetaTemplates;
$this->loadTypeHandlers();
}
private function loadTypeHandlers()
{
$typeHandlers = [
new TextType(),
new IPv4Type(),
new IPv6Type(),
];
foreach ($typeHandlers as $handler) {
$this->typeHandlers[$handler::TYPE] = $handler;
if (empty($this->typeHandlers)) {
$this->typeHandlers = $this->_metaTemplateFieldTable->getTypeHandlers();
}
}
@ -238,8 +233,7 @@ class MetaFieldsBehavior extends Behavior
protected function buildQuerySnippet(array $filter): Query
{
$this->MetaTemplateFields = TableRegistry::getTableLocator()->get('MetaTemplateFields');
$metaTemplateField = !empty($filter['meta_template_field_id']) ? $this->MetaTemplateFields->get($filter['meta_template_field_id']) : null;
$metaTemplateField = !empty($filter['meta_template_field_id']) ? $this->_metaTemplateFieldTable->get($filter['meta_template_field_id']) : null;
$whereClosure = function (QueryExpression $exp) use ($filter, $metaTemplateField) {
foreach ($filter as $column => $value) {
$keyedColumn = 'MetaFields.' . $column;

View File

@ -6,8 +6,14 @@ use App\Model\Table\AppTable;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use MetaFieldsTypes\TextType;
use MetaFieldsTypes\IPv4Type;
use MetaFieldsTypes\IPv6Type;
class MetaTemplateFieldsTable extends AppTable
{
private $typeHandlers = [];
public function initialize(array $config): void
{
parent::initialize($config);
@ -18,6 +24,7 @@ class MetaTemplateFieldsTable extends AppTable
$this->hasMany('MetaFields');
$this->setDisplayField('field');
$this->loadTypeHandlers();
}
public function beforeSave($event, $entity, $options)
@ -37,4 +44,28 @@ class MetaTemplateFieldsTable extends AppTable
->requirePresence(['field', 'type'], 'create');
return $validator;
}
public function loadTypeHandlers(): void
{
if (empty($this->typeHandlers)) {
$typeHandlers = [
new TextType(),
new IPv4Type(),
new IPv6Type(),
];
foreach ($typeHandlers as $handler) {
$this->typeHandlers[$handler::TYPE] = $handler;
}
}
}
public function getTypeHandlers(): array
{
return $this->typeHandlers;
}
public function getTypeHandler($type)
{
return $this->typeHandlers[$type] ?? false;
}
}