CustomField   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 3 1
A html() 0 14 4
A getFieldTemplate() 0 10 2
A __construct() 0 5 1
1
<?php
2
namespace Mezon\Gui\Field;
3
4
use Mezon\TemplateEngine\TemplateEngine;
5
use Mezon\Gui\Field;
6
7
/**
8
 * Class CustomField
9
 *
10
 * @package Field
11
 * @subpackage CustomField
12
 * @author Dodonov A.A.
13
 * @version v.1.0 (2019/09/13)
14
 * @copyright Copyright (c) 2019, http://aeon.su
15
 */
16
17
/**
18
 * Custom field control
19
 */
20
class CustomField extends Field
21
{
22
23
    /**
24
     * Custom field's parts
25
     *
26
     * @var array
27
     */
28
    protected $fields = [];
29
30
    /**
31
     * Constructor
32
     *
33
     * @param array $fieldDescription
34
     *            Field description
35
     * @param string $value
36
     *            Field value
37
     */
38
    public function __construct(array $fieldDescription, string $value = '')
39
    {
40
        parent::__construct($fieldDescription, $value);
41
42
        $this->fields = $fieldDescription['fields'];
43
    }
44
45
    /**
46
     * Method returns field's template
47
     *
48
     * @return string field's template
49
     */
50
    protected function getFieldTemplate(): string
51
    {
52
        // @codeCoverageIgnoreStart
53
        $content = file_get_contents('./res/templates/field-' . $this->name . '.tpl');
54
55
        if ($content === false) {
56
            throw (new \Exception('Template field-' . $this->name . '.tpl was not found'));
57
        }
58
59
        return $content;
60
        // @codeCoverageIgnoreEnd
61
    }
62
63
    /**
64
     * Generating custom feld
65
     *
66
     * @return string HTML representation of the custom field
67
     */
68
    public function html(): string
69
    {
70
        return TemplateEngine::printRecord(
71
            $this->getFieldTemplate(),
72
            [
73
                'name' => $this->name,
74
                'name-prefix' => $this->namePrefix,
75
                'disabled' => $this->disabled ? 1 : 0,
76
                'batch' => $this->batch ? 1 : 0,
77
                'custom' => $this->custom,
78
                'required' => $this->required ? 1 : 0,
79
                'toggler' => $this->toggler,
80
                'toggle-value' => $this->toggleValue,
81
                'class' => $this->class
82
            ]);
83
    }
84
85
    /**
86
     * Method returns parts of the custom field
87
     *
88
     * @return array parts of the custom field
89
     */
90
    public function getFields(): array
91
    {
92
        return $this->fields;
93
    }
94
}
95