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 11<?php
2- namespace PHPForm \Fields ;
2+ namespace PHPForm \Bounds ;
33
44use PHPForm \Config ;
55
@@ -10,7 +10,7 @@ class BoundField
1010 private $ form ;
1111 private $ field ;
1212 private $ name ;
13- private $ options_cache ;
13+ private $ bound_widgets_cache ;
1414
1515 public $ html_name ;
1616 public $ help_text ;
@@ -55,18 +55,25 @@ public function __get($name)
5555 }
5656
5757 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 ();
6060 }
61- return $ options_cache ;
61+ return $ bound_widgets_cache ;
6262 }
6363 }
6464
65- private function getOptions (array $ attrs = array ())
65+ private function getSubWidgets (array $ attrs = array ())
6666 {
67+ $ bounds = [];
68+
6769 $ 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+ }
6875
69- return $ this -> field -> getWidget ()-> getOptions ( $ this -> html_name , $ this -> getValue (), $ attrs ) ;
76+ return $ bounds ;
7077 }
7178
7279 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
3737 */
3838 public function setTemplatePack (string $ template_pack )
3939 {
40- $ this -> template_packs = array_unshift ($ this ->template_packs , $ template_pack );
40+ array_unshift ($ this ->template_packs , $ template_pack );
4141 }
4242
4343 /**
Original file line number Diff line number Diff line change 77use Iterator ;
88use UnexpectedValueException ;
99
10+ use PHPForm \Bounds \BoundField ;
1011use PHPForm \Errors \ErrorList ;
1112use PHPForm \Exceptions \ValidationError ;
12- use PHPForm \Fields \BoundField ;
1313
1414abstract class Form implements ArrayAccess, Iterator, Countable
1515{
Original file line number Diff line number Diff line change @@ -32,10 +32,6 @@ public function formatValue($value)
3232 */
3333 public function valueFromData ($ data , $ files , string $ name )
3434 {
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 ;
4036 }
4137}
Original file line number Diff line number Diff line change @@ -94,7 +94,7 @@ protected function formatValue($value)
9494 */
9595 public function valueFromData ($ data , $ files , string $ name )
9696 {
97- return array_key_exists ($ name , $ data ) ? $ data [$ name ] : null ;
97+ return ! is_null ( $ data ) && array_key_exists ($ name , $ data ) ? $ data [$ name ] : null ;
9898 }
9999
100100 /**
Original file line number Diff line number Diff line change 11<?php
2- namespace PHPForm \Unit \Fields ;
2+ namespace PHPForm \Unit \Bounds ;
33
44use PHPUnit \Framework \TestCase ;
55
6+ use PHPForm \Bounds \BoundField ;
7+ use PHPForm \Bounds \BoundWidget ;
68use PHPForm \Errors \ErrorList ;
79use PHPForm \Exceptions \ValidationError ;
8- use PHPForm \Fields \BoundField ;
910use PHPForm \Fields \CharField ;
1011use PHPForm \Fields \ChoiceField ;
1112use PHPForm \Widgets \RadioSelect ;
@@ -214,16 +215,6 @@ public function testOptions()
214215 $ field = new ChoiceField (["choices " => array ("option1 " => "Option1 " )]);
215216 $ bound = new BoundField ($ this ->simple_form , $ field , "name " );
216217
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 ]);
228219 }
229220}
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 44use PHPUnit \Framework \TestCase ;
55
66use PHPForm \Forms \Form ;
7- use PHPForm \Fields \BoundField ;
7+ use PHPForm \Bounds \BoundField ;
88
99class FormTest extends TestCase
1010{
Original file line number Diff line number Diff line change @@ -39,4 +39,10 @@ public function testValueFromDataWithData()
3939 $ value = $ widget ->valueFromData (array (), array ("name " => "file " ), "name " );
4040 $ this ->assertEquals ($ value , "file " );
4141 }
42+
43+ public function testValueFromDataWithNullData ()
44+ {
45+ $ widget = new FileInput ();
46+ $ this ->assertNull ($ widget ->valueFromData (null , null , "name " ));
47+ }
4248}
You can’t perform that action at this time.
0 commit comments