|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ngtfkx\Laradeck\FormBuilder\Traits; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
|
|
8
|
|
|
trait Common |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $id; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $name; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Collection |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $classes; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Collection |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $attributes; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Collection |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $styles; |
|
34
|
|
|
|
|
35
|
|
|
public function id($value): self |
|
36
|
|
|
{ |
|
37
|
|
|
$this->id = $value; |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function name($value): self |
|
43
|
|
|
{ |
|
44
|
|
|
$this->name = $value; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
View Code Duplication |
public function class(...$classes): self |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
foreach($classes as $class) { |
|
52
|
|
|
if(!$this->classes->contains($class)) { |
|
53
|
|
|
$this->classes->push($class); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
public function classes(iterable $classes): self |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
foreach($classes as $class) { |
|
63
|
|
|
if(!$this->classes->contains($class)) { |
|
64
|
|
|
$this->classes->push($class); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function style(string $key, string $value = null): self |
|
72
|
|
|
{ |
|
73
|
|
|
if (empty($value)) { |
|
74
|
|
|
$pos = strpos($key, ':', 2); |
|
75
|
|
|
if ($pos !== false) { |
|
76
|
|
|
$parts = explode(':', strrev($key)); |
|
77
|
|
|
$key = strrev($parts[1]); |
|
78
|
|
|
$value = strrev($parts[0]); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($key && $value) { |
|
|
|
|
|
|
83
|
|
|
$this->styles->put($key, $value); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function styles(iterable $styles): self |
|
90
|
|
|
{ |
|
91
|
|
|
foreach($styles as $key => $value) { |
|
92
|
|
|
$this->styles->put($key, $value); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function attr(string $key, string $value = null): self |
|
99
|
|
|
{ |
|
100
|
|
|
$this->attributes->put($key, $value); |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function attrs(iterable $attributes): self |
|
106
|
|
|
{ |
|
107
|
|
|
foreach($attributes as $key => $value) { |
|
108
|
|
|
if ($key === 'class') { |
|
109
|
|
|
$this->class($value); |
|
110
|
|
|
} else if($key === 'style') { |
|
111
|
|
|
$this->style($value); |
|
112
|
|
|
} else { |
|
113
|
|
|
$this->attributes->put($key, $value); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.