Skip to content

Commit 2162c6a

Browse files
author
Michael Herold
committed
Restructures FormFields etc.
1 parent 3d0a386 commit 2162c6a

35 files changed

Lines changed: 617 additions & 568 deletions

src/Abstract_/Form.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ abstract class Form extends \hemio\html\Form {
2525
* @var array
2626
*/
2727
private $post = [];
28-
public $arrStoredValues = [];
28+
29+
/**
30+
*
31+
* @var array
32+
*/
33+
protected $storedValues = [];
2934

3035
public function __construct($name, array $post = null, array $get = null, array $stored = []) {
3136
if ($post === null)
@@ -37,30 +42,31 @@ public function __construct($name, array $post = null, array $get = null, array
3742
$this->post = $post;
3843
$this->get = $get;
3944
$this->name = $name;
45+
$this->storedValues = $stored;
4046

4147
$this->setAttribute('name', $this->getHtmlName());
4248
$this->setId($this->getHtmlName());
43-
$this->addInheritableAppendage('_INPUT_SINGLE_TEMPLATE', new template\FormLineP);
49+
$this->addInheritableAppendage(self::FORM_FIELD_TEMPLATE, new template\FormLineP);
4450
$this->addInheritableAppendage('_FORM', $this);
4551
}
4652

4753
/**
4854
* @todo potentially completly useless in this functions, form elements should have this options?
49-
* @return TemplateFormFieldSingle
55+
* @return TemplateFormField
5056
* @deprecated since version 1.0
5157
*/
5258
public function getLineTemplate() {
53-
return $this->getInheritableAppendage(self::SINGLE_CONTROL_TEMPLATE);
59+
return $this->getInheritableAppendage(self::FORM_FIELD_TEMPLATE);
5460
}
5561

5662
/**
57-
* @return TemplateFormFieldSingle
63+
* @return TemplateFormField
5864
*/
5965
public function getSingleControlTemplate() {
60-
return $this->getInheritableAppendage(self::SINGLE_CONTROL_TEMPLATE);
66+
return $this->getInheritableAppendage(self::FORM_FIELD_TEMPLATE);
6167
}
62-
63-
const SINGLE_CONTROL_TEMPLATE = '_INPUT_SINGLE_TEMPLATE';
68+
69+
const FORM_FIELD_TEMPLATE = '_INPUT_SINGLE_TEMPLATE';
6470

6571
/**
6672
* Check for occured errors
@@ -69,7 +75,7 @@ public function getSingleControlTemplate() {
6975
*/
7076
public function dataValid() {
7177
foreach (new \RecursiveIteratorIterator($this) as $child) {
72-
if ($child instanceof Abstract_\FormField && !$child->dataValid()) {
78+
if ($child instanceof Abstract_\FormElement && !$child->dataValid()) {
7379
return false;
7480
}
7581
}
@@ -114,8 +120,8 @@ public function getPost($key) {
114120
* @return mixed
115121
*/
116122
public function getValueStored($key) {
117-
if (isset($this->arrStoredValues[$key])) {
118-
return $this->arrStoredValues[$key];
123+
if (isset($this->storedValues[$key])) {
124+
return $this->storedValues[$key];
119125
} else {
120126
return null;
121127
}

src/Abstract_/FormElement.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
protected $name = '';
13+
14+
/**
15+
* Is active value in the form correct.
16+
*
17+
* @return boolean
18+
*/
19+
abstract public function dataValid();
20+
21+
/**
22+
* Has the value changed with respective to the stored or default value.
23+
* @return boolean
24+
*/
25+
abstract public function changed();
26+
27+
/**
28+
* Get the form to which this element belongs
29+
*
30+
* @return Abstract_\Form
31+
* @throws exception\NotLazyEnough
32+
*/
33+
public function getForm() {
34+
if ($this->existsInheritableAppendage('_FORM'))
35+
return $this->getInheritableAppendage('_FORM');
36+
else {
37+
#print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
38+
39+
throw new exception\NotLazyEnough(
40+
'No Form for FormElement found. Maybe not yet a child of a Form.');
41+
}
42+
}
43+
44+
/**
45+
*
46+
* @return string
47+
*/
48+
public function getName() {
49+
return $this->name;
50+
}
51+
52+
/**
53+
*
54+
* @param array $extraKeys
55+
* @return string
56+
*/
57+
public function getHtmlName(array $extraKeys = []) {
58+
return $this->getForm()->getHtmlName() .
59+
'_' . $this->getName() . implode('_', $extraKeys);
60+
}
61+
62+
}

src/Abstract_/FormField.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,25 @@
22

33
namespace hemio\form\Abstract_;
44

5-
use hemio\form\exception;
6-
75
/**
8-
*
6+
* Field in a form that expects inputs maybe composed of several input elements.
97
*/
10-
abstract class FormField extends \hemio\form\Container {
11-
12-
protected $name = '';
8+
abstract class FormField extends FormElement {
139

1410
/**
15-
* Is active value in the form correct.
1611
*
17-
* @return boolean
12+
* @todo not implemented
1813
*/
19-
abstract public function dataValid();
20-
21-
/**
22-
* Has the value changed with respective to the stored or default value.
23-
* @return boolean
24-
*/
25-
abstract public function changed();
14+
public function changed() {
15+
return false;
16+
}
2617

2718
/**
28-
* Get the form to which this element belongs
2919
*
30-
* @return Abstract_\Form
31-
* @throws exception\NotLazyEnough
20+
* @todo not implemented
3221
*/
33-
public function getForm() {
34-
if ($this->existsInheritableAppendage('_FORM'))
35-
return $this->getInheritableAppendage('_FORM');
36-
else {
37-
#print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
38-
39-
throw new exception\NotLazyEnough(
40-
'No Form for FormElement found. Maybe not yet a child of a Form.');
41-
}
22+
public function dataValid() {
23+
return true;
4224
}
4325

4426
}

src/Abstract_/FormFieldDefault.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace hemio\form\Abstract_;
4+
5+
use hemio\form as form_;
6+
use hemio\form\exception;
7+
use hemio\html;
8+
9+
/**
10+
*
11+
*
12+
*/
13+
abstract class FormFieldDefault extends FormField {
14+
15+
use form_\Trait_\MaintainsFilters;
16+
17+
/**
18+
*
19+
* @var string
20+
*/
21+
public $title = '';
22+
23+
/**
24+
*
25+
* @var boolean
26+
*/
27+
protected $filled = false;
28+
29+
/**
30+
*
31+
* @var html\Interface_\Submittable
32+
*/
33+
protected $control;
34+
35+
/**
36+
*
37+
* @var string
38+
*/
39+
protected $defaultValue = '';
40+
41+
/**
42+
*
43+
* @param mixed $value
44+
*/
45+
public function setDefaultValue($value) {
46+
$this->defaultValue = $value;
47+
}
48+
49+
/**
50+
* @return mixed Default value
51+
*/
52+
public function getValueDefault() {
53+
return $this->getFiltered($this->defaultValue);
54+
}
55+
56+
/**
57+
*
58+
* @param string $name
59+
* @param string $title
60+
* @param html\Interface_\Submittable $control
61+
*/
62+
public function init($name, $title, $control) {
63+
$this->name = $name;
64+
$this->title = $title;
65+
$this->control = $control;
66+
}
67+
68+
public function getValueToUse() {
69+
if ($this->getValueUser() !== null)
70+
return $this->getValueUser();
71+
else if ($this->getValueStored() !== null)
72+
return $this->getValueStored();
73+
else
74+
return $this->getValueDefault();
75+
}
76+
77+
public function getValueUser() {
78+
$value = $this->getForm()->getValueUser($this->getHtmlName());
79+
if ($value === null)
80+
return null;
81+
else
82+
return $this->getFiltered($value);
83+
}
84+
85+
public function getValueStored() {
86+
return $this->getFiltered(
87+
$this->getForm()->getValueStored($this->getName()));
88+
}
89+
90+
public function changed() {
91+
return $this->getValueStored() != $this->getValueUser();
92+
}
93+
94+
/**
95+
*
96+
* @return TemplateFormField
97+
* @throws exception\NotLazyEnough
98+
* @throws exception\AppendageTypeError
99+
*/
100+
public function getFieldTemplateClone($special = null) {
101+
102+
$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE . '_' . $special;
103+
104+
if (!$this->existsInheritableAppendage($appendageName)) {
105+
$appendageName = form_\FormPost::FORM_FIELD_TEMPLATE;
106+
}
107+
108+
$template = $this->getInheritableAppendage($appendageName);
109+
110+
if ($template instanceof TemplateFormField) {
111+
return clone $template;
112+
} elseif ($template === null) {
113+
throw new exception\NotLazyEnough(
114+
sprintf(
115+
'There is no "%s" available for this Input', $appendageName
116+
)
117+
);
118+
} else {
119+
throw new exception\AppendageTypeError(
120+
sprintf(
121+
'Not an istance of TemplateFormFieldSingle "%s"', $appendageName
122+
)
123+
);
124+
}
125+
}
126+
127+
public function describe() {
128+
return 'INPUT';
129+
}
130+
131+
/**
132+
*
133+
* @return string
134+
*/
135+
public function __toString() {
136+
if (!$this->filled)
137+
$this->fill();
138+
139+
return parent::__toString();
140+
}
141+
142+
abstract public function fill();
143+
}

src/Abstract_/FormFieldIn.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)