Test Failed
Push — master ( 0b650e...b4d0bc )
by Chris
19:25
created

UsesFormFieldDataTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 19
c 0
b 0
f 0
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A formFields() 0 17 2
A getFormFieldData() 0 9 1
1
<?php
2
3
namespace Leonidas\Framework\Site\Form;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use WebTheory\Saveyour\Contracts\Field\FormFieldInterface;
7
use WebTheory\Saveyour\Controller\Builder\FormFieldControllerBuilder;
8
9
trait UsesFormFieldDataTrait
10
{
11
    protected function formFields(ServerRequestInterface $request): array
12
    {
13
        $fields = $this->fields($request);
14
        $data = $this->data($request);
0 ignored issues
show
Bug introduced by
It seems like data() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
        /** @scrutinizer ignore-call */ 
15
        $data = $this->data($request);
Loading history...
15
        $formatting = $this->formatting($request);
0 ignored issues
show
Bug introduced by
It seems like formatting() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
        /** @scrutinizer ignore-call */ 
16
        $formatting = $this->formatting($request);
Loading history...
16
17
        foreach ($fields as $key => &$field) {
18
            $controller = (FormFieldControllerBuilder::for($field->getName()))
19
                ->formField($field)
20
                ->dataManager($data[$key] ?? null)
21
                ->formatter($formatting[$key] ?? null)
22
                ->get();
23
24
            $field = $this->getFormFieldData($controller->compose($request));
25
        }
26
27
        return $fields;
28
    }
29
30
    /**
31
     * @return string|array|FormFieldInterface
32
     */
33
    protected function getFormFieldData(FormFieldInterface $field)
34
    {
35
        return [
36
            'id' => $field->getId(),
37
            'name' => $field->getName(),
38
            'value' => $field->getValue(),
39
            'disabled' => $field->isDisabled(),
40
            'readonly' => $field->isReadOnly(),
41
            'required' => $field->isRequired(),
42
        ];
43
    }
44
45
    /**
46
     * @return array<string,FormFieldInterface>
47
     */
48
    abstract protected function fields(ServerRequestInterface $request): array;
49
}
50