Detail   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 41
c 1
b 0
f 0
dl 0
loc 121
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fillRowObjects() 0 3 1
A render() 0 13 1
A __construct() 0 3 1
A getCaptionColumnHead() 0 7 1
A getValueColumnHead() 0 7 1
B applyColumnsConfig() 0 20 8
1
<?php
2
3
namespace Itstructure\DetailView;
4
5
use Closure;
6
use Exception;
7
use Illuminate\Http\Request;
8
use Illuminate\Database\Eloquent\Model;
9
use Itstructure\DetailView\Heads\ColumnHead;
10
use Itstructure\DetailView\Rows\{BaseRow, CallbackRow, DefaultRow};
11
use Itstructure\DetailView\Traits\{Configurable, Attributable};
12
13
/**
14
 * Class Detail
15
 * @package Itstructure\DetailView
16
 */
17
class Detail
18
{
19
    use Configurable, Attributable;
20
21
    /**
22
     * @var Model
23
     */
24
    protected $model;
25
26
    /**
27
     * @var string
28
     */
29
    protected $title;
30
31
    /**
32
     * @var array
33
     */
34
    protected $rowFields = [];
35
36
    /**
37
     * @var BaseRow[]
38
     */
39
    protected $rowObjects = [];
40
41
    /**
42
     * @var array
43
     */
44
    protected $captionColumnConfig = [];
45
46
    /**
47
     * @var array
48
     */
49
    protected $valueColumnConfig = [];
50
51
    /**
52
     * @var bool
53
     */
54
    protected $showHead = true;
55
56
    /**
57
     * Detail view constructor.
58
     * @param array $config
59
     * @throws Exception
60
     */
61
    public function __construct(array $config)
62
    {
63
        $this->loadConfig($config);
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function render(): string
70
    {
71
        $this->applyColumnsConfig();
72
73
        return view('detail_view::detail', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('detail_view...s->showHead))->render() could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
74
            'detailObj' => $this,
75
            'model' => $this->model,
76
            'rowObjects' => $this->rowObjects,
77
            'title' => $this->title,
78
            'captionColumnHead' => $this->getCaptionColumnHead(),
79
            'valueColumnHead' => $this->getValueColumnHead(),
80
            'showHead' => $this->showHead
81
        ])->render();
82
    }
83
84
    protected function applyColumnsConfig(): void
85
    {
86
        foreach ($this->rowFields as $key => $config) {
87
            if (is_string($config)) {
88
                $this->fillRowObjects(new DefaultRow(['attribute' => $config]));
89
                continue;
90
            }
91
92
            if (is_array($config)) {
93
                if (isset($config['class']) && class_exists($config['class'])) {
94
                    $this->fillRowObjects(new $config['class']($config));
95
                    continue;
96
                }
97
98
                if (isset($config['value']) && $config['value'] instanceof Closure) {
99
                    $this->fillRowObjects(new CallbackRow($config));
100
                    continue;
101
                }
102
103
                $this->fillRowObjects(new DefaultRow($config));
104
            }
105
        }
106
    }
107
108
    /**
109
     * @param BaseRow $rowObject
110
     */
111
    protected function fillRowObjects(BaseRow $rowObject): void
112
    {
113
        $this->rowObjects = array_merge($this->rowObjects, [$rowObject]);
114
    }
115
116
    /**
117
     * @return ColumnHead
118
     */
119
    protected function getCaptionColumnHead(): ColumnHead
120
    {
121
        $config = array_merge([
122
            'label' => trans('detail_view::detail.title')
123
        ], $this->captionColumnConfig);
124
125
        return new ColumnHead($config);
126
    }
127
128
    /**
129
     * @return ColumnHead
130
     */
131
    protected function getValueColumnHead(): ColumnHead
132
    {
133
        $config = array_merge([
134
            'label' => trans('detail_view::detail.value')
135
        ], $this->valueColumnConfig);
136
137
        return new ColumnHead($config);
138
    }
139
}
140