1010
1111namespace Kdyby \Replicator ;
1212
13- use Nette ;
13+ use ArrayIterator ;
14+ use Nette \Application \Request ;
15+ use Nette \Application \UI \Form ;
16+ use Nette \Application \UI \Presenter ;
17+ use Nette \ComponentModel \IComponent ;
18+ use Nette \ComponentModel \IContainer ;
19+ use Nette \Forms \Container as NetteFormsContainer ;
20+ use Nette \Forms \ControlGroup ;
21+ use Nette \Forms \Controls \BaseControl ;
1422use Nette \Forms \Controls \SubmitButton ;
23+ use Nette \Http \IRequest ;
24+ use Nette \InvalidArgumentException ;
25+ use Nette \MemberAccessException ;
26+ use Nette \Reflection \ClassType ;
27+ use Nette \Utils \Arrays ;
1528use Nette \Utils \Callback ;
16-
29+ use Nette \Utils \ObjectMixin ;
30+ use SplObjectStorage ;
31+ use Traversable ;
1732
1833
1934/**
2035 * @author Filip Procházka <filip@prochazka.su>
2136 * @author Jan Tvrdík
2237 *
23- * @method \Nette\Application\UI\ Form getForm()
24- * @property \Nette\Forms\Container $parent
38+ * @method Form getForm()
39+ * @property NetteFormsContainer $parent
2540 */
26- class Container extends Nette \ Forms \Container
41+ class Container extends NetteFormsContainer
2742{
2843
2944 /** @var bool */
@@ -44,7 +59,7 @@ class Container extends Nette\Forms\Container
4459 /** @var array */
4560 private $ created = [];
4661
47- /** @var \Nette\Http\ IRequest */
62+ /** @var IRequest */
4863 private $ httpRequest ;
4964
5065 /** @var array */
@@ -57,7 +72,7 @@ class Container extends Nette\Forms\Container
5772 * @param int $createDefault
5873 * @param bool $forceDefault
5974 *
60- * @throws \Nette\ InvalidArgumentException
75+ * @throws InvalidArgumentException
6176 */
6277 public function __construct ($ factory , $ createDefault = 0 , $ forceDefault = FALSE )
6378 {
@@ -67,9 +82,9 @@ public function __construct($factory, $createDefault = 0, $forceDefault = FALSE)
6782
6883 try {
6984 $ this ->factoryCallback = Callback::closure ($ factory );
70- } catch (Nette \ InvalidArgumentException $ e ) {
85+ } catch (InvalidArgumentException $ e ) {
7186 $ type = is_object ($ factory ) ? 'instanceof ' . get_class ($ factory ) : gettype ($ factory );
72- throw new Nette \ InvalidArgumentException (
87+ throw new InvalidArgumentException (
7388 'Replicator requires callable factory, ' . $ type . ' given. ' , 0 , $ e
7489 );
7590 }
@@ -93,16 +108,16 @@ public function setFactory($factory)
93108 /**
94109 * Magical component factory
95110 *
96- * @param \Nette\ComponentModel\ IContainer
111+ * @param IContainer
97112 */
98113 protected function attached ($ obj )
99114 {
100115 parent ::attached ($ obj );
101116
102117 if (
103- !$ obj instanceof Nette \ Application \ UI \ Presenter
118+ !$ obj instanceof Presenter
104119 &&
105- $ this ->form instanceof Nette \ Application \ UI \ Form
120+ $ this ->form instanceof Form
106121 ) {
107122 return ;
108123 }
@@ -115,7 +130,8 @@ protected function attached($obj)
115130
116131 /**
117132 * @param boolean $recursive
118- * @return \ArrayIterator|\Nette\Forms\Container[]
133+ *
134+ * @return ArrayIterator|NetteFormsContainer[]
119135 */
120136 public function getContainers ($ recursive = FALSE )
121137 {
@@ -126,7 +142,8 @@ public function getContainers($recursive = FALSE)
126142
127143 /**
128144 * @param boolean $recursive
129- * @return \ArrayIterator|Nette\Forms\Controls\SubmitButton[]
145+ *
146+ * @return ArrayIterator|SubmitButton[]
130147 */
131148 public function getButtons ($ recursive = FALSE )
132149 {
@@ -139,7 +156,8 @@ public function getButtons($recursive = FALSE)
139156 * Magical component factory
140157 *
141158 * @param string $name
142- * @return \Nette\Forms\Container
159+ *
160+ * @return NetteFormsContainer
143161 */
144162 protected function createComponent ($ name )
145163 {
@@ -169,7 +187,7 @@ private function getFirstControlName()
169187 /**
170188 * @param string $name
171189 *
172- * @return \Nette\Forms\Container
190+ * @return NetteFormsContainer
173191 */
174192 protected function createContainer ($ name )
175193 {
@@ -204,8 +222,8 @@ public function isSubmittedBy()
204222 *
205223 * @param string|int $name
206224 *
207- * @throws \Nette\InvalidArgumentException
208- * @return \Nette\Forms\Container
225+ * @return NetteFormsContainer
226+ *@throws InvalidArgumentException
209227 */
210228 public function createOne ($ name = NULL )
211229 {
@@ -216,7 +234,7 @@ public function createOne($name = NULL)
216234
217235 // Container is overriden, therefore every request for getComponent($name, FALSE) would return container
218236 if (isset ($ this ->created [$ name ])) {
219- throw new Nette \ InvalidArgumentException ("Container with name ' $ name' already exists. " );
237+ throw new InvalidArgumentException ("Container with name ' $ name' already exists. " );
220238 }
221239
222240 return $ this [$ name ];
@@ -225,16 +243,17 @@ public function createOne($name = NULL)
225243
226244
227245 /**
228- * @param array|\Traversable $values
229- * @param bool $erase
230- * @param bool $onlyDisabled
231- * @return \Nette\Forms\Container|Container
246+ * @param array|Traversable $values
247+ * @param bool $erase
248+ * @param bool $onlyDisabled
249+ *
250+ * @return NetteFormsContainer|Container
232251 */
233252 public function setValues ($ values , $ erase = FALSE , $ onlyDisabled = FALSE )
234253 {
235254 if (!$ this ->form ->isAnchored () || !$ this ->form ->isSubmitted ()) {
236255 foreach ($ values as $ name => $ value ) {
237- if ((is_array ($ value ) || $ value instanceof \ Traversable) && !$ this ->getComponent ($ name , FALSE )) {
256+ if ((is_array ($ value ) || $ value instanceof Traversable) && !$ this ->getComponent ($ name , FALSE )) {
238257 $ this ->createOne ($ name );
239258 }
240259 }
@@ -256,7 +275,7 @@ protected function loadHttpData()
256275 }
257276
258277 foreach ((array ) $ this ->getHttpData () as $ name => $ value ) {
259- if ((is_array ($ value ) || $ value instanceof \ Traversable) && !$ this ->getComponent ($ name , FALSE )) {
278+ if ((is_array ($ value ) || $ value instanceof Traversable) && !$ this ->getComponent ($ name , FALSE )) {
260279 $ this ->createOne ($ name );
261280 }
262281 }
@@ -307,7 +326,7 @@ private function getHttpData()
307326 {
308327 if ($ this ->httpPost === NULL ) {
309328 $ path = explode (self ::NAME_SEPARATOR , $ this ->lookupPath ('Nette\Forms\Form ' ));
310- $ this ->httpPost = Nette \ Utils \ Arrays::get ($ this ->getForm ()->getHttpData (), $ path , NULL );
329+ $ this ->httpPost = Arrays::get ($ this ->getForm ()->getHttpData (), $ path , NULL );
311330 }
312331
313332 return $ this ->httpPost ;
@@ -317,10 +336,10 @@ private function getHttpData()
317336
318337 /**
319338 * @internal
320- * @param \Nette\Application\ Request $request
339+ * @param Request $request
321340 * @return Container
322341 */
323- public function setRequest (Nette \ Application \ Request $ request )
342+ public function setRequest (Request $ request )
324343 {
325344 $ this ->httpRequest = $ request ;
326345 return $ this ;
@@ -329,7 +348,7 @@ public function setRequest(Nette\Application\Request $request)
329348
330349
331350 /**
332- * @return \Nette\Application\ Request
351+ * @return Request
333352 */
334353 private function getRequest ()
335354 {
@@ -343,40 +362,40 @@ private function getRequest()
343362
344363
345364 /**
346- * @param \Nette\Forms\Container $container
347- * @param boolean $cleanUpGroups
365+ * @param NetteFormsContainer $container
366+ * @param boolean $cleanUpGroups
348367 *
349- * @throws \Nette\InvalidArgumentException
350368 * @return void
369+ * @throws InvalidArgumentException
351370 */
352- public function remove (Nette \ Forms \ Container $ container , $ cleanUpGroups = FALSE )
371+ public function remove (NetteFormsContainer $ container , $ cleanUpGroups = FALSE )
353372 {
354373 if ($ container ->parent !== $ this ) {
355- throw new Nette \ InvalidArgumentException ('Given component ' . $ container ->name . ' is not children of ' . $ this ->name . '. ' );
374+ throw new InvalidArgumentException ('Given component ' . $ container ->name . ' is not children of ' . $ this ->name . '. ' );
356375 }
357376
358377 // to check if form was submitted by this one
359378 foreach ($ container ->getComponents (TRUE , 'Nette\Forms\ISubmitterControl ' ) as $ button ) {
360- /** @var \Nette\Forms\Controls\ SubmitButton $button */
379+ /** @var SubmitButton $button */
361380 if ($ button ->isSubmittedBy ()) {
362381 $ this ->submittedBy = TRUE ;
363382 break ;
364383 }
365384 }
366385
367- /** @var \Nette\Forms\Controls\ BaseControl[] $components */
386+ /** @var BaseControl[] $components */
368387 $ components = $ container ->getComponents (TRUE );
369388 $ this ->removeComponent ($ container );
370389
371390 // reflection is required to hack form groups
372- $ groupRefl = Nette \ Reflection \ ClassType::from ('Nette\Forms\ControlGroup ' );
391+ $ groupRefl = ClassType::from ('Nette\Forms\ControlGroup ' );
373392 $ controlsProperty = $ groupRefl ->getProperty ('controls ' );
374393 $ controlsProperty ->setAccessible (TRUE );
375394
376395 // walk groups and clean then from removed components
377396 $ affected = [];
378397 foreach ($ this ->getForm ()->getGroups () as $ group ) {
379- /** @var \ SplObjectStorage $groupControls */
398+ /** @var SplObjectStorage $groupControls */
380399 $ groupControls = $ controlsProperty ->getValue ($ group );
381400
382401 foreach ($ components as $ control ) {
@@ -398,7 +417,7 @@ public function remove(Nette\Forms\Container $container, $cleanUpGroups = FALSE)
398417 }
399418 }
400419
401- /** @var \Nette\Forms\ ControlGroup[] $affected */
420+ /** @var ControlGroup[] $affected */
402421 foreach ($ affected as $ group ) {
403422 if (!$ group ->getControls () && in_array ($ group , $ this ->getForm ()->getGroups (), TRUE )) {
404423 $ this ->getForm ()->removeGroup ($ group );
@@ -449,13 +468,13 @@ public function isAllFilled(array $exceptChildren = [])
449468 {
450469 $ components = [];
451470 foreach ($ this ->getComponents (FALSE , 'Nette\Forms\IControl ' ) as $ control ) {
452- /** @var \Nette\Forms\Controls\ BaseControl $control */
471+ /** @var BaseControl $control */
453472 $ components [] = $ control ->getName ();
454473 }
455474
456475 foreach ($ this ->getContainers () as $ container ) {
457476 foreach ($ container ->getComponents (TRUE , 'Nette\Forms\ISubmitterControl ' ) as $ button ) {
458- /** @var \Nette\Forms\Controls\ SubmitButton $button */
477+ /** @var SubmitButton $button */
459478 $ exceptChildren [] = $ button ->getName ();
460479 }
461480 }
@@ -468,22 +487,23 @@ public function isAllFilled(array $exceptChildren = [])
468487
469488 /**
470489 * @param $name
471- * @return \Nette\Forms\Container
490+ *
491+ * @return NetteFormsContainer
472492 */
473493 public function addContainer ($ name )
474494 {
475- return $ this [$ name ] = new Nette \ Forms \ Container ();
495+ return $ this [$ name ] = new NetteFormsContainer ();
476496 }
477497
478498
479499
480500 /**
481- * @param \Nette\ComponentModel\ IComponent $component
501+ * @param IComponent $component
482502 * @param $name
483503 * @param null $insertBefore
484- * @return \Nette\ComponentModel\Container|\Nette\Forms\Container
504+ * @return self
485505 */
486- public function addComponent (Nette \ ComponentModel \ IComponent $ component , $ name , $ insertBefore = NULL )
506+ public function addComponent (IComponent $ component , $ name , $ insertBefore = NULL )
487507 {
488508 $ group = $ this ->currentGroup ;
489509 $ this ->currentGroup = NULL ;
@@ -506,12 +526,12 @@ public function addComponent(Nette\ComponentModel\IComponent $component, $name,
506526 public static function register ($ methodName = 'addDynamic ' )
507527 {
508528 if (self ::$ registered ) {
509- Nette \ Utils \ ObjectMixin::setExtensionMethod ('Nette\Forms\Container ' , self ::$ registered , function () {
510- throw new Nette \ MemberAccessException ;
529+ ObjectMixin::setExtensionMethod ('Nette\Forms\Container ' , self ::$ registered , function () {
530+ throw new MemberAccessException ;
511531 });
512532 }
513533
514- Nette \ Utils \ ObjectMixin::setExtensionMethod ('Nette\Forms\Container ' , $ methodName , function (Nette \ Forms \ Container $ _this , $ name , $ factory , $ createDefault = 0 , $ forceDefault = FALSE ) {
534+ ObjectMixin::setExtensionMethod ('Nette\Forms\Container ' , $ methodName , function (NetteFormsContainer $ _this , $ name , $ factory , $ createDefault = 0 , $ forceDefault = FALSE ) {
515535 $ control = new Container ($ factory , $ createDefault , $ forceDefault );
516536 $ control ->currentGroup = $ _this ->currentGroup ;
517537 return $ _this [$ name ] = $ control ;
@@ -521,7 +541,7 @@ public static function register($methodName = 'addDynamic')
521541 return ;
522542 }
523543
524- Nette \ Utils \ ObjectMixin::setExtensionMethod ('Nette\Forms\Controls\SubmitButton ' , 'addRemoveOnClick ' , function (SubmitButton $ _this , $ callback = NULL ) {
544+ ObjectMixin::setExtensionMethod ('Nette\Forms\Controls\SubmitButton ' , 'addRemoveOnClick ' , function (SubmitButton $ _this , $ callback = NULL ) {
525545 $ _this ->setValidationScope (FALSE );
526546 $ _this ->onClick [] = function (SubmitButton $ button ) use ($ callback ) {
527547 $ replicator = $ button ->lookup (__NAMESPACE__ . '\Container ' );
@@ -537,7 +557,7 @@ public static function register($methodName = 'addDynamic')
537557 return $ _this ;
538558 });
539559
540- Nette \ Utils \ ObjectMixin::setExtensionMethod ('Nette\Forms\Controls\SubmitButton ' , 'addCreateOnClick ' , function (SubmitButton $ _this , $ allowEmpty = FALSE , $ callback = NULL ) {
560+ ObjectMixin::setExtensionMethod ('Nette\Forms\Controls\SubmitButton ' , 'addCreateOnClick ' , function (SubmitButton $ _this , $ allowEmpty = FALSE , $ callback = NULL ) {
541561 $ _this ->onClick [] = function (SubmitButton $ button ) use ($ allowEmpty , $ callback ) {
542562 $ replicator = $ button ->lookup (__NAMESPACE__ . '\Container ' );
543563 /** @var Container $replicator */
0 commit comments