-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGroup.php
More file actions
96 lines (77 loc) · 1.93 KB
/
Group.php
File metadata and controls
96 lines (77 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Gregwar\Formidable\Fields;
use Gregwar\Formidable\Language\Language;
/**
* A group handles the [] entries of an array
*/
class Group extends Field
{
/**
* Childs
*/
protected $childs = array();
/**
* A bad value was passed
*/
protected $badValue = false;
public function addChild(Field $child)
{
$this->childs[] = $child;
}
public function check()
{
if ($this->badValue) {
return array('bad_array_value', $this->printName());
}
foreach ($this->childs as $child) {
if ($error = $child->check()) {
return $error;
}
}
}
public function setValue($value, $default = false)
{
$this->badValue = false;
if (!$value) {
return;
}
if (!is_array($value)) {
$this->badValue = true;
return;
}
$position = 0;
foreach ($this->childs as $child) {
if ($child instanceof CheckboxField) {
if (in_array($child->getCheckedValue(), $value)) {
$child->setChecked(true);
}
} else {
if ($position < count($value)) {
$child->setValue($value[$position++]);
}
}
}
}
public function getValue()
{
$value = array();
foreach ($this->childs as $child) {
$fieldValue = $child->getValue();
if ($fieldValue != '') {
$value[] = $fieldValue;
}
}
return $value;
}
public function setLanguage(Language $language)
{
parent::setLanguage($language);
foreach ($this->childs as $child) {
$child->setLanguage($language);
}
}
public function __sleep()
{
return array_merge(parent::__sleep(), array('childs'));
}
}