File tree Expand file tree Collapse file tree 10 files changed +110
-29
lines changed Expand file tree Collapse file tree 10 files changed +110
-29
lines changed Original file line number Diff line number Diff line change 1
1
<?php
2
- namespace PHPForm \Fields ;
2
+ namespace PHPForm \Bounds ;
3
3
4
4
use PHPForm \Config ;
5
5
@@ -10,7 +10,7 @@ class BoundField
10
10
private $ form ;
11
11
private $ field ;
12
12
private $ name ;
13
- private $ options_cache ;
13
+ private $ bound_widgets_cache ;
14
14
15
15
public $ html_name ;
16
16
public $ help_text ;
@@ -55,18 +55,25 @@ public function __get($name)
55
55
}
56
56
57
57
if ($ name == 'options ' ) {
58
- if (!isset ($ options_cache )) {
59
- $ options_cache = $ this ->getOptions ();
58
+ if (!isset ($ bound_widgets_cache )) {
59
+ $ bound_widgets_cache = $ this ->getSubWidgets ();
60
60
}
61
- return $ options_cache ;
61
+ return $ bound_widgets_cache ;
62
62
}
63
63
}
64
64
65
- private function getOptions (array $ attrs = array ())
65
+ private function getSubWidgets (array $ attrs = array ())
66
66
{
67
+ $ bounds = [];
68
+
67
69
$ attrs = $ this ->buildWidgetAttrs ($ attrs );
70
+ $ options = $ this ->field ->getWidget ()->getOptions ($ this ->html_name , $ this ->getValue (), $ attrs );
71
+
72
+ foreach ($ options as $ option ) {
73
+ $ bounds [] = new BoundWidget ($ option );
74
+ }
68
75
69
- return $ this -> field -> getWidget ()-> getOptions ( $ this -> html_name , $ this -> getValue (), $ attrs ) ;
76
+ return $ bounds ;
70
77
}
71
78
72
79
protected function asWidget ($ widget = null , array $ attrs = array ())
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace PHPForm \Bounds ;
3
+
4
+ use PHPForm \Config ;
5
+
6
+ class BoundWidget
7
+ {
8
+ private $ data ;
9
+ private $ template ;
10
+
11
+ public $ for ;
12
+ public $ type ;
13
+ public $ name ;
14
+ public $ value ;
15
+ public $ label ;
16
+
17
+ public function __construct (array $ data )
18
+ {
19
+ $ this ->for = $ data ["for " ];
20
+ $ this ->type = $ data ["type " ];
21
+ $ this ->name = $ data ["name " ];
22
+ $ this ->value = $ data ["value " ];
23
+ $ this ->label = $ data ["label " ];
24
+ $ this ->template = $ data ["template " ];
25
+
26
+ unset($ data ["template " ]);
27
+
28
+ $ this ->data = $ data ;
29
+ }
30
+
31
+ public function __toString ()
32
+ {
33
+ $ renderer = Config::getInstance ()->getRenderer ();
34
+
35
+ return $ renderer ->render ($ this ->template , $ this ->data );
36
+ }
37
+ }
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ class Config extends Singleton
37
37
*/
38
38
public function setTemplatePack (string $ template_pack )
39
39
{
40
- $ this -> template_packs = array_unshift ($ this ->template_packs , $ template_pack );
40
+ array_unshift ($ this ->template_packs , $ template_pack );
41
41
}
42
42
43
43
/**
Original file line number Diff line number Diff line change 7
7
use Iterator ;
8
8
use UnexpectedValueException ;
9
9
10
+ use PHPForm \Bounds \BoundField ;
10
11
use PHPForm \Errors \ErrorList ;
11
12
use PHPForm \Exceptions \ValidationError ;
12
- use PHPForm \Fields \BoundField ;
13
13
14
14
abstract class Form implements ArrayAccess, Iterator, Countable
15
15
{
Original file line number Diff line number Diff line change @@ -32,10 +32,6 @@ public function formatValue($value)
32
32
*/
33
33
public function valueFromData ($ data , $ files , string $ name )
34
34
{
35
- if (array_key_exists ($ name , $ files )) {
36
- return $ files [$ name ];
37
- }
38
-
39
- return null ;
35
+ return !is_null ($ files ) && array_key_exists ($ name , $ files ) ? $ files [$ name ] : null ;
40
36
}
41
37
}
Original file line number Diff line number Diff line change @@ -94,7 +94,7 @@ protected function formatValue($value)
94
94
*/
95
95
public function valueFromData ($ data , $ files , string $ name )
96
96
{
97
- return array_key_exists ($ name , $ data ) ? $ data [$ name ] : null ;
97
+ return ! is_null ( $ data ) && array_key_exists ($ name , $ data ) ? $ data [$ name ] : null ;
98
98
}
99
99
100
100
/**
Original file line number Diff line number Diff line change 1
1
<?php
2
- namespace PHPForm \Unit \Fields ;
2
+ namespace PHPForm \Unit \Bounds ;
3
3
4
4
use PHPUnit \Framework \TestCase ;
5
5
6
+ use PHPForm \Bounds \BoundField ;
7
+ use PHPForm \Bounds \BoundWidget ;
6
8
use PHPForm \Errors \ErrorList ;
7
9
use PHPForm \Exceptions \ValidationError ;
8
- use PHPForm \Fields \BoundField ;
9
10
use PHPForm \Fields \CharField ;
10
11
use PHPForm \Fields \ChoiceField ;
11
12
use PHPForm \Widgets \RadioSelect ;
@@ -214,16 +215,6 @@ public function testOptions()
214
215
$ field = new ChoiceField (["choices " => array ("option1 " => "Option1 " )]);
215
216
$ bound = new BoundField ($ this ->simple_form , $ field , "name " );
216
217
217
- $ expected = array (array (
218
- "for " => "id_name_1 " ,
219
- "type " => null ,
220
- "name " => "name " ,
221
- "value " => "option1 " ,
222
- "label " => "Option1 " ,
223
- "attrs " => array (),
224
- "template " => "select_option.html "
225
- ));
226
-
227
- $ this ->assertEquals ($ expected , $ bound ->options );
218
+ $ this ->assertInstanceOf (BoundWidget::class, $ bound ->options [0 ]);
228
219
}
229
220
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace PHPForm \Unit \Bounds ;
3
+
4
+ use PHPUnit \Framework \TestCase ;
5
+
6
+ use PHPForm \Bounds \BoundWidget ;
7
+ use PHPForm \Widgets \RadioSelect ;
8
+
9
+ class BoundWidgetTest extends TestCase
10
+ {
11
+
12
+ protected function setUp ()
13
+ {
14
+ $ this ->data = array (
15
+ "for " => "for " ,
16
+ "type " => "type " ,
17
+ "name " => "name " ,
18
+ "value " => "value " ,
19
+ "label " => "label " ,
20
+ "attrs " => array (),
21
+ "template " => RadioSelect::TEMPLATE_CHOICE ,
22
+ );
23
+ }
24
+
25
+ public function testConstruct ()
26
+ {
27
+ $ bound = new BoundWidget ($ this ->data );
28
+
29
+ $ this ->assertAttributeEquals ("for " , "for " , $ bound );
30
+ $ this ->assertAttributeEquals ("type " , "type " , $ bound );
31
+ $ this ->assertAttributeEquals ("name " , "name " , $ bound );
32
+ $ this ->assertAttributeEquals ("value " , "value " , $ bound );
33
+ $ this ->assertAttributeEquals ("label " , "label " , $ bound );
34
+ $ this ->assertAttributeEquals ("radio_select_option.html " , "template " , $ bound );
35
+ }
36
+
37
+ public function testToString ()
38
+ {
39
+ $ bound = new BoundWidget ($ this ->data );
40
+
41
+ $ expected = '<label for="for"><input name="name" type="type" value="value"/> label</label> ' ;
42
+ $ this ->assertXmlStringEqualsXmlString ((string ) $ bound , $ expected );
43
+ }
44
+ }
Original file line number Diff line number Diff line change 4
4
use PHPUnit \Framework \TestCase ;
5
5
6
6
use PHPForm \Forms \Form ;
7
- use PHPForm \Fields \BoundField ;
7
+ use PHPForm \Bounds \BoundField ;
8
8
9
9
class FormTest extends TestCase
10
10
{
Original file line number Diff line number Diff line change @@ -39,4 +39,10 @@ public function testValueFromDataWithData()
39
39
$ value = $ widget ->valueFromData (array (), array ("name " => "file " ), "name " );
40
40
$ this ->assertEquals ($ value , "file " );
41
41
}
42
+
43
+ public function testValueFromDataWithNullData ()
44
+ {
45
+ $ widget = new FileInput ();
46
+ $ this ->assertNull ($ widget ->valueFromData (null , null , "name " ));
47
+ }
42
48
}
You can’t perform that action at this time.
0 commit comments