| Total Complexity | 18 |
| Total Lines | 58 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 15 | trait HasFormFields |
||
| 16 | { |
||
| 17 | public function populate(FormInterface $form): void |
||
| 18 | { |
||
| 19 | $data = $form->getValues(); |
||
| 20 | $mirror = new ReflectionClass($this); |
||
| 21 | $properties = $mirror->getProperties(); |
||
| 22 | |||
| 23 | foreach ($properties as $property) { |
||
| 24 | $fieldName = $property->getName(); |
||
| 25 | $attributes = $property->getAttributes(Field::class); |
||
| 26 | |||
| 27 | if (count($attributes) > 0) { |
||
| 28 | $rules = $attributes[0]->newInstance()->rules; |
||
| 29 | |||
| 30 | if (strpos($rules, '|') !== false) { |
||
| 31 | $rules = explode('|', $rules); |
||
| 32 | $fieldType = array_shift($rules); |
||
| 33 | } else { |
||
| 34 | $fieldType = $rules; |
||
| 35 | $rules = []; |
||
|
|
|||
| 36 | } |
||
| 37 | |||
| 38 | $value = $data[$fieldName]; |
||
| 39 | $this->setField($fieldName, $fieldType, $value); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | private function setField(string $fieldName, string $fieldType, mixed $value): void |
||
| 73 | } |
||
| 74 | } |
||
| 76 |