|
2 | 2 |
|
3 | 3 | namespace Mindee\Parsing\V2\Field; |
4 | 4 |
|
| 5 | +use InvalidArgumentException; |
| 6 | + |
5 | 7 | /** |
6 | 8 | * A field containing a nested set of inference fields. |
7 | 9 | */ |
@@ -43,4 +45,103 @@ public function toStringFromList(): string |
43 | 45 | { |
44 | 46 | return substr($this->fields->toString(2), 4); |
45 | 47 | } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns a ListField instance for the specified key. |
| 51 | + * |
| 52 | + * @param string $key The key of the list field to retrieve. |
| 53 | + * @return ListField |
| 54 | + * @throws InvalidArgumentException When the field does not exist or is not a list field. |
| 55 | + */ |
| 56 | + public function getListField(string $key): ListField |
| 57 | + { |
| 58 | + $field = $this->fields->get($key); |
| 59 | + if (!($field instanceof ListField)) { |
| 60 | + throw new InvalidArgumentException("Field $key is not a list field."); |
| 61 | + } |
| 62 | + return $field; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns a SimpleField instance for the specified key. |
| 67 | + * |
| 68 | + * @param string $key The key of the simple field to retrieve. |
| 69 | + * @return SimpleField |
| 70 | + * @throws InvalidArgumentException When the field does not exist or is not a simple field. |
| 71 | + */ |
| 72 | + public function getSimpleField(string $key): SimpleField |
| 73 | + { |
| 74 | + $field = $this->fields->get($key); |
| 75 | + if (!($field instanceof SimpleField)) { |
| 76 | + throw new InvalidArgumentException("Field $key is not a simple field."); |
| 77 | + } |
| 78 | + return $field; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns an ObjectField instance for the specified key. |
| 83 | + * |
| 84 | + * @param string $key The key of the simple field to retrieve. |
| 85 | + * @return ObjectField |
| 86 | + * @throws InvalidArgumentException When the field does not exist or is not a simple field. |
| 87 | + */ |
| 88 | + public function getObjectField(string $key): ObjectField |
| 89 | + { |
| 90 | + $field = $this->fields->get($key); |
| 91 | + if (!($field instanceof ObjectField)) { |
| 92 | + throw new InvalidArgumentException("Field $key is not a simple field."); |
| 93 | + } |
| 94 | + return $field; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns an array of SimpleField instances. |
| 99 | + * |
| 100 | + * @return SimpleField[] |
| 101 | + * @throws InvalidArgumentException When a field does not exist or is not a simple field. |
| 102 | + */ |
| 103 | + public function getSimpleFields(): array |
| 104 | + { |
| 105 | + $out = []; |
| 106 | + foreach ($this->fields->getArrayCopy() as $field) { |
| 107 | + if ($field instanceof SimpleField) { |
| 108 | + $out[] = $field; |
| 109 | + } |
| 110 | + } |
| 111 | + return $out; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns an array of ListField instances. |
| 116 | + * |
| 117 | + * @return ListField[] |
| 118 | + * @throws InvalidArgumentException When a field does not exist or is not a list field. |
| 119 | + */ |
| 120 | + public function getListFields(): array |
| 121 | + { |
| 122 | + $out = []; |
| 123 | + foreach ($this->fields->getArrayCopy() as $field) { |
| 124 | + if ($field instanceof ListField) { |
| 125 | + $out[] = $field; |
| 126 | + } |
| 127 | + } |
| 128 | + return $out; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns an array of ObjectField instances. |
| 133 | + * |
| 134 | + * @return ObjectField[] |
| 135 | + * @throws InvalidArgumentException When a field does not exist or is not an object field. |
| 136 | + */ |
| 137 | + public function getObjectFields(): array |
| 138 | + { |
| 139 | + $out = []; |
| 140 | + foreach ($this->fields->getArrayCopy() as $field) { |
| 141 | + if ($field instanceof ObjectField) { |
| 142 | + $out[] = $field; |
| 143 | + } |
| 144 | + } |
| 145 | + return $out; |
| 146 | + } |
46 | 147 | } |
0 commit comments