1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\DetailView\Rows; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Itstructure\DetailView\Formatters\{HtmlFormatter, ImageFormatter, TextFormatter, UrlFormatter}; |
8
|
|
|
use Itstructure\DetailView\Interfaces\Formatable; |
9
|
|
|
use Itstructure\DetailView\Traits\{Configurable, Attributable}; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class BaseRow |
13
|
|
|
* @package Itstructure\DetailView\Rows |
14
|
|
|
*/ |
15
|
|
|
abstract class BaseRow |
16
|
|
|
{ |
17
|
|
|
use Configurable, Attributable; |
18
|
|
|
|
19
|
|
|
const |
20
|
|
|
FORMATTER_HTML = 'html', |
21
|
|
|
FORMATTER_IMAGE = 'image', |
22
|
|
|
FORMATTER_TEXT = 'text', |
23
|
|
|
FORMATTER_URL = 'url', |
24
|
|
|
|
25
|
|
|
FORMATTER_DEFINITIONS = [ |
26
|
|
|
self::FORMATTER_HTML => HtmlFormatter::class, |
27
|
|
|
self::FORMATTER_IMAGE => ImageFormatter::class, |
28
|
|
|
self::FORMATTER_TEXT => TextFormatter::class, |
29
|
|
|
self::FORMATTER_URL => UrlFormatter::class, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $label; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $attribute; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var mixed |
44
|
|
|
*/ |
45
|
|
|
protected $value; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string|Formatable |
49
|
|
|
*/ |
50
|
|
|
protected $format; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* BaseRow constructor. |
54
|
|
|
* @param array $config |
55
|
|
|
*/ |
56
|
|
|
public function __construct(array $config) |
57
|
|
|
{ |
58
|
|
|
$this->loadConfig($config); |
59
|
|
|
$this->buildFormatter(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Model $model |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
abstract public function getValue(Model $model); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Render row attribute value. |
70
|
|
|
* @param $model |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function render($model) |
74
|
|
|
{ |
75
|
|
|
return $this->formatTo($this->getValue($model)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Format value with formatter. |
80
|
|
|
* @param $value |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function formatTo($value) |
84
|
|
|
{ |
85
|
|
|
return $this->format->format($value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get title for detail head. |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getLabel(): string |
93
|
|
|
{ |
94
|
|
|
return $this->label ?? ucfirst($this->attribute); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get attribute. |
99
|
|
|
* @return string|null |
100
|
|
|
*/ |
101
|
|
|
public function getAttribute() |
102
|
|
|
{ |
103
|
|
|
return $this->attribute; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Formatable $formatter |
108
|
|
|
*/ |
109
|
|
|
protected function setFormatter(Formatable $formatter): void |
110
|
|
|
{ |
111
|
|
|
$this->format = $formatter; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @throws Exception |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
protected function buildFormatter(): void |
119
|
|
|
{ |
120
|
|
|
if (is_null($this->format)) { |
121
|
|
|
$class = self::FORMATTER_DEFINITIONS[self::FORMATTER_TEXT]; |
122
|
|
|
$this->format = new $class; |
123
|
|
|
|
124
|
|
|
} else if (is_string($this->format)) { |
125
|
|
|
$class = self::FORMATTER_DEFINITIONS[$this->format] ?? self::FORMATTER_DEFINITIONS[self::FORMATTER_TEXT]; |
126
|
|
|
$this->format = new $class; |
127
|
|
|
|
128
|
|
|
} else if (is_array($this->format)) { |
|
|
|
|
129
|
|
|
if (isset($this->format['class']) && class_exists($this->format['class'])) { |
130
|
|
|
$this->setFormatter(new $this->format['class']($this->format)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} else if (!is_object($this->format) || !($this->format instanceof Formatable)) { |
134
|
|
|
throw new Exception('Incorrect formatter.'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|