FormContent   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
eloc 38
c 0
b 0
f 0
dl 0
loc 89
ccs 46
cts 46
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A validateRequest() 0 6 1
A formName() 0 9 1
A stringify() 0 12 3
A stringifyLineFormat() 0 3 1
A humanReadableKey() 0 16 3
A getCastableClassByModel() 0 8 3
A fillFromRequest() 0 10 3
A requestKeysToSave() 0 3 1
1
<?php
2
3
namespace FormEntries\Forms;
4
5
use FormEntries\Models\FormEntry;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Lang;
8
use Illuminate\Support\Str;
9
use JsonFieldCast\Json\AbstractMeta;
10
11
abstract class FormContent extends AbstractMeta
12
{
13
    use HasTypesMap;
14
15
    protected array $requestKeysToSave = ['*'];
16
17 10
    public static function getCastableClassByModel(FormEntry $model, ?array $data = null): string
18
    {
19 10
        $class = null;
20 10
        if ($model->content_type) {
0 ignored issues
show
Bug introduced by
The property content_type does not exist on FormEntries\Models\FormEntry. Did you mean content?
Loading history...
21 10
            $class = static::getClassByType($model->content_type);
22
        }
23
24 10
        return $class ?: UniversalFormContent::class;
25
    }
26
27 10
    public function fillFromRequest(Request $request): static
28
    {
29 10
        $requestKeysToSave = $this->requestKeysToSave();
30 10
        if (!empty($requestKeysToSave) && $requestKeysToSave[0] == '*') {
31 9
            $this->data = $request->all();
32
        } else {
33 1
            $this->data = $request->only($requestKeysToSave);
34
        }
35
36 10
        return $this;
37
    }
38
39 10
    public function requestKeysToSave(): array
40
    {
41 10
        return $this->requestKeysToSave;
42
    }
43
44 2
    public function formName(): string
45
    {
46 2
        return Str::title(
47 2
            Str::snake(
48 2
                Str::beforeLast(
49 2
                    class_basename(static::class),
50 2
                    'FormContent'
51 2
                ),
52 2
                ' '
53 2
            )
54 2
        );
55
    }
56
57 2
    public function stringify(): string
58
    {
59 2
        $string = '';
60 2
        foreach ($this->data as $key => $value) {
61 1
            $string .= sprintf(
62 1
                $this->stringifyLineFormat(),
63 1
                $this->humanReadableKey((string) $key),
64 1
                $value ?: trans('forms-entries::messages.empty_value')
65 1
            );
66
        }
67
68 2
        return $string;
69
    }
70
71 1
    public function stringifyLineFormat(): string
72
    {
73 1
        return "%s: \n%s \n\n";
74
    }
75
76 9
    public function validateRequest(Request $request): static
77
    {
78
        // Example:
79
        // $request->validate([]);
80
81 9
        return $this;
82
    }
83
84 1
    protected function humanReadableKey(string $key): string
85
    {
86 1
        $formID = static::class;
87
        foreach (
88 1
            [
89 1
                "forms-entry-keys.{$formID}.{$key}",
90 1
                "forms-entry-keys.{$key}",
91 1
                "forms-entries::keys.{$key}",
92 1
            ] as $path
93
        ) {
94 1
            if (Lang::has($path)) {
95 1
                return (string) Lang::get($path);
96
            }
97
        }
98
99 1
        return Str::ucfirst(Str::snake(Str::camel($key), ' '));
100
    }
101
}
102