1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kris\LaravelFormBuilder\Fields; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
class EntityType extends ChoiceType |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @inheritdoc |
13
|
|
|
*/ |
14
|
5 |
|
protected function getDefaults() |
15
|
|
|
{ |
16
|
|
|
$defaults = [ |
17
|
5 |
|
'class' => null, |
18
|
|
|
'query_builder' => null, |
19
|
|
|
'property' => 'name', |
20
|
|
|
'property_key' => null, |
21
|
|
|
]; |
22
|
|
|
|
23
|
5 |
|
return array_merge(parent::getDefaults(), $defaults); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
5 |
|
protected function createChildren() |
30
|
|
|
{ |
31
|
5 |
|
if ($this->getOption('choices')) { |
32
|
1 |
|
return parent::createChildren(); |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
$entity = $this->getOption('class'); |
36
|
4 |
|
$queryBuilder = $this->getOption('query_builder'); |
37
|
4 |
|
$key = $this->getOption('property_key'); |
38
|
4 |
|
$value = $this->getOption('property'); |
39
|
|
|
|
40
|
4 |
|
if (!$entity || !class_exists($entity)) { |
41
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
42
|
1 |
|
'Please provide valid "class" option for entity field [%s] in form class [%s]', |
43
|
1 |
|
$this->getRealName(), |
44
|
1 |
|
get_class($this->parent) |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
$entity = new $entity(); |
49
|
|
|
|
50
|
3 |
|
if ($key === null) { |
51
|
3 |
|
$key = $entity->getKeyName(); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
if ($queryBuilder instanceof \Closure) { |
55
|
1 |
|
$data = $queryBuilder($entity); |
56
|
|
|
} else { |
57
|
2 |
|
$data = $entity; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
if ($value instanceof \Closure) { |
61
|
|
|
$data = $this->get($data); |
62
|
|
|
} else { |
63
|
3 |
|
$data = $this->pluck($value, $key, $data); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
if ($data instanceof Collection) { |
67
|
3 |
|
$data = $data->all(); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
if ($value instanceof \Closure) { |
71
|
|
|
$part = []; |
72
|
|
|
foreach ($data as $item) { |
73
|
|
|
$part[$item->__get($key)] = $value($item); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$data = $part; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
$this->options['choices'] = $data; |
80
|
|
|
|
81
|
3 |
|
return parent::createChildren(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Pluck data. |
86
|
|
|
* |
87
|
|
|
* @param string $value |
88
|
|
|
* @param string $key |
89
|
|
|
* @param mixed $data |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
* */ |
93
|
3 |
|
protected function pluck($value, $key, $data) |
94
|
|
|
{ |
95
|
3 |
|
if (!is_object($data)) { |
96
|
|
|
return $data; |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
if (method_exists($data, 'pluck') || $data instanceof Model) { |
100
|
|
|
//laravel 5.3.* |
101
|
1 |
|
return $data->pluck($value, $key); |
102
|
2 |
|
} elseif (method_exists($data, 'lists')) { |
103
|
|
|
//laravel 5.2.* |
104
|
2 |
|
return $data->lists($value, $key); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function get($data) |
109
|
|
|
{ |
110
|
|
|
if (!is_object($data)) { |
111
|
|
|
return $data; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (method_exists($data, 'get') || $data instanceof Model) { |
115
|
|
|
//laravel 5.3.* |
116
|
|
|
return $data->get(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|