Factory
5 years ago
FieldCollection
5 years ago
FormField
5 years ago
FormFieldMediator
5 years ago
FieldCollection.php
5 years ago
FormField.php
5 years ago
FieldCollection.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI; |
| 4 | |
| 5 | use Give\Framework\FieldsAPI\FieldCollection\Contract\Node; |
| 6 | use Give\Framework\FieldsAPI\FieldCollection\Contract\GroupNode; |
| 7 | |
| 8 | /** |
| 9 | * @unreleased |
| 10 | */ |
| 11 | class FieldCollection implements GroupNode { |
| 12 | |
| 13 | use FieldCollection\NameCollision; |
| 14 | use FieldCollection\InsertNode; |
| 15 | use FieldCollection\MoveNode; |
| 16 | use FieldCollection\RemoveNode; |
| 17 | use FieldCollection\WalkNodes; |
| 18 | |
| 19 | /** @var string */ |
| 20 | protected $name; |
| 21 | |
| 22 | /** @var Node[] */ |
| 23 | protected $nodes = []; |
| 24 | |
| 25 | public function __construct( $name, array $nodes = [] ) { |
| 26 | $this->name = $name; |
| 27 | $this->nodes = $nodes; |
| 28 | } |
| 29 | |
| 30 | public function getName() { |
| 31 | return $this->name; |
| 32 | } |
| 33 | |
| 34 | public function append( Node $node ) { |
| 35 | $this->insertAtIndex( $this->count(), $node ); |
| 36 | return $this; |
| 37 | } |
| 38 | |
| 39 | public function getNodeIndexByName( $name ) { |
| 40 | foreach ( $this->nodes as $index => $node ) { |
| 41 | if ( $node->getName() === $name ) { |
| 42 | return $index; |
| 43 | } |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | public function getNodeByName( $name ) { |
| 49 | foreach ( $this->nodes as $index => $node ) { |
| 50 | if ( $node->getName() === $name ) { |
| 51 | return $node; |
| 52 | } |
| 53 | if ( $node instanceof GroupNode ) { |
| 54 | return $node->getNodeByName( $name ); |
| 55 | } |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | public function jsonserialize() { |
| 61 | return array_map( |
| 62 | function( $node ) { |
| 63 | return $node->jsonserialize(); |
| 64 | }, |
| 65 | $this->nodes |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | public function getFields() { |
| 70 | return $this->nodes; |
| 71 | } |
| 72 | |
| 73 | public function count() { |
| 74 | return count( $this->getFields() ); |
| 75 | } |
| 76 | } |
| 77 |