FieldDefinitionTrait::defineFieldDefinitions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jidaikobo\Kontiki\Models\BaseModelTraits;
4
5
/**
6
 * Trait FieldDefinitionTrait
7
 *
8
 * Provides methods for defining and managing field definitions in models.
9
 */
10
trait FieldDefinitionTrait
11
{
12
    protected ?array $fieldDefinitions = null;
13
    protected ?array $metaDataFieldDefinitions = null;
14
15
    public function getFields(
16
        string $context = '',
17
        array $data = [],
18
        int $id = null
19
    ): array {
20
        if (!empty($context)) {
21
            $this->processFields($context, $data, $id);
22
        }
23
24
        return array_merge($this->fieldDefinitions, $this->metaDataFieldDefinitions);
0 ignored issues
show
Bug introduced by
It seems like $this->fieldDefinitions can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        return array_merge(/** @scrutinizer ignore-type */ $this->fieldDefinitions, $this->metaDataFieldDefinitions);
Loading history...
25
    }
26
27
    public function getFieldDefinitions(): array
28
    {
29
        return $this->fieldDefinitions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fieldDefinitions could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
30
    }
31
32
    public function getMetaDataFieldDefinitions(): array
33
    {
34
        return $this->metaDataFieldDefinitions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->metaDataFieldDefinitions could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
35
    }
36
37
    protected function defineFieldDefinitions(): void
38
    {
39
        $this->fieldDefinitions = [];
40
    }
41
42
    protected function defineMetaDataFieldDefinitions(): void
43
    {
44
        $this->metaDataFieldDefinitions = [];
45
    }
46
47
    protected function processFieldDefinitions(
48
        string $context = '',
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
        /** @scrutinizer ignore-unused */ string $context = '',

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
        array $data = [],
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
        /** @scrutinizer ignore-unused */ array $data = [],

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
        int $id = null
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
        /** @scrutinizer ignore-unused */ int $id = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    ): void {
52
        return;
53
    }
54
55
    protected function disableFormFieldsForContext(): void
56
    {
57
        foreach (['metaDataFieldDefinitions', 'fieldDefinitions'] as $fieldType) {
58
            foreach ($this->$fieldType as &$field) {
59
                // Set input fields to readonly
60
                $field['attributes']['readonly'] = 'readonly';
61
62
                // Remove file upload class and add "form-control-plaintext"
63
                $existingClass = $field['attributes']['class'] ?? '';
64
                $existingClass = str_replace('kontiki-file-upload', '', $existingClass);
65
                $field['attributes']['class'] = trim(
66
                    $existingClass . ' form-control-plaintext p-2'
67
                );
68
69
                // Remove descriptions
70
                $field['description'] = '';
71
            }
72
        }
73
    }
74
75
    private function fillValueFieldDefinitions(
76
        string $context = '',
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
        /** @scrutinizer ignore-unused */ string $context = '',

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
        array $data = [],
78
        int $id = null
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
        /** @scrutinizer ignore-unused */ int $id = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    ): void {
80
        foreach (['metaDataFieldDefinitions', 'fieldDefinitions'] as $fieldType) {
81
            foreach (array_keys($this->$fieldType) as $fieldName) {
82
                if (isset($data[$fieldName])) {
83
                    $this->$fieldType[$fieldName]['default'] = $data[$fieldName];
84
                }
85
            }
86
        }
87
    }
88
89
    private function initializeFields(): void
90
    {
91
        if ($this->fieldDefinitions === null) {
92
            $this->defineFieldDefinitions();
93
        }
94
    }
95
96
    private function initializeMetaDataFields(): void
97
    {
98
        if ($this->metaDataFieldDefinitions === null) {
99
            $this->defineMetaDataFieldDefinitions();
100
        }
101
    }
102
103
    private function processFields(
104
        string $context = '',
105
        array $data = [],
106
        int $id = null
107
    ): void {
108
        // set values for save / form
109
        $this->fillValueFieldDefinitions($context, $data, $id);
110
111
        // process definitions for various purpose
112
        $this->processFieldDefinitions($context, $data, $id);
113
    }
114
115
    /**
116
     * Get the definition for Id field.
117
     *
118
     * wrapper of read-only field.
119
     */
120
    protected function getIdField(): array
121
    {
122
        return $this->getReadOnlyField(
123
            __('ID'),
124
            [
125
                'display_in_list' => true
126
            ]
127
        );
128
    }
129
130
    /**
131
     * Get the definition for a read-only field.
132
     *
133
     * This method defines the structure of a field that is displayed in lists
134
     * but not included in form inputs.
135
     *
136
     * @param string $label The label to be used for the field.
137
     * @param array $options Optional settings for the field.
138
     * @return array The read-only field definition.
139
     */
140
    protected function getReadOnlyField(string $label, array $options = []): array
141
    {
142
        return [
143
            'label' => $label,
144
            'display_in_list' => $options['display_in_list'] ?? false,
145
            'save_as_utc' => $options['save_as_utc'] ?? false,
146
        ];
147
    }
148
149
    /**
150
     * Generate a text field definition.
151
     *
152
     * @param string $label The field name.
153
     * @param array $options Optional settings for the field.
154
     * @return array Field definition.
155
     */
156
    protected function getField(string $label, array $options = []): array
157
    {
158
        return [
159
            'label' => $options['label'] ?? __($label),
160
            'type' => $options['type'] ?? 'text',
161
            'description' => $options['description'] ?? '',
162
            'attributes' => $options['attributes'] ?? ['class' => 'form-control'],
163
            'label_attributes' => $options['label_attributes'] ?? ['class' => 'form-label'],
164
            'options' => $options['options'] ?? [],
165
            'default' => $options['default'] ?? '',
166
            'searchable' => $options['searchable'] ?? true,
167
            'rules' => $options['rules'] ?? [],
168
            'filter' => $options['filter'] ?? (defined('FILTER_SANITIZE_FULL_SPECIAL_CHARS')
169
                ? FILTER_SANITIZE_FULL_SPECIAL_CHARS
0 ignored issues
show
Bug introduced by
The constant Jidaikobo\Kontiki\Models...TIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
170
                : FILTER_SANITIZE_SPECIAL_CHARS),
171
            'template' => $options['template'] ?? 'default',
172
            'group' => $options['group'] ?? 'main',
173
            'fieldset_template' => $options['fieldset_template'] ?? 'forms/fieldset/flat.php',
174
            'display_in_list' => $options['display_in_list'] ?? false,
175
            'save_as_utc' => $options['save_as_utc'] ?? false
176
        ];
177
    }
178
}
179