@@ -385,202 +385,3 @@ private function initialize()
385
385
}
386
386
}
387
387
}
388
-
389
- class FormFieldRegistry
390
- {
391
- private $ fields = array ();
392
-
393
- private $ base ;
394
-
395
- /**
396
- * Adds a field to the registry.
397
- *
398
- * @param FormField $field The field
399
- *
400
- * @throws \InvalidArgumentException when the name is malformed
401
- */
402
- public function add (FormField $ field )
403
- {
404
- $ segments = $ this ->getSegments ($ field ->getName ());
405
-
406
- $ target =& $ this ->fields ;
407
- while ($ segments ) {
408
- if (!is_array ($ target )) {
409
- $ target =
10000
array ();
410
- }
411
- $ path = array_shift ($ segments );
412
- if ('' === $ path ) {
413
- $ target =& $ target [];
414
- } else {
415
- $ target =& $ target [$ path ];
416
- }
417
- }
418
- $ target = $ field ;
419
- }
420
-
421
- /**
422
- * Removes a field and its children from the registry.
423
- *
424
- * @param string $name The fully qualified name of the base field
425
- *
426
- * @throws \InvalidArgumentException when the name is malformed
427
- */
428
- public function remove ($ name )
429
- {
430
- $ segments = $ this ->getSegments ($ name );
431
- $ target =& $ this ->fields ;
432
- while (count ($ segments ) > 1 ) {
433
- $ path = array_shift ($ segments );
434
- if (!array_key_exists ($ path , $ target )) {
435
- return ;
436
- }
437
- $ target =& $ target [$ path ];
438
- }
439
- unset($ target [array_shift ($ segments )]);
440
- }
441
-
442
- /**
443
- * Returns the value of the field and its children.
444
- *
445
- * @param string $name The fully qualified name of the field
446
- *
447
- * @return mixed The value of the field
448
- *
449
- * @throws \InvalidArgumentException when the name is malformed
450
- * @throws \InvalidArgumentException if the field does not exist
451
- */
452
- public function &get ($ name )
453
- {
454
- $ segments = $ this ->getSegments ($ name );
455
- $ target =& $ this ->fields ;
456
- while ($ segments ) {
457
- $ path = array_shift ($ segments );
458
- if (!array_key_exists ($ path , $ target )) {
459
- throw new \InvalidArgumentException (sprintf ('Unreachable field "%s" ' , $ path ));
460
- }
461
- $ target =& $ target [$ path ];
462
- }
463
-
464
- return $ target ;
465
- }
466
-
467
- /**
468
- * Tests whether the form has the given field.
469
- *
470
- * @param string $name The fully qualified name of the field
471
- *
472
- * @return Boolean Whether the form has the given field
473
- */
474
- public function has ($ name )
475
- {
476
- try {
477
- $ this ->get ($ name );
478
-
479
- return true ;
480
- } catch (\InvalidArgumentException $ e ) {
481
- return false ;
482
- }
483
- }
484
-
485
- /**
486
- * Set the value of a field and its children.
487
- *
488
- * @param string $name The fully qualified name of the field
489
- * @param mixed $value The value
490
- *
491
- * @throws \InvalidArgumentException when the name is malformed
492
- * @throws \InvalidArgumentException if the field does not exist
493
- */
494
- public function set ($ name , $ value )
495
- {
496
- $ target =& $ this ->get ($ name );
497
- if (!is_array ($ value ) || $ target instanceof Field \ChoiceFormField) {
498
- $ target ->setValue ($ value );
499
- } else {
500
- $ fields = self ::create ($ name , $ value );
501
- foreach ($ fields ->all () as $ k => $ v ) {
502
- $ this ->set ($ k , $ v );
503
- }
504
- }
505
- }
506
-
507
- /**
508
- * Returns the list of field with their value.
509
- *
510
- * @return array The list of fields as array((string) Fully qualified name => (mixed) value)
511
- */
512
- public function all ()
513
- {
514
- return $ this ->walk ($ this ->fields , $ this ->base );
515
- }
516
-
517
- /**
518
- * Creates an instance of the class.
519
- *
520
- * This function is made private because it allows overriding the $base and
521
- * the $values properties without any type checking.
522
- *
523
- * @param string $base The fully qualified name of the base field
524
- * @param array $values The values of the fields
525
- *
526
- * @return FormFieldRegistry
527
- */
528
- private static function create ($ base , array $ values )
529
- {
530
- $ registry = new static ();
531
- $ registry ->base = $ base ;
532
- $ registry ->fields = $ values ;
533
-
534
- return $ registry ;
535
- }
536
-
537
- /**
538
- * Transforms a PHP array in a list of fully qualified name / value.
539
- *
540
- * @param array $array The PHP array
541
- * @param string $base The name of the base field
542
- * @param array $output The initial values
543
- *
544
- * @return array The list of fields as array((string) Fully qualified name => (mixed) value)
545
- */
546
- private function walk (array $ array , $ base = '' , array &$ output = array ())
547
- {
548
- foreach ($ array as $ k => $ v ) {
549
- $ path = empty ($ base ) ? $ k : sprintf ("%s[%s] " , $ base , $ k );
550
- if (is_array ($ v )) {
551
- $ this ->walk ($ v , $ path , $ output );
552
- } else {
553
- $ output [$ path ] = $ v ;
554
- }
555
- }
556
-
557
- return $ output ;
558
- }
559
-
560
- /**
561
- * Splits a field name into segments as a web browser would do.
562
- *
563
- * <code>
564
- * getSegments('base[foo][3][]') = array('base', 'foo, '3', '');
565
- * </code>
566
- *
567
- * @param string $name The name of the field
568
- *
569
- * @return array The list of segments
570
- *
571
- * @throws \InvalidArgumentException when the name is malformed
572
- */
573
- private function getSegments ($ name )
574
- {
575
- if (preg_match ('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/ ' , $ name , $ m )) {
576
- $ segments = array ($ m ['base ' ]);
577
- while (preg_match ('/^\[(?P<segment>.*?)\](?P<extra>.*)$/ ' , $ m ['extra ' ], $ m )) {
578
- $ segments [] = $ m ['segment ' ];
579
- }
580
-
581
- return $ segments ;
582
- }
583
-
584
- throw new \InvalidArgumentException (sprintf ('Malformed field path "%s" ' , $ name ));
585
- }
586
- }
0 commit comments