FormElement   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 72
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
dataValid() 0 1 ?
A getForm() 0 9 2
A getName() 0 3 1
A getHtmlName() 0 7 1
A getHtmlId() 0 8 2
A getValueUser() 0 5 1
1
<?php
2
3
namespace hemio\form\Abstract_;
4
5
use hemio\form\exception;
6
7
/**
8
 *
9
 */
10
abstract class FormElement extends \hemio\form\Container {
11
12
    use \hemio\form\Trait_\MaintainsTransformations;
13
14
    protected $name = '';
15
    protected $idSuffix = null;
16
17
    /**
18
     * Is active value in the form correct.
19
     *
20
     * @return boolean
21
     */
22
    abstract public function dataValid();
23
24
    /**
25
     * Get the form to which this element belongs
26
     *
27
     * @return Abstract_\Form
28
     * @throws exception\NotLazyEnough
29
     */
30
    public function getForm() {
31
        if ($this->existsInheritableAppendage('_FORM'))
32
            return $this->getInheritableAppendage('_FORM');
33
        else {
34
            throw new exception\NotLazyEnough(
35
            'No Form for FormElement found. Maybe not yet a child of a Form.'
36
            );
37
        }
38
    }
39
40
    /**
41
     *
42
     * @return string
43
     */
44
    public function getName() {
45
        return $this->name;
46
    }
47
48
    /**
49
     *
50
     * @param array $extraKeys
51
     * @return string
52
     */
53
    public function getHtmlName(array $extraKeys = []) {
54
        return sprintf('%s_%s%s'
55
                , $this->getForm()->getHtmlName()
56
                , $this->getName()
57
                , implode('_', $extraKeys)
58
        );
59
    }
60
61
    /**
62
     *
63
     * @param array $extraKeys
64
     * @return string
65
     */
66
    public function getHtmlId(array $extraKeys = []) {
67
        return sprintf('%s_%s%s'
68
                , $this->getForm()->getHtmlName()
69
                , $this->getName()
70
                , $this->idSuffix === null ? '' : '_' . $this->idSuffix
71
                , implode('_', $extraKeys)
72
        );
73
    }
74
75
    public function getValueUser() {
76
        return $this->withTransformations(
77
                        $this->getForm()->getValueUser($this->getHtmlName())
78
        );
79
    }
80
81
}
82